blob: 280e3d7fca4191800eeb7a5ecb09c442f6b13845 [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"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000028using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030
John McCall5fca7ea2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall5fca7ea2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
46 ExpectedStruct
John McCall5fca7ea2011-03-02 12:29:23 +000047};
48
Chris Lattner58418ff2008-06-29 00:16:31 +000049//===----------------------------------------------------------------------===//
50// Helper functions
51//===----------------------------------------------------------------------===//
52
Chandler Carruthff4c4f02011-07-01 23:49:12 +000053static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000054 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000055 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000056 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000057 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000059 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000060 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000061 Ty = decl->getUnderlyingType();
62 else
63 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000064
Chris Lattner2c6fcf52008-06-26 18:38:35 +000065 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000066 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000067 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000068 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069
John McCall9dd450b2009-09-21 23:43:11 +000070 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000071}
72
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000073// FIXME: We should provide an abstraction around a method or function
74// to provide the following bits of information.
75
Nuno Lopes518e3702009-12-20 23:11:08 +000076/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000077/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078static bool isFunction(const Decl *D) {
79 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000080}
81
82/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083/// type (function or function-typed variable) or an Objective-C
84/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085static bool isFunctionOrMethod(const Decl *D) {
86 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000087}
88
Fariborz Jahanian4447e172009-05-15 23:15:03 +000089/// isFunctionOrMethodOrBlock - Return true if the given decl has function
90/// type (function or function-typed variable) or an Objective-C
91/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092static bool isFunctionOrMethodOrBlock(const Decl *D) {
93 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000094 return true;
95 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000097 QualType Ty = V->getType();
98 return Ty->isBlockPointerType();
99 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000101}
102
John McCall3882ace2011-01-05 12:14:39 +0000103/// Return true if the given decl has a declarator that should have
104/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000105static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000106 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
108 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000109}
110
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000111/// hasFunctionProto - Return true if the given decl has a argument
112/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000113/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000114static bool hasFunctionProto(const Decl *D) {
115 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000116 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000117 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000118 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000119 return true;
120 }
121}
122
123/// getFunctionOrMethodNumArgs - Return number of function or method
124/// arguments. It is an error to call this on a K&R function (use
125/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000128 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000130 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000131 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000136 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000138 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000139
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000141}
142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143static QualType getFunctionOrMethodResultType(const Decl *D) {
144 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000145 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000146 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000147}
148
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149static bool isFunctionOrMethodVariadic(const Decl *D) {
150 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000151 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000152 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000153 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000154 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000155 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000157 }
158}
159
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000160static bool isInstanceMethod(const Decl *D) {
161 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000162 return MethodDecl->isInstance();
163 return false;
164}
165
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000166static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000167 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000168 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000169 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000170
John McCall96fa4842010-05-17 21:00:27 +0000171 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
172 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
John McCall96fa4842010-05-17 21:00:27 +0000175 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000176
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000177 // FIXME: Should we walk the chain of classes?
178 return ClsName == &Ctx.Idents.get("NSString") ||
179 ClsName == &Ctx.Idents.get("NSMutableString");
180}
181
Daniel Dunbar980c6692008-09-26 03:32:58 +0000182static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000183 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000184 if (!PT)
185 return false;
186
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000187 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000188 if (!RT)
189 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000190
Daniel Dunbar980c6692008-09-26 03:32:58 +0000191 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000192 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000193 return false;
194
195 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
196}
197
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000198/// \brief Check if the attribute has exactly as many args as Num. May
199/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000200static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
201 unsigned int Num) {
202 if (Attr.getNumArgs() != Num) {
203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
204 return false;
205 }
206
207 return true;
208}
209
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000210
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000211/// \brief Check if the attribute has at least as many args as Num. May
212/// output an error.
213static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
214 unsigned int Num) {
215 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
217 return false;
218 }
219
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000220 return true;
221}
222
223///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000224/// \brief Check if passed in Decl is a field or potentially shared global var
225/// \return true if the Decl is a field or potentially shared global variable
226///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000227static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000228 if (isa<FieldDecl>(D))
229 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000230 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000231 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
232
233 return false;
234}
235
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000236/// \brief Check if the passed-in expression is of type int or bool.
237static bool isIntOrBool(Expr *Exp) {
238 QualType QT = Exp->getType();
239 return QT->isBooleanType() || QT->isIntegerType();
240}
241
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000242
243// Check to see if the type is a smart pointer of some kind. We assume
244// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000245static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
246 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
247 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
248 if (Res1.first == Res1.second)
249 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000250
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000251 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
252 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
253 if (Res2.first == Res2.second)
254 return false;
255
256 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000257}
258
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000259/// \brief Check if passed in Decl is a pointer type.
260/// Note that this function may produce an error message.
261/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000262static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
263 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000264 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000265 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000266 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000267 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000268
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000269 if (const RecordType *RT = QT->getAs<RecordType>()) {
270 // If it's an incomplete type, it could be a smart pointer; skip it.
271 // (We don't want to force template instantiation if we can avoid it,
272 // since that would alter the order in which templates are instantiated.)
273 if (RT->isIncompleteType())
274 return true;
275
276 if (threadSafetyCheckIsSmartPointer(S, RT))
277 return true;
278 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000279
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000280 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000281 << Attr.getName()->getName() << QT;
282 } else {
283 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
284 << Attr.getName();
285 }
286 return false;
287}
288
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000289/// \brief Checks that the passed in QualType either is of RecordType or points
290/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000291static const RecordType *getRecordType(QualType QT) {
292 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000293 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000294
295 // Now check if we point to record type.
296 if (const PointerType *PT = QT->getAs<PointerType>())
297 return PT->getPointeeType()->getAs<RecordType>();
298
299 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000300}
301
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000302
Jordy Rose740b0c22012-05-08 03:27:22 +0000303static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
304 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000305 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
306 if (RT->getDecl()->getAttr<LockableAttr>())
307 return true;
308 return false;
309}
310
311
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000312/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000313/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000314static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
315 QualType Ty) {
316 const RecordType *RT = getRecordType(Ty);
317
318 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000319 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000320 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000321 << Attr.getName() << Ty.getAsString();
322 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000323 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000324
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000325 // Don't check for lockable if the class hasn't been defined yet.
326 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000327 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000328
329 // Allow smart pointers to be used as lockable objects.
330 // FIXME -- Check the type that the smart pointer points to.
331 if (threadSafetyCheckIsSmartPointer(S, RT))
332 return;
333
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000334 // Check if the type is lockable.
335 RecordDecl *RD = RT->getDecl();
336 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000337 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000338
339 // Else check if any base classes are lockable.
340 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
341 CXXBasePaths BPaths(false, false);
342 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
343 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000344 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000345
346 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
347 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000348}
349
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000350/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000351/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000352/// \param Sidx The attribute argument index to start checking with.
353/// \param ParamIdxOk Whether an argument can be indexing into a function
354/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000355static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000356 const AttributeList &Attr,
357 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000358 int Sidx = 0,
359 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000360 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000361 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000362
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000363 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000364 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000365 Args.push_back(ArgExp);
366 continue;
367 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000368
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000369 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
370 // Ignore empty strings without warnings
371 if (StrLit->getLength() == 0)
372 continue;
373
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000374 // We allow constant strings to be used as a placeholder for expressions
375 // that are not valid C++ syntax, but warn that they are ignored.
376 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
377 Attr.getName();
378 continue;
379 }
380
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000381 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000382
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000383 // A pointer to member expression of the form &MyClass::mu is treated
384 // specially -- we need to look at the type of the member.
385 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
386 if (UOp->getOpcode() == UO_AddrOf)
387 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
388 if (DRE->getDecl()->isCXXInstanceMember())
389 ArgTy = DRE->getDecl()->getType();
390
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000391 // First see if we can just cast to record type, or point to record type.
392 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000393
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000394 // Now check if we index into a record type function param.
395 if(!RT && ParamIdxOk) {
396 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000397 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
398 if(FD && IL) {
399 unsigned int NumParams = FD->getNumParams();
400 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000401 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
402 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
403 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
405 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000406 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000407 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000408 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000409 }
410 }
411
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000412 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000413
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000414 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000415 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000416}
417
Chris Lattner58418ff2008-06-29 00:16:31 +0000418//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000419// Attribute Implementations
420//===----------------------------------------------------------------------===//
421
Daniel Dunbar032db472008-07-31 22:40:48 +0000422// FIXME: All this manual attribute parsing code is gross. At the
423// least add some helper functions to check most argument patterns (#
424// and types of args).
425
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000426static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
427 bool pointer = false) {
428 assert(!Attr.isInvalid());
429
430 if (!checkAttributeNumArgs(S, Attr, 0))
431 return;
432
433 // D must be either a member field or global (potentially shared) variable.
434 if (!mayBeSharedVariable(D)) {
435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000436 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000437 return;
438 }
439
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000440 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000441 return;
442
443 if (pointer)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000444 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000445 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000446 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000447}
448
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000449static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000450 bool pointer = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000451 assert(!Attr.isInvalid());
452
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000453 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000454 return;
455
456 // D must be either a member field or global (potentially shared) variable.
457 if (!mayBeSharedVariable(D)) {
458 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000459 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000460 return;
461 }
462
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000463 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000464 return;
465
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000466 SmallVector<Expr*, 1> Args;
467 // check that all arguments are lockable objects
468 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
469 unsigned Size = Args.size();
470 if (Size != 1)
471 return;
472 Expr *Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000473
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000474 if (pointer)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000475 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000477 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000478 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000479}
480
481
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000482static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
483 bool scoped = false) {
484 assert(!Attr.isInvalid());
485
486 if (!checkAttributeNumArgs(S, Attr, 0))
487 return;
488
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000489 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000490 if (!isa<CXXRecordDecl>(D)) {
491 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
492 << Attr.getName() << ExpectedClass;
493 return;
494 }
495
496 if (scoped)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000497 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000498 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000499 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000500}
501
502static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
503 const AttributeList &Attr) {
504 assert(!Attr.isInvalid());
505
506 if (!checkAttributeNumArgs(S, Attr, 0))
507 return;
508
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000510 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
511 << Attr.getName() << ExpectedFunctionOrMethod;
512 return;
513 }
514
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000515 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000516 S.Context));
517}
518
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000519static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000521 assert(!Attr.isInvalid());
522
523 if (!checkAttributeNumArgs(S, Attr, 0))
524 return;
525
526 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
528 << Attr.getName() << ExpectedFunctionOrMethod;
529 return;
530 }
531
532 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
533 S.Context));
534}
535
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000536static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
537 bool before) {
538 assert(!Attr.isInvalid());
539
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000540 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000541 return;
542
543 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000544 ValueDecl *VD = dyn_cast<ValueDecl>(D);
545 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000547 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000548 return;
549 }
550
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000551 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000552 QualType QT = VD->getType();
553 if (!QT->isDependentType()) {
554 const RecordType *RT = getRecordType(QT);
555 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000556 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000557 << Attr.getName();
558 return;
559 }
560 }
561
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000563 // Check that all arguments are lockable objects.
564 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000565 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000566 if (Size == 0)
567 return;
568 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000569
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000570 if (before)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000571 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000572 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000573 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000574 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000575 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000576}
577
578static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000579 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000580 assert(!Attr.isInvalid());
581
582 // zero or more arguments ok
583
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000584 // check that the attribute is applied to a function
585 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
587 << Attr.getName() << ExpectedFunctionOrMethod;
588 return;
589 }
590
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000592 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000593 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000594 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000595 Expr **StartArg = Size == 0 ? 0 : &Args[0];
596
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000597 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000598 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000599 S.Context, StartArg,
600 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000601 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000602 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000603 S.Context, StartArg,
604 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000605}
606
607static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000608 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000609 assert(!Attr.isInvalid());
610
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000611 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000612 return;
613
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000614 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
616 << Attr.getName() << ExpectedFunctionOrMethod;
617 return;
618 }
619
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620 if (!isIntOrBool(Attr.getArg(0))) {
621 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
622 << Attr.getName();
623 return;
624 }
625
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000626 SmallVector<Expr*, 2> Args;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000627 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000628 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000629 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000630 Expr **StartArg = Size == 0 ? 0 : &Args[0];
631
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000632 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000633 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000634 S.Context,
Caitlin Sadowskibf06c722011-09-15 17:50:19 +0000635 Attr.getArg(0),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000636 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000637 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000638 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowskibf06c722011-09-15 17:50:19 +0000639 S.Context,
640 Attr.getArg(0),
641 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000642}
643
644static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000645 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000646 assert(!Attr.isInvalid());
647
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000648 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000649 return;
650
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000651 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
653 << Attr.getName() << ExpectedFunctionOrMethod;
654 return;
655 }
656
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000657 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000658 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000659 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000660 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000661 if (Size == 0)
662 return;
663 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000664
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000665 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000666 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000667 S.Context, StartArg,
668 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000669 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000670 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000671 S.Context, StartArg,
672 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000673}
674
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000675static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000676 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000677 assert(!Attr.isInvalid());
678
679 // zero or more arguments ok
680
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000681 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000682 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
683 << Attr.getName() << ExpectedFunctionOrMethod;
684 return;
685 }
686
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000687 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000688 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000689 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000690 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000691 Expr **StartArg = Size == 0 ? 0 : &Args[0];
692
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000693 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000694 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000695}
696
697static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000699 assert(!Attr.isInvalid());
700
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000701 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000702 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000703 Expr *Arg = Attr.getArg(0);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000704
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
707 << Attr.getName() << ExpectedFunctionOrMethod;
708 return;
709 }
710
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000711 if (Arg->isTypeDependent())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000712 return;
713
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000714 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000715 SmallVector<Expr*, 1> Args;
716 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
717 unsigned Size = Args.size();
718 if (Size == 0)
719 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000720
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000721 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
722 Args[0]));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000723}
724
725static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000726 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727 assert(!Attr.isInvalid());
728
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000729 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000730 return;
731
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000732 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000733 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
734 << Attr.getName() << ExpectedFunctionOrMethod;
735 return;
736 }
737
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000738 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000739 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000740 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000741 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000742 if (Size == 0)
743 return;
744 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000745
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000746 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000747 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000748}
749
750
Chandler Carruthedc2c642011-07-02 00:01:44 +0000751static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
752 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000753 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000754 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000755 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000756 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000757 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000758
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000759 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000760
761 Expr *sizeExpr;
762
763 // Special case where the argument is a template id.
764 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000765 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000766 SourceLocation TemplateKWLoc;
John McCalle66edc12009-11-24 19:00:30 +0000767 UnqualifiedId id;
768 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor39c02722011-06-15 16:02:29 +0000769
Abramo Bagnara7945c982012-01-27 09:46:47 +0000770 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
771 false, false);
Douglas Gregor39c02722011-06-15 16:02:29 +0000772 if (Size.isInvalid())
773 return;
774
775 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000776 } else {
777 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000778 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor758a8692009-06-17 21:51:59 +0000779 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000780
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000781 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000782 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000783
784 // Instantiate/Install the vector type, and let Sema build the type for us.
785 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000786 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000787 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000788 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000789 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000790
Douglas Gregor758a8692009-06-17 21:51:59 +0000791 // Remember this typedef decl, we will need it later for diagnostics.
792 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000793 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000794}
795
Chandler Carruthedc2c642011-07-02 00:01:44 +0000796static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000797 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000798 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000799 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000800
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000801 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000802 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000803 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000804 // If the alignment is less than or equal to 8 bits, the packed attribute
805 // has no effect.
806 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000807 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000808 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000809 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000810 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000811 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000812 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000813 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000814}
815
Chandler Carruthedc2c642011-07-02 00:01:44 +0000816static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000817 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000818 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000819 else
820 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
821}
822
Chandler Carruthedc2c642011-07-02 00:01:44 +0000823static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000824 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000825 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000826 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000827
Ted Kremenek1f672822010-02-18 03:08:58 +0000828 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000829 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000830 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000831 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000832 return;
833 }
834
Ted Kremenekd68ec812011-02-04 06:54:16 +0000835 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000836}
837
Ted Kremenek7fd17232011-09-29 07:02:25 +0000838static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
839 // The IBOutlet/IBOutletCollection attributes only apply to instance
840 // variables or properties of Objective-C classes. The outlet must also
841 // have an object reference type.
842 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
843 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +0000844 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +0000845 << Attr.getName() << VD->getType() << 0;
846 return false;
847 }
848 }
849 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
850 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +0000851 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +0000852 << Attr.getName() << PD->getType() << 1;
853 return false;
854 }
855 }
856 else {
857 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
858 return false;
859 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +0000860
Ted Kremenek7fd17232011-09-29 07:02:25 +0000861 return true;
862}
863
Chandler Carruthedc2c642011-07-02 00:01:44 +0000864static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000865 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000866 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +0000867 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +0000868
869 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +0000870 return;
Ted Kremenek1f672822010-02-18 03:08:58 +0000871
Ted Kremenek7fd17232011-09-29 07:02:25 +0000872 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000873}
874
Chandler Carruthedc2c642011-07-02 00:01:44 +0000875static void handleIBOutletCollection(Sema &S, Decl *D,
876 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000877
878 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000879 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000880 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
881 return;
882 }
883
Ted Kremenek7fd17232011-09-29 07:02:25 +0000884 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +0000885 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +0000886
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000887 IdentifierInfo *II = Attr.getParameterName();
888 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +0000889 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000890
John McCallba7bf592010-08-24 05:47:05 +0000891 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000892 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000893 if (!TypeRep) {
894 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
895 return;
896 }
John McCallba7bf592010-08-24 05:47:05 +0000897 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000898 // Diagnose use of non-object type in iboutletcollection attribute.
899 // FIXME. Gnu attribute extension ignores use of builtin types in
900 // attributes. So, __attribute__((iboutletcollection(char))) will be
901 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +0000902 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000903 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
904 return;
905 }
Argyrios Kyrtzidis8db67df2011-09-13 18:41:59 +0000906 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
907 QT, Attr.getParameterLoc()));
Ted Kremenek26bde772010-05-19 17:38:06 +0000908}
909
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000910static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000911 if (const RecordType *UT = T->getAsUnionType())
912 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
913 RecordDecl *UD = UT->getDecl();
914 for (RecordDecl::field_iterator it = UD->field_begin(),
915 itend = UD->field_end(); it != itend; ++it) {
916 QualType QT = it->getType();
917 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
918 T = QT;
919 return;
920 }
921 }
922 }
923}
924
Chandler Carruthedc2c642011-07-02 00:01:44 +0000925static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000926 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
927 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000928 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000929 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000930 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000931 return;
932 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000933
Chandler Carruth743682b2010-11-16 08:35:43 +0000934 // In C++ the implicit 'this' function parameter also counts, and they are
935 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000936 bool HasImplicitThisParam = isInstanceMethod(D);
937 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000938
939 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000940 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000941
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000942 for (AttributeList::arg_iterator I=Attr.arg_begin(),
943 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000944
945
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000946 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000947 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000948 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000949 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
950 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000951 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
952 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000953 return;
954 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000955
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000956 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000957
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000958 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000959 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000960 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000961 return;
962 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000963
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000964 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000965 if (HasImplicitThisParam) {
966 if (x == 0) {
967 S.Diag(Attr.getLoc(),
968 diag::err_attribute_invalid_implicit_this_argument)
969 << "nonnull" << Ex->getSourceRange();
970 return;
971 }
972 --x;
973 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000974
975 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000976 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000977 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000978
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000979 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000980 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000981 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000982 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000983 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000984 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000985
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000986 NonNullArgs.push_back(x);
987 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000988
989 // If no arguments were specified to __attribute__((nonnull)) then all pointer
990 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000991 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000992 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
993 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000994 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000995 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000996 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000997 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000998
Ted Kremenek22813f42010-10-21 18:49:36 +0000999 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001000 if (NonNullArgs.empty()) {
1001 // Warn the trivial case only if attribute is not coming from a
1002 // macro instantiation.
1003 if (Attr.getLoc().isFileID())
1004 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001005 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001006 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001007 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001008
1009 unsigned* start = &NonNullArgs[0];
1010 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001011 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001012 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001013 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001014}
1015
Chandler Carruthedc2c642011-07-02 00:01:44 +00001016static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001017 // This attribute must be applied to a function declaration.
1018 // The first argument to the attribute must be a string,
1019 // the name of the resource, for example "malloc".
1020 // The following arguments must be argument indexes, the arguments must be
1021 // of integer type for Returns, otherwise of pointer type.
1022 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001023 // after being held. free() should be __attribute((ownership_takes)), whereas
1024 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001025
1026 if (!AL.getParameterName()) {
1027 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1028 << AL.getName()->getName() << 1;
1029 return;
1030 }
1031 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001032 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001033 switch (AL.getKind()) {
1034 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001035 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001036 if (AL.getNumArgs() < 1) {
1037 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1038 return;
1039 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001040 break;
1041 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001042 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001043 if (AL.getNumArgs() < 1) {
1044 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1045 return;
1046 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001047 break;
1048 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001049 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001050 if (AL.getNumArgs() > 1) {
1051 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1052 << AL.getNumArgs() + 1;
1053 return;
1054 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001055 break;
1056 default:
1057 // This should never happen given how we are called.
1058 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001059 }
1060
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001061 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001062 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1063 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001064 return;
1065 }
1066
Chandler Carruth743682b2010-11-16 08:35:43 +00001067 // In C++ the implicit 'this' function parameter also counts, and they are
1068 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001069 bool HasImplicitThisParam = isInstanceMethod(D);
1070 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001071
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001072 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001073
1074 // Normalize the argument, __foo__ becomes foo.
1075 if (Module.startswith("__") && Module.endswith("__"))
1076 Module = Module.substr(2, Module.size() - 4);
1077
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001078 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001079
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001080 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1081 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001082
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001083 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001084 llvm::APSInt ArgNum(32);
1085 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1086 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1087 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1088 << AL.getName()->getName() << IdxExpr->getSourceRange();
1089 continue;
1090 }
1091
1092 unsigned x = (unsigned) ArgNum.getZExtValue();
1093
1094 if (x > NumArgs || x < 1) {
1095 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1096 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1097 continue;
1098 }
1099 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001100 if (HasImplicitThisParam) {
1101 if (x == 0) {
1102 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1103 << "ownership" << IdxExpr->getSourceRange();
1104 return;
1105 }
1106 --x;
1107 }
1108
Ted Kremenekd21139a2010-07-31 01:52:11 +00001109 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001110 case OwnershipAttr::Takes:
1111 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001112 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001113 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001114 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1115 // FIXME: Should also highlight argument in decl.
1116 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001117 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001118 << "pointer"
1119 << IdxExpr->getSourceRange();
1120 continue;
1121 }
1122 break;
1123 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001124 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001125 if (AL.getNumArgs() > 1) {
1126 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001127 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001128 llvm::APSInt ArgNum(32);
1129 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1130 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1131 S.Diag(AL.getLoc(), diag::err_ownership_type)
1132 << "ownership_returns" << "integer"
1133 << IdxExpr->getSourceRange();
1134 return;
1135 }
1136 }
1137 break;
1138 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001139 } // switch
1140
1141 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001142 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001143 i = D->specific_attr_begin<OwnershipAttr>(),
1144 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001145 i != e; ++i) {
1146 if ((*i)->getOwnKind() != K) {
1147 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1148 I!=E; ++I) {
1149 if (x == *I) {
1150 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1151 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001152 }
1153 }
1154 }
1155 }
1156 OwnershipArgs.push_back(x);
1157 }
1158
1159 unsigned* start = OwnershipArgs.data();
1160 unsigned size = OwnershipArgs.size();
1161 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001162
1163 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1164 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1165 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001166 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001167
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001168 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001169 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001170}
1171
John McCall7a198ce2011-02-08 22:35:49 +00001172/// Whether this declaration has internal linkage for the purposes of
1173/// things that want to complain about things not have internal linkage.
1174static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1175 switch (D->getLinkage()) {
1176 case NoLinkage:
1177 case InternalLinkage:
1178 return true;
1179
1180 // Template instantiations that go from external to unique-external
1181 // shouldn't get diagnosed.
1182 case UniqueExternalLinkage:
1183 return true;
1184
1185 case ExternalLinkage:
1186 return false;
1187 }
1188 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001189}
1190
Chandler Carruthedc2c642011-07-02 00:01:44 +00001191static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001192 // Check the attribute arguments.
1193 if (Attr.getNumArgs() > 1) {
1194 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1195 return;
1196 }
1197
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001198 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001200 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001201 return;
1202 }
1203
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001204 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001205
Rafael Espindolac18086a2010-02-23 22:00:30 +00001206 // gcc rejects
1207 // class c {
1208 // static int a __attribute__((weakref ("v2")));
1209 // static int b() __attribute__((weakref ("f3")));
1210 // };
1211 // and ignores the attributes of
1212 // void f(void) {
1213 // static int a __attribute__((weakref ("v2")));
1214 // }
1215 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001216 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001217 if (!Ctx->isFileContext()) {
1218 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001219 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001220 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001221 }
1222
1223 // The GCC manual says
1224 //
1225 // At present, a declaration to which `weakref' is attached can only
1226 // be `static'.
1227 //
1228 // It also says
1229 //
1230 // Without a TARGET,
1231 // given as an argument to `weakref' or to `alias', `weakref' is
1232 // equivalent to `weak'.
1233 //
1234 // gcc 4.4.1 will accept
1235 // int a7 __attribute__((weakref));
1236 // as
1237 // int a7 __attribute__((weak));
1238 // This looks like a bug in gcc. We reject that for now. We should revisit
1239 // it if this behaviour is actually used.
1240
John McCall7a198ce2011-02-08 22:35:49 +00001241 if (!hasEffectivelyInternalLinkage(nd)) {
1242 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001243 return;
1244 }
1245
1246 // GCC rejects
1247 // static ((alias ("y"), weakref)).
1248 // Should we? How to check that weakref is before or after alias?
1249
1250 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001251 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001252 Arg = Arg->IgnoreParenCasts();
1253 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1254
Douglas Gregorfb65e592011-07-27 05:40:30 +00001255 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1257 << "weakref" << 1;
1258 return;
1259 }
1260 // GCC will accept anything as the argument of weakref. Should we
1261 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001262 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001263 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001264 }
1265
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001266 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001267}
1268
Chandler Carruthedc2c642011-07-02 00:01:44 +00001269static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001270 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001271 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001272 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001273 return;
1274 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001275
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001276 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001277 Arg = Arg->IgnoreParenCasts();
1278 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001279
Douglas Gregorfb65e592011-07-27 05:40:30 +00001280 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001282 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001283 return;
1284 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001285
Douglas Gregore8bbc122011-09-02 00:18:52 +00001286 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001287 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1288 return;
1289 }
1290
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001291 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001292
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001293 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001294 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001295}
1296
Chandler Carruthedc2c642011-07-02 00:01:44 +00001297static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001298 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001299 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001300 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001301
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001302 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001304 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001305 return;
1306 }
1307
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001308 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001309}
1310
Chandler Carruthedc2c642011-07-02 00:01:44 +00001311static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1312 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001313 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001314 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1316 return;
1317 }
1318
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001319 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001320 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001321 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001322 return;
1323 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001324
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001325 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001326}
1327
Chandler Carruthedc2c642011-07-02 00:01:44 +00001328static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001329 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001330 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001331 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1332 return;
1333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001335 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001336 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001337 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001338 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001339 return;
1340 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001341 }
1342
Ted Kremenek08479ae2009-08-15 00:51:46 +00001343 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001344}
1345
Chandler Carruthedc2c642011-07-02 00:01:44 +00001346static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001347 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001348 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001349 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001350
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001351 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001352}
1353
Chandler Carruthedc2c642011-07-02 00:01:44 +00001354static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001355 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001356 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001357 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001358 else
1359 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001360 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001361}
1362
Chandler Carruthedc2c642011-07-02 00:01:44 +00001363static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001364 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001365 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001366 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001367 else
1368 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001369 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001370}
1371
Chandler Carruthedc2c642011-07-02 00:01:44 +00001372static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001373 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001374
1375 if (S.CheckNoReturnAttr(attr)) return;
1376
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001377 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001378 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001379 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001380 return;
1381 }
1382
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001383 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001384}
1385
1386bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001387 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001388 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1389 attr.setInvalid();
1390 return true;
1391 }
1392
1393 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001394}
1395
Chandler Carruthedc2c642011-07-02 00:01:44 +00001396static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1397 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001398
1399 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1400 // because 'analyzer_noreturn' does not impact the type.
1401
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001402 if(!checkAttributeNumArgs(S, Attr, 0))
1403 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001404
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001405 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1406 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001407 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1408 && !VD->getType()->isFunctionPointerType())) {
1409 S.Diag(Attr.getLoc(),
1410 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1411 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001412 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001413 return;
1414 }
1415 }
1416
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001417 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001418}
1419
John Thompsoncdb847ba2010-08-09 21:53:52 +00001420// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001421static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001422/*
1423 Returning a Vector Class in Registers
1424
Eric Christopherbc638a82010-12-01 22:13:54 +00001425 According to the PPU ABI specifications, a class with a single member of
1426 vector type is returned in memory when used as the return value of a function.
1427 This results in inefficient code when implementing vector classes. To return
1428 the value in a single vector register, add the vecreturn attribute to the
1429 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001430
1431 Example:
1432
1433 struct Vector
1434 {
1435 __vector float xyzw;
1436 } __attribute__((vecreturn));
1437
1438 Vector Add(Vector lhs, Vector rhs)
1439 {
1440 Vector result;
1441 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1442 return result; // This will be returned in a register
1443 }
1444*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001445 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001446 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001447 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001448 return;
1449 }
1450
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001451 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001452 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1453 return;
1454 }
1455
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001456 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001457 int count = 0;
1458
1459 if (!isa<CXXRecordDecl>(record)) {
1460 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1461 return;
1462 }
1463
1464 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1465 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1466 return;
1467 }
1468
Eric Christopherbc638a82010-12-01 22:13:54 +00001469 for (RecordDecl::field_iterator iter = record->field_begin();
1470 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001471 if ((count == 1) || !iter->getType()->isVectorType()) {
1472 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1473 return;
1474 }
1475 count++;
1476 }
1477
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001478 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001479}
1480
Chandler Carruthedc2c642011-07-02 00:01:44 +00001481static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001482 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001484 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001485 return;
1486 }
1487 // FIXME: Actually store the attribute on the declaration
1488}
1489
Chandler Carruthedc2c642011-07-02 00:01:44 +00001490static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001491 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001492 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001494 return;
1495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001497 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1498 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001499 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001500 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001501 return;
1502 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001503
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001504 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001505}
1506
Rafael Espindola70107f92011-10-03 14:59:42 +00001507static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1508 const AttributeList &Attr) {
1509 // check the attribute arguments.
1510 if (Attr.hasParameterOrArguments()) {
1511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1512 return;
1513 }
1514
1515 if (!isa<FunctionDecl>(D)) {
1516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1517 << Attr.getName() << ExpectedFunction;
1518 return;
1519 }
1520
1521 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1522}
1523
Chandler Carruthedc2c642011-07-02 00:01:44 +00001524static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001525 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001526 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1528 return;
1529 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001530
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001531 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001532 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001533 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1534 return;
1535 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001536 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001537 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001538 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001539 return;
1540 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001541
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001542 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001543}
1544
Chandler Carruthedc2c642011-07-02 00:01:44 +00001545static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001546 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001547 if (Attr.getNumArgs() > 1) {
1548 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001549 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001550 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001551
1552 int priority = 65535; // FIXME: Do not hardcode such constants.
1553 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001554 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001555 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001556 if (E->isTypeDependent() || E->isValueDependent() ||
1557 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001558 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001559 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001560 return;
1561 }
1562 priority = Idx.getZExtValue();
1563 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001564
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001565 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001566 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001567 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001568 return;
1569 }
1570
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001571 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001572 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001573}
1574
Chandler Carruthedc2c642011-07-02 00:01:44 +00001575static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001576 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001577 if (Attr.getNumArgs() > 1) {
1578 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001579 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001580 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001581
1582 int priority = 65535; // FIXME: Do not hardcode such constants.
1583 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001584 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001585 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001586 if (E->isTypeDependent() || E->isValueDependent() ||
1587 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001588 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001589 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001590 return;
1591 }
1592 priority = Idx.getZExtValue();
1593 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001594
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001595 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001596 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001597 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001598 return;
1599 }
1600
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001601 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001602 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001603}
1604
Chandler Carruthedc2c642011-07-02 00:01:44 +00001605static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001606 unsigned NumArgs = Attr.getNumArgs();
1607 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001608 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001609 return;
1610 }
Chris Lattner190aa102011-02-24 05:42:24 +00001611
Fariborz Jahanian551063102010-10-06 21:18:44 +00001612 // Handle the case where deprecated attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001613 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001614 if (NumArgs == 1) {
1615 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001616 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001617 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1618 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001619 return;
1620 }
Chris Lattner190aa102011-02-24 05:42:24 +00001621 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001622 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001623
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001624 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001625}
1626
Chandler Carruthedc2c642011-07-02 00:01:44 +00001627static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001628 unsigned NumArgs = Attr.getNumArgs();
1629 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001630 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001631 return;
1632 }
Chris Lattner190aa102011-02-24 05:42:24 +00001633
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001634 // Handle the case where unavailable attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001635 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001636 if (NumArgs == 1) {
1637 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001638 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001639 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001640 diag::err_attribute_not_string) << "unavailable";
1641 return;
1642 }
Chris Lattner190aa102011-02-24 05:42:24 +00001643 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001644 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001645 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001646}
1647
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001648static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1649 const AttributeList &Attr) {
1650 unsigned NumArgs = Attr.getNumArgs();
1651 if (NumArgs > 0) {
1652 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1653 return;
1654 }
1655
1656 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001657 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001658}
1659
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001660static void handleObjCRootClassAttr(Sema &S, Decl *D,
1661 const AttributeList &Attr) {
1662 if (!isa<ObjCInterfaceDecl>(D)) {
1663 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1664 return;
1665 }
1666
1667 unsigned NumArgs = Attr.getNumArgs();
1668 if (NumArgs > 0) {
1669 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1670 return;
1671 }
1672
1673 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1674}
1675
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001676static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001677 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001678 if (!isa<ObjCInterfaceDecl>(D)) {
1679 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1680 return;
1681 }
1682
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001683 unsigned NumArgs = Attr.getNumArgs();
1684 if (NumArgs > 0) {
1685 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1686 return;
1687 }
1688
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001689 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001690 Attr.getRange(), S.Context));
1691}
1692
Jordy Rose740b0c22012-05-08 03:27:22 +00001693static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1694 IdentifierInfo *Platform,
1695 VersionTuple Introduced,
1696 VersionTuple Deprecated,
1697 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001698 StringRef PlatformName
1699 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1700 if (PlatformName.empty())
1701 PlatformName = Platform->getName();
1702
1703 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1704 // of these steps are needed).
1705 if (!Introduced.empty() && !Deprecated.empty() &&
1706 !(Introduced <= Deprecated)) {
1707 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1708 << 1 << PlatformName << Deprecated.getAsString()
1709 << 0 << Introduced.getAsString();
1710 return true;
1711 }
1712
1713 if (!Introduced.empty() && !Obsoleted.empty() &&
1714 !(Introduced <= Obsoleted)) {
1715 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1716 << 2 << PlatformName << Obsoleted.getAsString()
1717 << 0 << Introduced.getAsString();
1718 return true;
1719 }
1720
1721 if (!Deprecated.empty() && !Obsoleted.empty() &&
1722 !(Deprecated <= Obsoleted)) {
1723 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1724 << 2 << PlatformName << Obsoleted.getAsString()
1725 << 1 << Deprecated.getAsString();
1726 return true;
1727 }
1728
1729 return false;
1730}
1731
Rafael Espindolac67f2232012-05-10 02:50:16 +00001732bool Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1733 bool Inherited,
1734 IdentifierInfo *Platform,
1735 VersionTuple Introduced,
1736 VersionTuple Deprecated,
1737 VersionTuple Obsoleted,
1738 bool IsUnavailable,
1739 StringRef Message) {
1740 VersionTuple MergedIntroduced = Introduced;
1741 VersionTuple MergedDeprecated = Deprecated;
1742 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001743 bool FoundAny = false;
1744
Rafael Espindolac67f2232012-05-10 02:50:16 +00001745 if (D->hasAttrs()) {
1746 AttrVec &Attrs = D->getAttrs();
1747 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1748 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1749 if (!OldAA) {
1750 ++i;
1751 continue;
1752 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001753
Rafael Espindolac67f2232012-05-10 02:50:16 +00001754 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1755 if (OldPlatform != Platform) {
1756 ++i;
1757 continue;
1758 }
1759
1760 FoundAny = true;
1761 VersionTuple OldIntroduced = OldAA->getIntroduced();
1762 VersionTuple OldDeprecated = OldAA->getDeprecated();
1763 VersionTuple OldObsoleted = OldAA->getObsoleted();
1764 bool OldIsUnavailable = OldAA->getUnavailable();
1765 StringRef OldMessage = OldAA->getMessage();
1766
1767 if ((!OldIntroduced.empty() && !Introduced.empty() &&
1768 OldIntroduced != Introduced) ||
1769 (!OldDeprecated.empty() && !Deprecated.empty() &&
1770 OldDeprecated != Deprecated) ||
1771 (!OldObsoleted.empty() && !Obsoleted.empty() &&
1772 OldObsoleted != Obsoleted) ||
1773 (OldIsUnavailable != IsUnavailable) ||
1774 (OldMessage != Message)) {
1775 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1776 Diag(Range.getBegin(), diag::note_previous_attribute);
1777 Attrs.erase(Attrs.begin() + i);
1778 --e;
1779 continue;
1780 }
1781
1782 VersionTuple MergedIntroduced2 = MergedIntroduced;
1783 VersionTuple MergedDeprecated2 = MergedDeprecated;
1784 VersionTuple MergedObsoleted2 = MergedObsoleted;
1785
1786 if (MergedIntroduced2.empty())
1787 MergedIntroduced2 = OldIntroduced;
1788 if (MergedDeprecated2.empty())
1789 MergedDeprecated2 = OldDeprecated;
1790 if (MergedObsoleted2.empty())
1791 MergedObsoleted2 = OldObsoleted;
1792
1793 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1794 MergedIntroduced2, MergedDeprecated2,
1795 MergedObsoleted2)) {
1796 Attrs.erase(Attrs.begin() + i);
1797 --e;
1798 continue;
1799 }
1800
1801 MergedIntroduced = MergedIntroduced2;
1802 MergedDeprecated = MergedDeprecated2;
1803 MergedObsoleted = MergedObsoleted2;
1804 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001805 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001806 }
1807
1808 if (FoundAny &&
1809 MergedIntroduced == Introduced &&
1810 MergedDeprecated == Deprecated &&
1811 MergedObsoleted == Obsoleted)
Rafael Espindolac67f2232012-05-10 02:50:16 +00001812 return false;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001813
Rafael Espindolac67f2232012-05-10 02:50:16 +00001814 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001815 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001816 AvailabilityAttr *Attr =
1817 ::new (Context) AvailabilityAttr(Range, Context, Platform,
1818 Introduced, Deprecated,
1819 Obsoleted, IsUnavailable, Message);
1820
1821 if (Inherited)
1822 Attr->setInherited(true);
1823 D->addAttr(Attr);
1824 return true;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001825 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 return false;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001827}
1828
Chandler Carruthedc2c642011-07-02 00:01:44 +00001829static void handleAvailabilityAttr(Sema &S, Decl *D,
1830 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001831 IdentifierInfo *Platform = Attr.getParameterName();
1832 SourceLocation PlatformLoc = Attr.getParameterLoc();
1833
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001834 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001835 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1836 << Platform;
1837
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001838 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1839 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1840 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001841 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001842 StringRef Str;
1843 const StringLiteral *SE =
1844 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1845 if (SE)
1846 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001847
Rafael Espindolac67f2232012-05-10 02:50:16 +00001848 S.mergeAvailabilityAttr(D, Attr.getRange(),
1849 false, Platform,
1850 Introduced.Version,
1851 Deprecated.Version,
1852 Obsoleted.Version,
1853 IsUnavailable,
1854 Str);
1855}
1856
1857bool Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
1858 bool Inherited,
1859 VisibilityAttr::VisibilityType Vis) {
1860 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
1861 if (ExistingAttr) {
1862 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
1863 if (ExistingVis == Vis)
1864 return false;
1865 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
1866 Diag(Range.getBegin(), diag::note_previous_attribute);
1867 D->dropAttr<VisibilityAttr>();
1868 }
1869 VisibilityAttr *Attr = ::new (Context) VisibilityAttr(Range, Context, Vis);
1870 if (Inherited)
1871 Attr->setInherited(true);
1872 D->addAttr(Attr);
1873 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001874}
1875
Chandler Carruthedc2c642011-07-02 00:01:44 +00001876static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001877 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001878 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001879 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001880
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001881 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001882 Arg = Arg->IgnoreParenCasts();
1883 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001884
Douglas Gregorfb65e592011-07-27 05:40:30 +00001885 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001886 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001887 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001888 return;
1889 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001890
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001891 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001892 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001893
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001894 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001895 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001896 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001897 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001898 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001899 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00001900 else if (TypeStr == "protected") {
1901 // Complain about attempts to use protected visibility on targets
1902 // (like Darwin) that don't support it.
1903 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1904 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1905 type = VisibilityAttr::Default;
1906 } else {
1907 type = VisibilityAttr::Protected;
1908 }
1909 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001910 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001911 return;
1912 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001913
Rafael Espindolac67f2232012-05-10 02:50:16 +00001914 S.mergeVisibilityAttr(D, Attr.getRange(), false, type);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001915}
1916
Chandler Carruthedc2c642011-07-02 00:01:44 +00001917static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1918 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001919 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1920 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001922 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001923 return;
1924 }
1925
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001926 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1927 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1928 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001929 << "objc_method_family" << 1;
1930 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001931 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001932 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001933 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001934 return;
1935 }
1936
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001937 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001938 ObjCMethodFamilyAttr::FamilyKind family;
1939 if (param == "none")
1940 family = ObjCMethodFamilyAttr::OMF_None;
1941 else if (param == "alloc")
1942 family = ObjCMethodFamilyAttr::OMF_alloc;
1943 else if (param == "copy")
1944 family = ObjCMethodFamilyAttr::OMF_copy;
1945 else if (param == "init")
1946 family = ObjCMethodFamilyAttr::OMF_init;
1947 else if (param == "mutableCopy")
1948 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1949 else if (param == "new")
1950 family = ObjCMethodFamilyAttr::OMF_new;
1951 else {
1952 // Just warn and ignore it. This is future-proof against new
1953 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001954 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001955 return;
1956 }
1957
John McCall31168b02011-06-15 23:02:42 +00001958 if (family == ObjCMethodFamilyAttr::OMF_init &&
1959 !method->getResultType()->isObjCObjectPointerType()) {
1960 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1961 << method->getResultType();
1962 // Ignore the attribute.
1963 return;
1964 }
1965
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001966 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00001967 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001968}
1969
Chandler Carruthedc2c642011-07-02 00:01:44 +00001970static void handleObjCExceptionAttr(Sema &S, Decl *D,
1971 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001972 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00001973 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001974
Chris Lattner677a3582009-02-14 08:09:34 +00001975 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1976 if (OCI == 0) {
1977 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1978 return;
1979 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001980
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001981 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001982}
1983
Chandler Carruthedc2c642011-07-02 00:01:44 +00001984static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001985 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001986 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001987 return;
1988 }
Richard Smithdda56e42011-04-15 14:24:37 +00001989 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001990 QualType T = TD->getUnderlyingType();
1991 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001992 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001993 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1994 return;
1995 }
1996 }
Ted Kremenek05e916b2012-03-01 01:40:32 +00001997 else if (!isa<ObjCPropertyDecl>(D)) {
1998 // It is okay to include this attribute on properties, e.g.:
1999 //
2000 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2001 //
2002 // In this case it follows tradition and suppresses an error in the above
2003 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002004 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002005 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002006 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002007}
2008
Mike Stumpd3bb5572009-07-24 19:02:52 +00002009static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002010handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002011 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002012 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002013 return;
2014 }
2015
2016 if (!isa<FunctionDecl>(D)) {
2017 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2018 return;
2019 }
2020
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002021 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002022}
2023
Chandler Carruthedc2c642011-07-02 00:01:44 +00002024static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002025 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002027 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002028 return;
2029 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002030
Steve Naroff3405a732008-09-18 16:44:58 +00002031 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002032 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002033 return;
2034 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002035
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002036 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002037 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002038 type = BlocksAttr::ByRef;
2039 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002040 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002041 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002042 return;
2043 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002044
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002045 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002046}
2047
Chandler Carruthedc2c642011-07-02 00:01:44 +00002048static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002049 // check the attribute arguments.
2050 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002051 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002052 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002053 }
2054
John McCallb46f2872011-09-09 07:56:05 +00002055 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002056 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002057 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002058 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002059 if (E->isTypeDependent() || E->isValueDependent() ||
2060 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002061 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002062 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002063 return;
2064 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002065
John McCallb46f2872011-09-09 07:56:05 +00002066 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002067 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2068 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002069 return;
2070 }
John McCallb46f2872011-09-09 07:56:05 +00002071
2072 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002073 }
2074
John McCallb46f2872011-09-09 07:56:05 +00002075 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002077 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002078 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002079 if (E->isTypeDependent() || E->isValueDependent() ||
2080 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002082 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002083 return;
2084 }
2085 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002086
John McCallb46f2872011-09-09 07:56:05 +00002087 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002088 // FIXME: This error message could be improved, it would be nice
2089 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002090 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2091 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002092 return;
2093 }
2094 }
2095
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002096 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002097 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002098 if (isa<FunctionNoProtoType>(FT)) {
2099 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2100 return;
2101 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002102
Chris Lattner9363e312009-03-17 23:03:47 +00002103 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002104 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002105 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002106 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002107 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002108 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002109 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002110 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002111 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002112 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2113 if (!BD->isVariadic()) {
2114 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2115 return;
2116 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002117 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002118 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002119 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002120 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002121 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002122 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002123 int m = Ty->isFunctionPointerType() ? 0 : 1;
2124 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002125 return;
2126 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002127 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002128 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002129 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002130 return;
2131 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002133 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002134 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002135 return;
2136 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002137 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002138 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002139}
2140
Chandler Carruthedc2c642011-07-02 00:01:44 +00002141static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002142 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002143 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002144 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002145
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002146 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002147 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002148 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002149 return;
2150 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002151
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002152 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2153 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2154 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002155 return;
2156 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002157 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2158 if (MD->getResultType()->isVoidType()) {
2159 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2160 << Attr.getName() << 1;
2161 return;
2162 }
2163
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002164 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002165}
2166
Chandler Carruthedc2c642011-07-02 00:01:44 +00002167static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002168 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002169 if (Attr.hasParameterOrArguments()) {
2170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002171 return;
2172 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002173
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002174 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002175 if (isa<CXXRecordDecl>(D)) {
2176 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2177 return;
2178 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002179 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2180 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002181 return;
2182 }
2183
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002184 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002185
2186 // 'weak' only applies to declarations with external linkage.
2187 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002188 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002189 return;
2190 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002191
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002192 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002193}
2194
Chandler Carruthedc2c642011-07-02 00:01:44 +00002195static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002196 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002197 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002198 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002199
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002200
2201 // weak_import only applies to variable & function declarations.
2202 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002203 if (!D->canBeWeakImported(isDef)) {
2204 if (isDef)
2205 S.Diag(Attr.getLoc(),
2206 diag::warn_attribute_weak_import_invalid_on_definition)
2207 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002208 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002209 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002210 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002211 // Nothing to warn about here.
2212 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002213 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002214 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002215
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002216 return;
2217 }
2218
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002219 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002220}
2221
Chandler Carruthedc2c642011-07-02 00:01:44 +00002222static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2223 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002224 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002225 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00002226 return;
Nate Begemanf2758702009-06-26 06:32:41 +00002227
2228 unsigned WGSize[3];
2229 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002230 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002231 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002232 if (E->isTypeDependent() || E->isValueDependent() ||
2233 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2235 << "reqd_work_group_size" << E->getSourceRange();
2236 return;
2237 }
2238 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2239 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002240 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002241 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00002242 WGSize[2]));
2243}
2244
Chandler Carruthedc2c642011-07-02 00:01:44 +00002245static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002246 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002247 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002248 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002249
2250 // Make sure that there is a string literal as the sections's single
2251 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002252 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002253 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002254 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002255 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002256 return;
2257 }
Mike Stump11289f42009-09-09 15:08:12 +00002258
Chris Lattner30ba6742009-08-10 19:03:04 +00002259 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002260 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002261 if (!Error.empty()) {
2262 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2263 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002264 return;
2265 }
Mike Stump11289f42009-09-09 15:08:12 +00002266
Chris Lattner20aee9b2010-01-12 20:58:53 +00002267 // This attribute cannot be applied to local variables.
2268 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2269 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2270 return;
2271 }
2272
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002273 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002274 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00002275}
2276
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002277
Chandler Carruthedc2c642011-07-02 00:01:44 +00002278static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002279 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002280 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002282 return;
2283 }
Douglas Gregor88336832011-06-15 05:45:11 +00002284
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002285 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002286 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002287 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002288 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002289 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002290 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002291}
2292
Chandler Carruthedc2c642011-07-02 00:01:44 +00002293static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002294 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002295 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002296 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002297 return;
2298 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002299
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002300 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002301 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002302 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002303 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002304 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002305 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002306}
2307
Chandler Carruthedc2c642011-07-02 00:01:44 +00002308static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002309 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002310 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002311 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002312
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002313 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002314}
2315
Chandler Carruthedc2c642011-07-02 00:01:44 +00002316static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002317 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002318 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2319 return;
2320 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002321
Anders Carlssond277d792009-01-31 01:16:18 +00002322 if (Attr.getNumArgs() != 0) {
2323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2324 return;
2325 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002327 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002328
Anders Carlssond277d792009-01-31 01:16:18 +00002329 if (!VD || !VD->hasLocalStorage()) {
2330 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2331 return;
2332 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002333
Anders Carlssond277d792009-01-31 01:16:18 +00002334 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002335 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002336 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002337 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2338 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002339 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002340 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002341 Attr.getParameterName();
2342 return;
2343 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002344
Anders Carlssond277d792009-01-31 01:16:18 +00002345 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2346 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002347 S.Diag(Attr.getParameterLoc(),
2348 diag::err_attribute_cleanup_arg_not_function)
2349 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002350 return;
2351 }
2352
Anders Carlssond277d792009-01-31 01:16:18 +00002353 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002354 S.Diag(Attr.getParameterLoc(),
2355 diag::err_attribute_cleanup_func_must_take_one_arg)
2356 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002357 return;
2358 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002359
Anders Carlsson723f55d2009-02-07 23:16:50 +00002360 // We're currently more strict than GCC about what function types we accept.
2361 // If this ever proves to be a problem it should be easy to fix.
2362 QualType Ty = S.Context.getPointerType(VD->getType());
2363 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002364 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2365 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002366 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002367 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2368 Attr.getParameterName() << ParamTy << Ty;
2369 return;
2370 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002371
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002372 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002373 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002374}
2375
Mike Stumpd3bb5572009-07-24 19:02:52 +00002376/// Handle __attribute__((format_arg((idx)))) attribute based on
2377/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002378static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002379 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002380 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002381
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002382 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002384 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002385 return;
2386 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002387
2388 // In C++ the implicit 'this' function parameter also counts, and they are
2389 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002390 bool HasImplicitThisParam = isInstanceMethod(D);
2391 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002392 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002393
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002394 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002395 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002396 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002397 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2398 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002399 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2400 << "format" << 2 << IdxExpr->getSourceRange();
2401 return;
2402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002404 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2406 << "format" << 2 << IdxExpr->getSourceRange();
2407 return;
2408 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002409
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002410 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002411
Chandler Carruth743682b2010-11-16 08:35:43 +00002412 if (HasImplicitThisParam) {
2413 if (ArgIdx == 0) {
2414 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2415 << "format_arg" << IdxExpr->getSourceRange();
2416 return;
2417 }
2418 ArgIdx--;
2419 }
2420
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002421 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002422 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002423
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002424 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2425 if (not_nsstring_type &&
2426 !isCFStringType(Ty, S.Context) &&
2427 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002428 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002429 // FIXME: Should highlight the actual expression that has the wrong type.
2430 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002431 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002432 << IdxExpr->getSourceRange();
2433 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002434 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002435 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002436 if (!isNSStringType(Ty, S.Context) &&
2437 !isCFStringType(Ty, S.Context) &&
2438 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002439 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002440 // FIXME: Should highlight the actual expression that has the wrong type.
2441 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002442 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002443 << IdxExpr->getSourceRange();
2444 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002445 }
2446
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002447 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002448 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002449}
2450
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002451enum FormatAttrKind {
2452 CFStringFormat,
2453 NSStringFormat,
2454 StrftimeFormat,
2455 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002456 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002457 InvalidFormat
2458};
2459
2460/// getFormatAttrKind - Map from format attribute names to supported format
2461/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002462static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002463 // Check for formats that get handled specially.
2464 if (Format == "NSString")
2465 return NSStringFormat;
2466 if (Format == "CFString")
2467 return CFStringFormat;
2468 if (Format == "strftime")
2469 return StrftimeFormat;
2470
2471 // Otherwise, check for supported formats.
2472 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupasfc0da1a2012-01-27 09:14:17 +00002473 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00002474 Format == "zcmn_err" ||
2475 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002476 return SupportedFormat;
2477
Duncan Sandsde4fe352010-03-23 14:44:19 +00002478 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2479 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00002480 return IgnoredFormat;
2481
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002482 return InvalidFormat;
2483}
2484
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002485/// Handle __attribute__((init_priority(priority))) attributes based on
2486/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002487static void handleInitPriorityAttr(Sema &S, Decl *D,
2488 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002489 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002490 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2491 return;
2492 }
2493
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002494 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002495 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2496 Attr.setInvalid();
2497 return;
2498 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002499 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002500 if (S.Context.getAsArrayType(T))
2501 T = S.Context.getBaseElementType(T);
2502 if (!T->getAs<RecordType>()) {
2503 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2504 Attr.setInvalid();
2505 return;
2506 }
2507
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002508 if (Attr.getNumArgs() != 1) {
2509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2510 Attr.setInvalid();
2511 return;
2512 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002513 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002514
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002515 llvm::APSInt priority(32);
2516 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2517 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2519 << "init_priority" << priorityExpr->getSourceRange();
2520 Attr.setInvalid();
2521 return;
2522 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002523 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002524 if (prioritynum < 101 || prioritynum > 65535) {
2525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2526 << priorityExpr->getSourceRange();
2527 Attr.setInvalid();
2528 return;
2529 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002530 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002531 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002532}
2533
Mike Stumpd3bb5572009-07-24 19:02:52 +00002534/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2535/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002536static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002537
Chris Lattner4a927cb2008-06-28 23:36:30 +00002538 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002540 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002541 return;
2542 }
2543
Chris Lattner4a927cb2008-06-28 23:36:30 +00002544 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002546 return;
2547 }
2548
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002549 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002551 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002552 return;
2553 }
2554
Chandler Carruth743682b2010-11-16 08:35:43 +00002555 // In C++ the implicit 'this' function parameter also counts, and they are
2556 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002557 bool HasImplicitThisParam = isInstanceMethod(D);
2558 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559 unsigned FirstIdx = 1;
2560
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002561 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562
2563 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002564 if (Format.startswith("__") && Format.endswith("__"))
2565 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002567 // Check for supported formats.
2568 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002569
2570 if (Kind == IgnoredFormat)
2571 return;
2572
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002573 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002574 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002575 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576 return;
2577 }
2578
2579 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002580 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002581 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002582 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2583 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002585 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002586 return;
2587 }
2588
2589 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002591 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592 return;
2593 }
2594
2595 // FIXME: Do we need to bounds check?
2596 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002597
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002598 if (HasImplicitThisParam) {
2599 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002600 S.Diag(Attr.getLoc(),
2601 diag::err_format_attribute_implicit_this_format_string)
2602 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002603 return;
2604 }
2605 ArgIdx--;
2606 }
Mike Stump11289f42009-09-09 15:08:12 +00002607
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002609 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002611 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002612 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002613 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2614 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002615 return;
2616 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002617 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002618 // FIXME: do we need to check if the type is NSString*? What are the
2619 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002620 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002621 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002622 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2623 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002624 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002625 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002626 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002627 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002628 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002629 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2630 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631 return;
2632 }
2633
2634 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002635 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002636 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002637 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2638 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002639 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002640 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641 return;
2642 }
2643
2644 // check if the function is variadic if the 3rd argument non-zero
2645 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002646 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002647 ++NumArgs; // +1 for ...
2648 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002649 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650 return;
2651 }
2652 }
2653
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002654 // strftime requires FirstArg to be 0 because it doesn't read from any
2655 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002656 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002658 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2659 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002660 return;
2661 }
2662 // if 0 it disables parameter checking (to use with e.g. va_list)
2663 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002665 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002666 return;
2667 }
2668
Douglas Gregor88336832011-06-15 05:45:11 +00002669 // Check whether we already have an equivalent format attribute.
2670 for (specific_attr_iterator<FormatAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002671 i = D->specific_attr_begin<FormatAttr>(),
2672 e = D->specific_attr_end<FormatAttr>();
Douglas Gregor88336832011-06-15 05:45:11 +00002673 i != e ; ++i) {
2674 FormatAttr *f = *i;
2675 if (f->getType() == Format &&
2676 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2677 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2678 // If we don't have a valid location for this attribute, adopt the
2679 // location.
2680 if (f->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002681 f->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002682 return;
2683 }
2684 }
2685
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002686 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002687 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002688 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002689}
2690
Chandler Carruthedc2c642011-07-02 00:01:44 +00002691static void handleTransparentUnionAttr(Sema &S, Decl *D,
2692 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002693 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002694 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002695 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002696
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002697
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002698 // Try to find the underlying union declaration.
2699 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002700 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002701 if (TD && TD->getUnderlyingType()->isUnionType())
2702 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2703 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002704 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002705
2706 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002708 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002709 return;
2710 }
2711
John McCallf937c022011-10-07 06:10:15 +00002712 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002713 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002714 diag::warn_transparent_union_attribute_not_definition);
2715 return;
2716 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002717
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002718 RecordDecl::field_iterator Field = RD->field_begin(),
2719 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002720 if (Field == FieldEnd) {
2721 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2722 return;
2723 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002724
David Blaikie2d7c57e2012-04-30 02:36:29 +00002725 FieldDecl *FirstField = &*Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002726 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002727 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002728 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002729 diag::warn_transparent_union_attribute_floating)
2730 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 return;
2732 }
2733
2734 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2735 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2736 for (; Field != FieldEnd; ++Field) {
2737 QualType FieldType = Field->getType();
2738 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2739 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2740 // Warn if we drop the attribute.
2741 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002742 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002743 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002744 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002745 diag::warn_transparent_union_attribute_field_size_align)
2746 << isSize << Field->getDeclName() << FieldBits;
2747 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002748 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002749 diag::note_transparent_union_first_field_size_align)
2750 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002751 return;
2752 }
2753 }
2754
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002755 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002756}
2757
Chandler Carruthedc2c642011-07-02 00:01:44 +00002758static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002759 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002760 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002761 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002762
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002763 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002764 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002765
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002766 // Make sure that there is a string literal as the annotation's single
2767 // argument.
2768 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002769 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002770 return;
2771 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002772
2773 // Don't duplicate annotations that are already set.
2774 for (specific_attr_iterator<AnnotateAttr>
2775 i = D->specific_attr_begin<AnnotateAttr>(),
2776 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2777 if ((*i)->getAnnotation() == SE->getString())
2778 return;
2779 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002780 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002781 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782}
2783
Chandler Carruthedc2c642011-07-02 00:01:44 +00002784static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002785 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002786 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002788 return;
2789 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002790
2791 //FIXME: The C++0x version of this attribute has more limited applicabilty
2792 // than GNU's, and should error out when it is used to specify a
2793 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002794
Chris Lattner4a927cb2008-06-28 23:36:30 +00002795 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002796 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002797 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002798 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002799
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002800 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002801}
2802
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002803void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002804 // FIXME: Handle pack-expansions here.
2805 if (DiagnoseUnexpandedParameterPack(E))
2806 return;
2807
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002808 if (E->isTypeDependent() || E->isValueDependent()) {
2809 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002810 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002811 return;
2812 }
2813
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002814 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002815 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002816 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002817 ExprResult ICE
2818 = VerifyIntegerConstantExpression(E, &Alignment,
2819 diag::err_aligned_attribute_argument_not_int,
2820 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002821 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002822 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002823 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002824 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2825 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002826 return;
2827 }
2828
Richard Smithf4c51d92012-02-04 09:53:13 +00002829 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002830}
2831
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002832void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002833 // FIXME: Cache the number on the Attr object if non-dependent?
2834 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002835 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002836 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002837}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002838
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002839/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002840/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002841///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002842/// Despite what would be logical, the mode attribute is a decl attribute, not a
2843/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2844/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002845static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002846 // This attribute isn't documented, but glibc uses it. It changes
2847 // the width of an int or unsigned int to the specified size.
2848
2849 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002850 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002851 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002852
Chris Lattneracbc2d22008-06-27 22:18:37 +00002853
2854 IdentifierInfo *Name = Attr.getParameterName();
2855 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002856 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002857 return;
2858 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002859
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002860 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002861
2862 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002863 if (Str.startswith("__") && Str.endswith("__"))
2864 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002865
2866 unsigned DestWidth = 0;
2867 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002868 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002869 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002870 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002871 switch (Str[0]) {
2872 case 'Q': DestWidth = 8; break;
2873 case 'H': DestWidth = 16; break;
2874 case 'S': DestWidth = 32; break;
2875 case 'D': DestWidth = 64; break;
2876 case 'X': DestWidth = 96; break;
2877 case 'T': DestWidth = 128; break;
2878 }
2879 if (Str[1] == 'F') {
2880 IntegerMode = false;
2881 } else if (Str[1] == 'C') {
2882 IntegerMode = false;
2883 ComplexMode = true;
2884 } else if (Str[1] != 'I') {
2885 DestWidth = 0;
2886 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002887 break;
2888 case 4:
2889 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2890 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002891 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002892 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002893 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002894 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002895 break;
2896 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002897 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002898 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002899 break;
2900 }
2901
2902 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002903 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002904 OldTy = TD->getUnderlyingType();
2905 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2906 OldTy = VD->getType();
2907 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002908 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002909 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002910 return;
2911 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002912
John McCall9dd450b2009-09-21 23:43:11 +00002913 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002914 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2915 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002916 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002917 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2918 } else if (ComplexMode) {
2919 if (!OldTy->isComplexType())
2920 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2921 } else {
2922 if (!OldTy->isFloatingType())
2923 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2924 }
2925
Mike Stump87c57ac2009-05-16 07:39:55 +00002926 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2927 // and friends, at least with glibc.
2928 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2929 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002930 // FIXME: Make sure floating-point mappings are accurate
2931 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002932 QualType NewTy;
2933 switch (DestWidth) {
2934 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002935 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002936 return;
2937 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002938 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939 return;
2940 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002941 if (!IntegerMode) {
2942 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2943 return;
2944 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002945 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002946 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002947 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002948 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002949 break;
2950 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002951 if (!IntegerMode) {
2952 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2953 return;
2954 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002955 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002956 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002957 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002958 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002959 break;
2960 case 32:
2961 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002962 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002963 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002964 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002965 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002966 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002967 break;
2968 case 64:
2969 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002970 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002971 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00002972 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00002973 NewTy = S.Context.LongTy;
2974 else
2975 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002976 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00002977 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00002978 NewTy = S.Context.UnsignedLongTy;
2979 else
2980 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002981 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002982 case 96:
2983 NewTy = S.Context.LongDoubleTy;
2984 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002985 case 128:
2986 if (!IntegerMode) {
2987 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2988 return;
2989 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002990 if (OldTy->isSignedIntegerType())
2991 NewTy = S.Context.Int128Ty;
2992 else
2993 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002994 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002995 }
2996
Eli Friedman4735374e2009-03-03 06:41:03 +00002997 if (ComplexMode) {
2998 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002999 }
3000
3001 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003002 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003003 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003004 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003005 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003006 cast<ValueDecl>(D)->setType(NewTy);
3007}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003008
Chandler Carruthedc2c642011-07-02 00:01:44 +00003009static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003010 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003011 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003012 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003013
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003014 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003016 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00003017 return;
3018 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003019
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003020 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003021}
3022
Chandler Carruthedc2c642011-07-02 00:01:44 +00003023static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003024 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003025 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003026 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003027
Mike Stumpd3bb5572009-07-24 19:02:52 +00003028
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003029 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003030 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003031 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003032 return;
3033 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003034
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003035 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003036}
3037
Chandler Carruthedc2c642011-07-02 00:01:44 +00003038static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3039 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003040 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003041 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003042 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003043
Chris Lattner3c77a352010-06-22 00:03:40 +00003044
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003045 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003047 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003048 return;
3049 }
3050
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003051 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003052 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003053}
3054
Chandler Carruthedc2c642011-07-02 00:01:44 +00003055static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003056 if (S.LangOpts.CUDA) {
3057 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003058 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3060 return;
3061 }
3062
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003063 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003064 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003065 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003066 return;
3067 }
3068
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003069 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003070 } else {
3071 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3072 }
3073}
3074
Chandler Carruthedc2c642011-07-02 00:01:44 +00003075static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003076 if (S.LangOpts.CUDA) {
3077 // check the attribute arguments.
3078 if (Attr.getNumArgs() != 0) {
3079 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3080 return;
3081 }
3082
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003083 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003084 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003085 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003086 return;
3087 }
3088
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003089 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003090 } else {
3091 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3092 }
3093}
3094
Chandler Carruthedc2c642011-07-02 00:01:44 +00003095static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003096 if (S.LangOpts.CUDA) {
3097 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003098 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003099 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003100
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003101 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003102 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003103 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003104 return;
3105 }
3106
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003107 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003108 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003109 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003110 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3111 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3112 << FD->getType()
3113 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3114 "void");
3115 } else {
3116 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3117 << FD->getType();
3118 }
3119 return;
3120 }
3121
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003122 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003123 } else {
3124 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3125 }
3126}
3127
Chandler Carruthedc2c642011-07-02 00:01:44 +00003128static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003129 if (S.LangOpts.CUDA) {
3130 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003131 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003132 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003133
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003134
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003135 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003136 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003137 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003138 return;
3139 }
3140
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003141 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003142 } else {
3143 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3144 }
3145}
3146
Chandler Carruthedc2c642011-07-02 00:01:44 +00003147static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003148 if (S.LangOpts.CUDA) {
3149 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003150 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003151 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003152
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003153
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003154 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003155 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003156 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003157 return;
3158 }
3159
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003160 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003161 } else {
3162 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3163 }
3164}
3165
Chandler Carruthedc2c642011-07-02 00:01:44 +00003166static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003167 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003168 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003169 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003170
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003171 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003172 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003173 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003174 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003175 return;
3176 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003177
Douglas Gregor35b57532009-10-27 21:01:01 +00003178 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003179 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003180 return;
3181 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003182
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003183 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003184}
3185
Chandler Carruthedc2c642011-07-02 00:01:44 +00003186static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003187 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003188
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003189 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003190 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3191 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003192 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003193 return;
3194
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003195 if (!isa<ObjCMethodDecl>(D)) {
3196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3197 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003198 return;
3199 }
3200
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003201 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00003202 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003203 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003204 return;
3205 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003206 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003207 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00003208 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003209 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003210 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003211 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003212 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003213 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003214 case AttributeList::AT_pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003215 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003216 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003217 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003218 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003219 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003220 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003221 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003222 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003223 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003224 return;
3225 }
3226
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003227 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003228 PcsAttr::PCSType PCS;
3229 if (StrRef == "aapcs")
3230 PCS = PcsAttr::AAPCS;
3231 else if (StrRef == "aapcs-vfp")
3232 PCS = PcsAttr::AAPCS_VFP;
3233 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003234 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3235 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003236 return;
3237 }
3238
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003239 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003240 }
Abramo Bagnara50099372010-04-30 13:10:51 +00003241 default:
3242 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003243 }
3244}
3245
Chandler Carruthedc2c642011-07-02 00:01:44 +00003246static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003247 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003248 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003249}
3250
John McCall3882ace2011-01-05 12:14:39 +00003251bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3252 if (attr.isInvalid())
3253 return true;
3254
Ted Kremenek1551d552011-04-15 05:49:29 +00003255 if ((attr.getNumArgs() != 0 &&
3256 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3257 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00003258 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3259 attr.setInvalid();
3260 return true;
3261 }
3262
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003263 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3264 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003265 switch (attr.getKind()) {
3266 case AttributeList::AT_cdecl: CC = CC_C; break;
3267 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3268 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3269 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3270 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003271 case AttributeList::AT_pcs: {
3272 Expr *Arg = attr.getArg(0);
3273 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003274 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003275 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3276 << "pcs" << 1;
3277 attr.setInvalid();
3278 return true;
3279 }
3280
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003281 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003282 if (StrRef == "aapcs") {
3283 CC = CC_AAPCS;
3284 break;
3285 } else if (StrRef == "aapcs-vfp") {
3286 CC = CC_AAPCS_VFP;
3287 break;
3288 }
3289 // FALLS THROUGH
3290 }
David Blaikie8a40f702012-01-17 06:56:22 +00003291 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003292 }
3293
3294 return false;
3295}
3296
Chandler Carruthedc2c642011-07-02 00:01:44 +00003297static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003298 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003299
3300 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003301 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003302 return;
3303
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003304 if (!isa<ObjCMethodDecl>(D)) {
3305 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3306 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003307 return;
3308 }
Eli Friedman7044b762009-03-27 21:06:47 +00003309
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003310 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003311}
3312
3313/// Checks a regparm attribute, returning true if it is ill-formed and
3314/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003315bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3316 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003317 return true;
3318
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003319 if (Attr.getNumArgs() != 1) {
3320 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3321 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003322 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003323 }
Eli Friedman7044b762009-03-27 21:06:47 +00003324
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003325 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003326 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003327 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003328 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003329 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003330 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003331 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003332 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003333 }
3334
Douglas Gregore8bbc122011-09-02 00:18:52 +00003335 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003336 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003337 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003338 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003339 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003340 }
3341
John McCall3882ace2011-01-05 12:14:39 +00003342 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003343 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003344 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003345 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003346 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003347 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003348 }
3349
John McCall3882ace2011-01-05 12:14:39 +00003350 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003351}
3352
Chandler Carruthedc2c642011-07-02 00:01:44 +00003353static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003354 if (S.LangOpts.CUDA) {
3355 // check the attribute arguments.
3356 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003357 // FIXME: 0 is not okay.
3358 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003359 return;
3360 }
3361
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003362 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003364 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003365 return;
3366 }
3367
3368 Expr *MaxThreadsExpr = Attr.getArg(0);
3369 llvm::APSInt MaxThreads(32);
3370 if (MaxThreadsExpr->isTypeDependent() ||
3371 MaxThreadsExpr->isValueDependent() ||
3372 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3373 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3374 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3375 return;
3376 }
3377
3378 llvm::APSInt MinBlocks(32);
3379 if (Attr.getNumArgs() > 1) {
3380 Expr *MinBlocksExpr = Attr.getArg(1);
3381 if (MinBlocksExpr->isTypeDependent() ||
3382 MinBlocksExpr->isValueDependent() ||
3383 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3384 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3385 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3386 return;
3387 }
3388 }
3389
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003390 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003391 MaxThreads.getZExtValue(),
3392 MinBlocks.getZExtValue()));
3393 } else {
3394 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3395 }
3396}
3397
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003398//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003399// Checker-specific attribute handlers.
3400//===----------------------------------------------------------------------===//
3401
John McCalled433932011-01-25 03:31:58 +00003402static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003403 return type->isDependentType() ||
3404 type->isObjCObjectPointerType() ||
3405 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003406}
3407static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003408 return type->isDependentType() ||
3409 type->isPointerType() ||
3410 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003411}
3412
Chandler Carruthedc2c642011-07-02 00:01:44 +00003413static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003414 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003415 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003416 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003417 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003418 return;
3419 }
3420
3421 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003422 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00003423 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3424 cf = false;
3425 } else {
3426 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3427 cf = true;
3428 }
3429
3430 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003432 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003433 return;
3434 }
3435
3436 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003437 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003438 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003439 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003440}
3441
Chandler Carruthedc2c642011-07-02 00:01:44 +00003442static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3443 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003444 if (!isa<ObjCMethodDecl>(D)) {
3445 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003446 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003447 return;
3448 }
3449
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003450 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003451}
3452
Chandler Carruthedc2c642011-07-02 00:01:44 +00003453static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3454 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003455
John McCalled433932011-01-25 03:31:58 +00003456 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003457
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003458 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003459 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003460 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00003461 returnType = PD->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003462 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003463 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00003464 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003465 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003466 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003467 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003468 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003469 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003470 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003471 return;
3472 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003473
John McCalled433932011-01-25 03:31:58 +00003474 bool typeOK;
3475 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003476 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003477 default: llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003478 case AttributeList::AT_ns_returns_autoreleased:
3479 case AttributeList::AT_ns_returns_retained:
3480 case AttributeList::AT_ns_returns_not_retained:
3481 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3482 cf = false;
3483 break;
3484
3485 case AttributeList::AT_cf_returns_retained:
3486 case AttributeList::AT_cf_returns_not_retained:
3487 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3488 cf = true;
3489 break;
3490 }
3491
3492 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003493 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003494 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003495 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003496 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003497
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003498 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003499 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003500 llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003501 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003502 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003503 S.Context));
3504 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003505 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003506 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003507 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003508 return;
3509 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003510 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003511 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003512 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003513 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003514 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003515 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003516 return;
3517 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003518 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003519 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003520 return;
3521 };
3522}
3523
John McCallcf166702011-07-22 08:53:00 +00003524static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3525 const AttributeList &attr) {
3526 SourceLocation loc = attr.getLoc();
3527
3528 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3529
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003530 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003531 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003532 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003533 return;
3534 }
3535
3536 // Check that the method returns a normal pointer.
3537 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003538
3539 if (!resultType->isReferenceType() &&
3540 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003541 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3542 << SourceRange(loc)
3543 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3544
3545 // Drop the attribute.
3546 return;
3547 }
3548
3549 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003550 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003551}
3552
John McCall32f5fe12011-09-30 05:12:12 +00003553/// Handle cf_audited_transfer and cf_unknown_transfer.
3554static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3555 if (!isa<FunctionDecl>(D)) {
3556 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003557 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00003558 return;
3559 }
3560
3561 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3562
3563 // Check whether there's a conflicting attribute already present.
3564 Attr *Existing;
3565 if (IsAudited) {
3566 Existing = D->getAttr<CFUnknownTransferAttr>();
3567 } else {
3568 Existing = D->getAttr<CFAuditedTransferAttr>();
3569 }
3570 if (Existing) {
3571 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3572 << A.getName()
3573 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3574 << A.getRange() << Existing->getRange();
3575 return;
3576 }
3577
3578 // All clear; add the attribute.
3579 if (IsAudited) {
3580 D->addAttr(
3581 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3582 } else {
3583 D->addAttr(
3584 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3585 }
3586}
3587
John McCallf1e8b342011-09-29 07:17:38 +00003588static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3589 const AttributeList &Attr) {
3590 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3591 if (!RD || RD->isUnion()) {
3592 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003593 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003594 }
3595
3596 IdentifierInfo *ParmName = Attr.getParameterName();
3597
3598 // In Objective-C, verify that the type names an Objective-C type.
3599 // We don't want to check this outside of ObjC because people sometimes
3600 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003601 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003602 // Check for an existing type with this name.
3603 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3604 Sema::LookupOrdinaryName);
3605 if (S.LookupName(R, Sc)) {
3606 NamedDecl *Target = R.getFoundDecl();
3607 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3608 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3609 S.Diag(Target->getLocStart(), diag::note_declared_at);
3610 }
3611 }
3612 }
3613
3614 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3615 ParmName));
3616}
3617
Chandler Carruthedc2c642011-07-02 00:01:44 +00003618static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3619 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003620 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003621
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003622 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003623 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003624}
3625
Chandler Carruthedc2c642011-07-02 00:01:44 +00003626static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3627 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003628 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003629 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003630 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003631 return;
3632 }
3633
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003634 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003635 QualType type = vd->getType();
3636
3637 if (!type->isDependentType() &&
3638 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003639 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003640 << type;
3641 return;
3642 }
3643
3644 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3645
3646 // If we have no lifetime yet, check the lifetime we're presumably
3647 // going to infer.
3648 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3649 lifetime = type->getObjCARCImplicitLifetime();
3650
3651 switch (lifetime) {
3652 case Qualifiers::OCL_None:
3653 assert(type->isDependentType() &&
3654 "didn't infer lifetime for non-dependent type?");
3655 break;
3656
3657 case Qualifiers::OCL_Weak: // meaningful
3658 case Qualifiers::OCL_Strong: // meaningful
3659 break;
3660
3661 case Qualifiers::OCL_ExplicitNone:
3662 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003663 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003664 << (lifetime == Qualifiers::OCL_Autoreleasing);
3665 break;
3666 }
3667
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003668 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003669 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003670}
3671
Charles Davis163855f2010-02-16 18:27:26 +00003672static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman0c84ebb2012-02-23 22:46:33 +00003673 switch (Attr.getKind()) {
3674 default:
3675 return false;
3676 case AttributeList::AT_dllimport:
3677 case AttributeList::AT_dllexport:
3678 case AttributeList::AT_uuid:
3679 case AttributeList::AT_deprecated:
3680 case AttributeList::AT_noreturn:
3681 case AttributeList::AT_nothrow:
3682 case AttributeList::AT_naked:
3683 case AttributeList::AT_noinline:
3684 return true;
3685 }
Francois Picheta83957a2010-12-19 06:50:37 +00003686}
3687
3688//===----------------------------------------------------------------------===//
3689// Microsoft specific attribute handlers.
3690//===----------------------------------------------------------------------===//
3691
Chandler Carruthedc2c642011-07-02 00:01:44 +00003692static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00003693 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00003694 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003695 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003696 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003697
Francois Picheta83957a2010-12-19 06:50:37 +00003698 Expr *Arg = Attr.getArg(0);
3699 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003700 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003701 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3702 << "uuid" << 1;
3703 return;
3704 }
3705
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003706 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003707
3708 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3709 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003710
Francois Pichet7da11662010-12-20 01:41:49 +00003711 // Validate GUID length.
3712 if (IsCurly && StrRef.size() != 38) {
3713 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3714 return;
3715 }
3716 if (!IsCurly && StrRef.size() != 36) {
3717 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3718 return;
3719 }
3720
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003721 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00003722 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003723 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003724 if (IsCurly) // Skip the optional '{'
3725 ++I;
3726
3727 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003728 if (i == 8 || i == 13 || i == 18 || i == 23) {
3729 if (*I != '-') {
3730 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3731 return;
3732 }
3733 } else if (!isxdigit(*I)) {
3734 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3735 return;
3736 }
3737 I++;
3738 }
Francois Picheta83957a2010-12-19 06:50:37 +00003739
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003740 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003741 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003742 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003743 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003744}
3745
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003746//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003747// Top Level Sema Entry Points
3748//===----------------------------------------------------------------------===//
3749
Chandler Carruthedc2c642011-07-02 00:01:44 +00003750static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3751 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003752 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003753 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3754 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3755 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003756 default:
3757 break;
3758 }
3759}
Abramo Bagnara50099372010-04-30 13:10:51 +00003760
Chandler Carruthedc2c642011-07-02 00:01:44 +00003761static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3762 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003763 switch (Attr.getKind()) {
Michael Han4a045172012-03-07 00:12:16 +00003764 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3765 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3766 case AttributeList::AT_iboutletcollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003767 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003768 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003769 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003770 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003771 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003772 case AttributeList::AT_neon_vector_type:
3773 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003774 // Ignore these, these are type attributes, handled by
3775 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003776 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003777 case AttributeList::AT_device:
3778 case AttributeList::AT_host:
3779 case AttributeList::AT_overloadable:
3780 // Ignore, this is a non-inheritable attribute, handled
3781 // by ProcessNonInheritableDeclAttr.
3782 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003783 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3784 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003785 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003786 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003787 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003788 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3789 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3790 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003791 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003792 handleDependencyAttr (S, D, Attr); break;
3793 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3794 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3795 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3796 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3797 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003798 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003799 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003800 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003801 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3802 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3803 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3804 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003805 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003806 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003807 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003808 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3809 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3810 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3811 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3812 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003813 case AttributeList::AT_ownership_returns:
3814 case AttributeList::AT_ownership_takes:
3815 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003816 handleOwnershipAttr (S, D, Attr); break;
3817 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3818 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3819 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3820 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3821 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003822
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003823 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003824 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003825 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003826 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003827
John McCallcf166702011-07-22 08:53:00 +00003828 case AttributeList::AT_objc_returns_inner_pointer:
3829 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3830
John McCallf1e8b342011-09-29 07:17:38 +00003831 case AttributeList::AT_ns_bridged:
3832 handleNSBridgedAttr(S, scope, D, Attr); break;
3833
John McCall32f5fe12011-09-30 05:12:12 +00003834 case AttributeList::AT_cf_audited_transfer:
3835 case AttributeList::AT_cf_unknown_transfer:
3836 handleCFTransferAttr(S, D, Attr); break;
3837
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003838 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003839 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003840 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003841 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003842 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003843
3844 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003845 case AttributeList::AT_ns_returns_not_retained:
3846 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003847 case AttributeList::AT_ns_returns_retained:
3848 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003849 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003850
Michael Han4a045172012-03-07 00:12:16 +00003851 case AttributeList::AT_reqd_work_group_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003852 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003853
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003854 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003855 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003856
Chandler Carruthedc2c642011-07-02 00:01:44 +00003857 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003858 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003859 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3860 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003861 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003862 handleArcWeakrefUnavailableAttr (S, D, Attr);
3863 break;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00003864 case AttributeList::AT_objc_root_class:
3865 handleObjCRootClassAttr(S, D, Attr);
3866 break;
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00003867 case AttributeList::AT_objc_requires_property_definitions:
3868 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00003869 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003870 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindola70107f92011-10-03 14:59:42 +00003871 case AttributeList::AT_returns_twice:
3872 handleReturnsTwiceAttr(S, D, Attr);
3873 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003874 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3875 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3876 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003877 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003878 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3879 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3880 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003881 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003882 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003883 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003884 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003885 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003886 break;
John McCall86bc21f2011-03-02 11:33:24 +00003887 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003888 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003889 break;
Michael Han4a045172012-03-07 00:12:16 +00003890 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003891 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3892 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3893 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3894 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3895 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3896 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3897 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3898 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003899 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003900 // Just ignore
3901 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003902 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003903 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003904 break;
John McCallab26cfa2010-02-05 21:31:56 +00003905 case AttributeList::AT_stdcall:
3906 case AttributeList::AT_cdecl:
3907 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003908 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003909 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003910 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003911 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003912 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003913 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003914 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003915 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003916 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003917 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003918 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003919
3920 // Thread safety attributes:
3921 case AttributeList::AT_guarded_var:
3922 handleGuardedVarAttr(S, D, Attr);
3923 break;
3924 case AttributeList::AT_pt_guarded_var:
3925 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3926 break;
3927 case AttributeList::AT_scoped_lockable:
3928 handleLockableAttr(S, D, Attr, /*scoped = */true);
3929 break;
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00003930 case AttributeList::AT_no_address_safety_analysis:
3931 handleNoAddressSafetyAttr(S, D, Attr);
3932 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003933 case AttributeList::AT_no_thread_safety_analysis:
3934 handleNoThreadSafetyAttr(S, D, Attr);
3935 break;
3936 case AttributeList::AT_lockable:
3937 handleLockableAttr(S, D, Attr);
3938 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00003939 case AttributeList::AT_guarded_by:
3940 handleGuardedByAttr(S, D, Attr);
3941 break;
3942 case AttributeList::AT_pt_guarded_by:
3943 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3944 break;
3945 case AttributeList::AT_exclusive_lock_function:
3946 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3947 break;
3948 case AttributeList::AT_exclusive_locks_required:
3949 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3950 break;
3951 case AttributeList::AT_exclusive_trylock_function:
3952 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3953 break;
3954 case AttributeList::AT_lock_returned:
3955 handleLockReturnedAttr(S, D, Attr);
3956 break;
3957 case AttributeList::AT_locks_excluded:
3958 handleLocksExcludedAttr(S, D, Attr);
3959 break;
3960 case AttributeList::AT_shared_lock_function:
3961 handleLockFunAttr(S, D, Attr);
3962 break;
3963 case AttributeList::AT_shared_locks_required:
3964 handleLocksRequiredAttr(S, D, Attr);
3965 break;
3966 case AttributeList::AT_shared_trylock_function:
3967 handleTrylockFunAttr(S, D, Attr);
3968 break;
3969 case AttributeList::AT_unlock_function:
3970 handleUnlockFunAttr(S, D, Attr);
3971 break;
3972 case AttributeList::AT_acquired_before:
3973 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3974 break;
3975 case AttributeList::AT_acquired_after:
3976 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3977 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003978
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003979 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003980 // Ask target about the attribute.
3981 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3982 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003983 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3984 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003985 break;
3986 }
3987}
3988
Peter Collingbourneb331b262011-01-21 02:08:45 +00003989/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3990/// the attribute applies to decls. If the attribute is a type attribute, just
3991/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3992/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00003993static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3994 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003995 bool NonInheritable, bool Inheritable) {
3996 if (Attr.isInvalid())
3997 return;
3998
3999 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
4000 // FIXME: Try to deal with other __declspec attributes!
4001 return;
4002
4003 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004004 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004005
4006 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004007 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004008}
4009
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004010/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4011/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004012void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004013 const AttributeList *AttrList,
4014 bool NonInheritable, bool Inheritable) {
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004015 SmallVector<const AttributeList*, 4> attrs;
Rafael Espindolac18086a2010-02-23 22:00:30 +00004016 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004017 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004018 attrs.push_back(l);
4019 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004020
4021 // GCC accepts
4022 // static int a9 __attribute__((weakref));
4023 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004024 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004025 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004026 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004027 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004028 }
4029}
4030
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004031// Annotation attributes are the only attributes allowed after an access
4032// specifier.
4033bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4034 const AttributeList *AttrList) {
4035 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
4036 if (l->getKind() == AttributeList::AT_annotate) {
4037 handleAnnotateAttr(*this, ASDecl, *l);
4038 } else {
4039 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4040 return true;
4041 }
4042 }
4043
4044 return false;
4045}
4046
John McCall42856de2011-10-01 05:17:03 +00004047/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4048/// contains any decl attributes that we should warn about.
4049static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4050 for ( ; A; A = A->getNext()) {
4051 // Only warn if the attribute is an unignored, non-type attribute.
4052 if (A->isUsedAsTypeAttr()) continue;
4053 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4054
4055 if (A->getKind() == AttributeList::UnknownAttribute) {
4056 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4057 << A->getName() << A->getRange();
4058 } else {
4059 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4060 << A->getName() << A->getRange();
4061 }
4062 }
4063}
4064
4065/// checkUnusedDeclAttributes - Given a declarator which is not being
4066/// used to build a declaration, complain about any decl attributes
4067/// which might be lying around on it.
4068void Sema::checkUnusedDeclAttributes(Declarator &D) {
4069 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4070 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4071 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4072 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4073}
4074
Ryan Flynn7d470f32009-07-30 03:15:39 +00004075/// DeclClonePragmaWeak - clone existing decl (maybe definition),
4076/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedmance3e2c82011-09-07 04:05:06 +00004077NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4078 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004079 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004080 NamedDecl *NewD = 0;
4081 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004082 FunctionDecl *NewFD;
4083 // FIXME: Missing call to CheckFunctionDeclaration().
4084 // FIXME: Mangling?
4085 // FIXME: Is the qualifier info correct?
4086 // FIXME: Is the DeclContext correct?
4087 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4088 Loc, Loc, DeclarationName(II),
4089 FD->getType(), FD->getTypeSourceInfo(),
4090 SC_None, SC_None,
4091 false/*isInlineSpecified*/,
4092 FD->hasPrototype(),
4093 false/*isConstexprSpecified*/);
4094 NewD = NewFD;
4095
4096 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004097 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004098
4099 // Fake up parameter variables; they are declared as if this were
4100 // a typedef.
4101 QualType FDTy = FD->getType();
4102 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4103 SmallVector<ParmVarDecl*, 16> Params;
4104 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4105 AE = FT->arg_type_end(); AI != AE; ++AI) {
4106 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4107 Param->setScopeInfo(0, Params.size());
4108 Params.push_back(Param);
4109 }
David Blaikie9c70e042011-09-21 18:16:56 +00004110 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004111 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004112 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4113 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004114 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004115 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004116 VD->getStorageClass(),
4117 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004118 if (VD->getQualifier()) {
4119 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004120 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004121 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004122 }
4123 return NewD;
4124}
4125
4126/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
4127/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004128void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004129 if (W.getUsed()) return; // only do this once
4130 W.setUsed(true);
4131 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4132 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004133 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004134 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4135 NDId->getName()));
4136 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004137 WeakTopLevelDecl.push_back(NewD);
4138 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4139 // to insert Decl at TU scope, sorry.
4140 DeclContext *SavedContext = CurContext;
4141 CurContext = Context.getTranslationUnitDecl();
4142 PushOnScopeChains(NewD, S);
4143 CurContext = SavedContext;
4144 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004145 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004146 }
4147}
4148
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004149/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4150/// it, apply them to D. This is a bit tricky because PD can have attributes
4151/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004152void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4153 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004154 // It's valid to "forward-declare" #pragma weak, in which case we
4155 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004156 if (Inheritable) {
4157 LoadExternalWeakUndeclaredIdentifiers();
4158 if (!WeakUndeclaredIdentifiers.empty()) {
4159 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4160 if (IdentifierInfo *Id = ND->getIdentifier()) {
4161 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4162 = WeakUndeclaredIdentifiers.find(Id);
4163 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4164 WeakInfo W = I->second;
4165 DeclApplyPragmaWeak(S, ND, W);
4166 WeakUndeclaredIdentifiers[Id] = W;
4167 }
John McCall6fe02402010-10-27 00:59:00 +00004168 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004169 }
4170 }
4171 }
4172
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004173 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004174 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004175 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004176
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004177 // Walk the declarator structure, applying decl attributes that were in a type
4178 // position to the decl itself. This handles cases like:
4179 // int *__attr__(x)** D;
4180 // when X is a decl attribute.
4181 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4182 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004183 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004184
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004185 // Finally, apply any attributes on the decl itself.
4186 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004187 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004188}
John McCall28a6aea2009-11-04 02:18:39 +00004189
John McCall31168b02011-06-15 23:02:42 +00004190/// Is the given declaration allowed to use a forbidden type?
4191static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4192 // Private ivars are always okay. Unfortunately, people don't
4193 // always properly make their ivars private, even in system headers.
4194 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004195 // Function declarations in sys headers will be marked unavailable.
4196 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4197 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004198 return false;
4199
4200 // Require it to be declared in a system header.
4201 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4202}
4203
4204/// Handle a delayed forbidden-type diagnostic.
4205static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4206 Decl *decl) {
4207 if (decl && isForbiddenTypeAllowed(S, decl)) {
4208 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4209 "this system declaration uses an unsupported type"));
4210 return;
4211 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004212 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004213 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4214 // FIXME. we may want to supress diagnostics for all
4215 // kind of forbidden type messages on unavailable functions.
4216 if (FD->hasAttr<UnavailableAttr>() &&
4217 diag.getForbiddenTypeDiagnostic() ==
4218 diag::err_arc_array_param_no_ownership) {
4219 diag.Triggered = true;
4220 return;
4221 }
4222 }
John McCall31168b02011-06-15 23:02:42 +00004223
4224 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4225 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4226 diag.Triggered = true;
4227}
4228
John McCall2ec85372012-05-07 06:16:41 +00004229void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4230 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004231 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004232 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004233
John McCall2ec85372012-05-07 06:16:41 +00004234 // When delaying diagnostics to run in the context of a parsed
4235 // declaration, we only want to actually emit anything if parsing
4236 // succeeds.
4237 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004238
John McCall2ec85372012-05-07 06:16:41 +00004239 // We emit all the active diagnostics in this pool or any of its
4240 // parents. In general, we'll get one pool for the decl spec
4241 // and a child pool for each declarator; in a decl group like:
4242 // deprecated_typedef foo, *bar, baz();
4243 // only the declarator pops will be passed decls. This is correct;
4244 // we really do need to consider delayed diagnostics from the decl spec
4245 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004246 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004247 do {
John McCall6347b682012-05-07 06:16:58 +00004248 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004249 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4250 // This const_cast is a bit lame. Really, Triggered should be mutable.
4251 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004252 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004253 continue;
4254
John McCallc1465822011-02-14 07:13:47 +00004255 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004256 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004257 // Don't bother giving deprecation diagnostics if the decl is invalid.
4258 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004259 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004260 break;
4261
4262 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004263 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004264 break;
John McCall31168b02011-06-15 23:02:42 +00004265
4266 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004267 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004268 break;
John McCall86121512010-01-27 03:50:35 +00004269 }
4270 }
John McCall2ec85372012-05-07 06:16:41 +00004271 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004272}
4273
John McCall6347b682012-05-07 06:16:58 +00004274/// Given a set of delayed diagnostics, re-emit them as if they had
4275/// been delayed in the current context instead of in the given pool.
4276/// Essentially, this just moves them to the current pool.
4277void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4278 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4279 assert(curPool && "re-emitting in undelayed context not supported");
4280 curPool->steal(pool);
4281}
4282
John McCall28a6aea2009-11-04 02:18:39 +00004283static bool isDeclDeprecated(Decl *D) {
4284 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004285 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004286 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004287 // A category implicitly has the availability of the interface.
4288 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4289 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004290 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4291 return false;
4292}
4293
John McCallb45a1e72010-08-26 02:13:20 +00004294void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004295 Decl *Ctx) {
4296 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004297 return;
4298
John McCall86121512010-01-27 03:50:35 +00004299 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004300 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004301 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004302 << DD.getDeprecationDecl()->getDeclName()
4303 << DD.getDeprecationMessage();
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004304 else if (DD.getUnknownObjCClass()) {
4305 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4306 << DD.getDeprecationDecl()->getDeclName();
4307 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4308 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00004309 else
4310 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004311 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00004312}
4313
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004314void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004315 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004316 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00004317 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004318 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004319 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4320 UnknownObjCClass,
4321 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004322 return;
4323 }
4324
4325 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004326 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004327 return;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004328 if (!Message.empty()) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00004329 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4330 << Message;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004331 Diag(D->getLocation(),
4332 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4333 : diag::note_previous_decl) << D->getDeclName();
4334 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004335 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00004336 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004337 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004338 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004339 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004340 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4341 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004342 }
John McCall28a6aea2009-11-04 02:18:39 +00004343}