blob: 7c290f76eea0826accf8edc5c9ba22fbb15b78ee [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
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001297static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1298 // Check the attribute arguments.
1299 if (!checkAttributeNumArgs(S, Attr, 0))
1300 return;
1301
1302 if (!isa<FunctionDecl>(D)) {
1303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1304 << Attr.getName() << ExpectedFunction;
1305 return;
1306 }
1307
1308 if (D->hasAttr<HotAttr>()) {
1309 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1310 << Attr.getName() << "hot";
1311 return;
1312 }
1313
1314 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1315}
1316
1317static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1318 // Check the attribute arguments.
1319 if (!checkAttributeNumArgs(S, Attr, 0))
1320 return;
1321
1322 if (!isa<FunctionDecl>(D)) {
1323 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1324 << Attr.getName() << ExpectedFunction;
1325 return;
1326 }
1327
1328 if (D->hasAttr<ColdAttr>()) {
1329 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1330 << Attr.getName() << "cold";
1331 return;
1332 }
1333
1334 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1335}
1336
Chandler Carruthedc2c642011-07-02 00:01:44 +00001337static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001338 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001339 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001340 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001341
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001342 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001343 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001344 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001345 return;
1346 }
1347
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001348 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001349}
1350
Chandler Carruthedc2c642011-07-02 00:01:44 +00001351static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1352 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001353 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001354 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1356 return;
1357 }
1358
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001359 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001360 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001361 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001362 return;
1363 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001364
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001365 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001366}
1367
Chandler Carruthedc2c642011-07-02 00:01:44 +00001368static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001369 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001370 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001371 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1372 return;
1373 }
Mike Stump11289f42009-09-09 15:08:12 +00001374
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001375 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001376 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001377 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001378 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001379 return;
1380 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001381 }
1382
Ted Kremenek08479ae2009-08-15 00:51:46 +00001383 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001384}
1385
Chandler Carruthedc2c642011-07-02 00:01:44 +00001386static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001387 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001388 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001389 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001390
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001391 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001392}
1393
Chandler Carruthedc2c642011-07-02 00:01:44 +00001394static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001395 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001396 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001397 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001398 else
1399 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001400 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001401}
1402
Chandler Carruthedc2c642011-07-02 00:01:44 +00001403static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001404 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001405 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001406 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001407 else
1408 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001409 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001410}
1411
Chandler Carruthedc2c642011-07-02 00:01:44 +00001412static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001413 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001414
1415 if (S.CheckNoReturnAttr(attr)) return;
1416
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001417 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001418 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001419 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001420 return;
1421 }
1422
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001423 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001424}
1425
1426bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001427 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001428 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1429 attr.setInvalid();
1430 return true;
1431 }
1432
1433 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001434}
1435
Chandler Carruthedc2c642011-07-02 00:01:44 +00001436static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1437 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001438
1439 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1440 // because 'analyzer_noreturn' does not impact the type.
1441
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001442 if(!checkAttributeNumArgs(S, Attr, 0))
1443 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001444
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001445 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1446 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001447 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1448 && !VD->getType()->isFunctionPointerType())) {
1449 S.Diag(Attr.getLoc(),
1450 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1451 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001452 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001453 return;
1454 }
1455 }
1456
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001457 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001458}
1459
John Thompsoncdb847ba2010-08-09 21:53:52 +00001460// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001461static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001462/*
1463 Returning a Vector Class in Registers
1464
Eric Christopherbc638a82010-12-01 22:13:54 +00001465 According to the PPU ABI specifications, a class with a single member of
1466 vector type is returned in memory when used as the return value of a function.
1467 This results in inefficient code when implementing vector classes. To return
1468 the value in a single vector register, add the vecreturn attribute to the
1469 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001470
1471 Example:
1472
1473 struct Vector
1474 {
1475 __vector float xyzw;
1476 } __attribute__((vecreturn));
1477
1478 Vector Add(Vector lhs, Vector rhs)
1479 {
1480 Vector result;
1481 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1482 return result; // This will be returned in a register
1483 }
1484*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001485 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001487 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001488 return;
1489 }
1490
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001491 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001492 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1493 return;
1494 }
1495
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001496 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001497 int count = 0;
1498
1499 if (!isa<CXXRecordDecl>(record)) {
1500 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1501 return;
1502 }
1503
1504 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1505 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1506 return;
1507 }
1508
Eric Christopherbc638a82010-12-01 22:13:54 +00001509 for (RecordDecl::field_iterator iter = record->field_begin();
1510 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001511 if ((count == 1) || !iter->getType()->isVectorType()) {
1512 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1513 return;
1514 }
1515 count++;
1516 }
1517
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001518 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001519}
1520
Chandler Carruthedc2c642011-07-02 00:01:44 +00001521static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001522 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001524 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001525 return;
1526 }
1527 // FIXME: Actually store the attribute on the declaration
1528}
1529
Chandler Carruthedc2c642011-07-02 00:01:44 +00001530static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001531 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001532 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001534 return;
1535 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001536
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001537 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1538 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001539 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001540 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001541 return;
1542 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001543
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001544 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001545}
1546
Rafael Espindola70107f92011-10-03 14:59:42 +00001547static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1548 const AttributeList &Attr) {
1549 // check the attribute arguments.
1550 if (Attr.hasParameterOrArguments()) {
1551 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1552 return;
1553 }
1554
1555 if (!isa<FunctionDecl>(D)) {
1556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1557 << Attr.getName() << ExpectedFunction;
1558 return;
1559 }
1560
1561 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1562}
1563
Chandler Carruthedc2c642011-07-02 00:01:44 +00001564static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001565 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001566 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1568 return;
1569 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001570
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001571 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001572 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001573 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1574 return;
1575 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001576 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001578 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001579 return;
1580 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001581
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001582 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001583}
1584
Chandler Carruthedc2c642011-07-02 00:01:44 +00001585static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001586 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001587 if (Attr.getNumArgs() > 1) {
1588 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001589 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001590 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001591
1592 int priority = 65535; // FIXME: Do not hardcode such constants.
1593 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001594 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001595 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001596 if (E->isTypeDependent() || E->isValueDependent() ||
1597 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001598 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001599 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001600 return;
1601 }
1602 priority = Idx.getZExtValue();
1603 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001604
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001605 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001607 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001608 return;
1609 }
1610
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001611 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001612 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001613}
1614
Chandler Carruthedc2c642011-07-02 00:01:44 +00001615static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001616 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001617 if (Attr.getNumArgs() > 1) {
1618 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001619 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001620 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001621
1622 int priority = 65535; // FIXME: Do not hardcode such constants.
1623 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001624 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001625 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001626 if (E->isTypeDependent() || E->isValueDependent() ||
1627 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001628 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001629 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001630 return;
1631 }
1632 priority = Idx.getZExtValue();
1633 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001634
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001635 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001637 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001638 return;
1639 }
1640
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001641 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001642 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001643}
1644
Chandler Carruthedc2c642011-07-02 00:01:44 +00001645static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001646 unsigned NumArgs = Attr.getNumArgs();
1647 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001649 return;
1650 }
Chris Lattner190aa102011-02-24 05:42:24 +00001651
Fariborz Jahanian551063102010-10-06 21:18:44 +00001652 // Handle the case where deprecated attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001653 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001654 if (NumArgs == 1) {
1655 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001656 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001657 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1658 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001659 return;
1660 }
Chris Lattner190aa102011-02-24 05:42:24 +00001661 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001662 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001663
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001664 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001665}
1666
Chandler Carruthedc2c642011-07-02 00:01:44 +00001667static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001668 unsigned NumArgs = Attr.getNumArgs();
1669 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001670 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001671 return;
1672 }
Chris Lattner190aa102011-02-24 05:42:24 +00001673
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001674 // Handle the case where unavailable attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001675 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001676 if (NumArgs == 1) {
1677 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001678 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001679 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001680 diag::err_attribute_not_string) << "unavailable";
1681 return;
1682 }
Chris Lattner190aa102011-02-24 05:42:24 +00001683 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001684 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001685 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001686}
1687
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001688static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1689 const AttributeList &Attr) {
1690 unsigned NumArgs = Attr.getNumArgs();
1691 if (NumArgs > 0) {
1692 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1693 return;
1694 }
1695
1696 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001697 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001698}
1699
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001700static void handleObjCRootClassAttr(Sema &S, Decl *D,
1701 const AttributeList &Attr) {
1702 if (!isa<ObjCInterfaceDecl>(D)) {
1703 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1704 return;
1705 }
1706
1707 unsigned NumArgs = Attr.getNumArgs();
1708 if (NumArgs > 0) {
1709 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1710 return;
1711 }
1712
1713 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1714}
1715
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001716static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001717 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001718 if (!isa<ObjCInterfaceDecl>(D)) {
1719 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1720 return;
1721 }
1722
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001723 unsigned NumArgs = Attr.getNumArgs();
1724 if (NumArgs > 0) {
1725 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1726 return;
1727 }
1728
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001729 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001730 Attr.getRange(), S.Context));
1731}
1732
Jordy Rose740b0c22012-05-08 03:27:22 +00001733static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1734 IdentifierInfo *Platform,
1735 VersionTuple Introduced,
1736 VersionTuple Deprecated,
1737 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001738 StringRef PlatformName
1739 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1740 if (PlatformName.empty())
1741 PlatformName = Platform->getName();
1742
1743 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1744 // of these steps are needed).
1745 if (!Introduced.empty() && !Deprecated.empty() &&
1746 !(Introduced <= Deprecated)) {
1747 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1748 << 1 << PlatformName << Deprecated.getAsString()
1749 << 0 << Introduced.getAsString();
1750 return true;
1751 }
1752
1753 if (!Introduced.empty() && !Obsoleted.empty() &&
1754 !(Introduced <= Obsoleted)) {
1755 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1756 << 2 << PlatformName << Obsoleted.getAsString()
1757 << 0 << Introduced.getAsString();
1758 return true;
1759 }
1760
1761 if (!Deprecated.empty() && !Obsoleted.empty() &&
1762 !(Deprecated <= Obsoleted)) {
1763 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1764 << 2 << PlatformName << Obsoleted.getAsString()
1765 << 1 << Deprecated.getAsString();
1766 return true;
1767 }
1768
1769 return false;
1770}
1771
Rafael Espindolac67f2232012-05-10 02:50:16 +00001772bool Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1773 bool Inherited,
1774 IdentifierInfo *Platform,
1775 VersionTuple Introduced,
1776 VersionTuple Deprecated,
1777 VersionTuple Obsoleted,
1778 bool IsUnavailable,
1779 StringRef Message) {
1780 VersionTuple MergedIntroduced = Introduced;
1781 VersionTuple MergedDeprecated = Deprecated;
1782 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001783 bool FoundAny = false;
1784
Rafael Espindolac67f2232012-05-10 02:50:16 +00001785 if (D->hasAttrs()) {
1786 AttrVec &Attrs = D->getAttrs();
1787 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1788 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1789 if (!OldAA) {
1790 ++i;
1791 continue;
1792 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001793
Rafael Espindolac67f2232012-05-10 02:50:16 +00001794 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1795 if (OldPlatform != Platform) {
1796 ++i;
1797 continue;
1798 }
1799
1800 FoundAny = true;
1801 VersionTuple OldIntroduced = OldAA->getIntroduced();
1802 VersionTuple OldDeprecated = OldAA->getDeprecated();
1803 VersionTuple OldObsoleted = OldAA->getObsoleted();
1804 bool OldIsUnavailable = OldAA->getUnavailable();
1805 StringRef OldMessage = OldAA->getMessage();
1806
1807 if ((!OldIntroduced.empty() && !Introduced.empty() &&
1808 OldIntroduced != Introduced) ||
1809 (!OldDeprecated.empty() && !Deprecated.empty() &&
1810 OldDeprecated != Deprecated) ||
1811 (!OldObsoleted.empty() && !Obsoleted.empty() &&
1812 OldObsoleted != Obsoleted) ||
1813 (OldIsUnavailable != IsUnavailable) ||
1814 (OldMessage != Message)) {
1815 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1816 Diag(Range.getBegin(), diag::note_previous_attribute);
1817 Attrs.erase(Attrs.begin() + i);
1818 --e;
1819 continue;
1820 }
1821
1822 VersionTuple MergedIntroduced2 = MergedIntroduced;
1823 VersionTuple MergedDeprecated2 = MergedDeprecated;
1824 VersionTuple MergedObsoleted2 = MergedObsoleted;
1825
1826 if (MergedIntroduced2.empty())
1827 MergedIntroduced2 = OldIntroduced;
1828 if (MergedDeprecated2.empty())
1829 MergedDeprecated2 = OldDeprecated;
1830 if (MergedObsoleted2.empty())
1831 MergedObsoleted2 = OldObsoleted;
1832
1833 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1834 MergedIntroduced2, MergedDeprecated2,
1835 MergedObsoleted2)) {
1836 Attrs.erase(Attrs.begin() + i);
1837 --e;
1838 continue;
1839 }
1840
1841 MergedIntroduced = MergedIntroduced2;
1842 MergedDeprecated = MergedDeprecated2;
1843 MergedObsoleted = MergedObsoleted2;
1844 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001845 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001846 }
1847
1848 if (FoundAny &&
1849 MergedIntroduced == Introduced &&
1850 MergedDeprecated == Deprecated &&
1851 MergedObsoleted == Obsoleted)
Rafael Espindolac67f2232012-05-10 02:50:16 +00001852 return false;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001853
Rafael Espindolac67f2232012-05-10 02:50:16 +00001854 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001855 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001856 AvailabilityAttr *Attr =
1857 ::new (Context) AvailabilityAttr(Range, Context, Platform,
1858 Introduced, Deprecated,
1859 Obsoleted, IsUnavailable, Message);
1860
1861 if (Inherited)
1862 Attr->setInherited(true);
1863 D->addAttr(Attr);
1864 return true;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001865 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00001866 return false;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001867}
1868
Chandler Carruthedc2c642011-07-02 00:01:44 +00001869static void handleAvailabilityAttr(Sema &S, Decl *D,
1870 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001871 IdentifierInfo *Platform = Attr.getParameterName();
1872 SourceLocation PlatformLoc = Attr.getParameterLoc();
1873
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001874 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001875 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1876 << Platform;
1877
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001878 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1879 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1880 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001881 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001882 StringRef Str;
1883 const StringLiteral *SE =
1884 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1885 if (SE)
1886 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001887
Rafael Espindolac67f2232012-05-10 02:50:16 +00001888 S.mergeAvailabilityAttr(D, Attr.getRange(),
1889 false, Platform,
1890 Introduced.Version,
1891 Deprecated.Version,
1892 Obsoleted.Version,
1893 IsUnavailable,
1894 Str);
1895}
1896
1897bool Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
1898 bool Inherited,
1899 VisibilityAttr::VisibilityType Vis) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00001900 if (isa<TypedefNameDecl>(D)) {
1901 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
1902 return false;
1903 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00001904 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
1905 if (ExistingAttr) {
1906 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
1907 if (ExistingVis == Vis)
1908 return false;
1909 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
1910 Diag(Range.getBegin(), diag::note_previous_attribute);
1911 D->dropAttr<VisibilityAttr>();
1912 }
1913 VisibilityAttr *Attr = ::new (Context) VisibilityAttr(Range, Context, Vis);
1914 if (Inherited)
1915 Attr->setInherited(true);
1916 D->addAttr(Attr);
1917 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001918}
1919
Chandler Carruthedc2c642011-07-02 00:01:44 +00001920static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001921 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001922 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001923 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001924
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001925 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001926 Arg = Arg->IgnoreParenCasts();
1927 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001928
Douglas Gregorfb65e592011-07-27 05:40:30 +00001929 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001930 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001931 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001932 return;
1933 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001934
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001935 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001936 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001937
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001938 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001939 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001940 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001941 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001942 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001943 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00001944 else if (TypeStr == "protected") {
1945 // Complain about attempts to use protected visibility on targets
1946 // (like Darwin) that don't support it.
1947 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1948 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1949 type = VisibilityAttr::Default;
1950 } else {
1951 type = VisibilityAttr::Protected;
1952 }
1953 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001954 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001955 return;
1956 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001957
Rafael Espindolac67f2232012-05-10 02:50:16 +00001958 S.mergeVisibilityAttr(D, Attr.getRange(), false, type);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001959}
1960
Chandler Carruthedc2c642011-07-02 00:01:44 +00001961static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1962 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001963 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1964 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001966 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001967 return;
1968 }
1969
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001970 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1971 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1972 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001973 << "objc_method_family" << 1;
1974 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001976 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001977 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001978 return;
1979 }
1980
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001981 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001982 ObjCMethodFamilyAttr::FamilyKind family;
1983 if (param == "none")
1984 family = ObjCMethodFamilyAttr::OMF_None;
1985 else if (param == "alloc")
1986 family = ObjCMethodFamilyAttr::OMF_alloc;
1987 else if (param == "copy")
1988 family = ObjCMethodFamilyAttr::OMF_copy;
1989 else if (param == "init")
1990 family = ObjCMethodFamilyAttr::OMF_init;
1991 else if (param == "mutableCopy")
1992 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1993 else if (param == "new")
1994 family = ObjCMethodFamilyAttr::OMF_new;
1995 else {
1996 // Just warn and ignore it. This is future-proof against new
1997 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001998 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001999 return;
2000 }
2001
John McCall31168b02011-06-15 23:02:42 +00002002 if (family == ObjCMethodFamilyAttr::OMF_init &&
2003 !method->getResultType()->isObjCObjectPointerType()) {
2004 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2005 << method->getResultType();
2006 // Ignore the attribute.
2007 return;
2008 }
2009
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002010 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002011 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002012}
2013
Chandler Carruthedc2c642011-07-02 00:01:44 +00002014static void handleObjCExceptionAttr(Sema &S, Decl *D,
2015 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002016 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002017 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002018
Chris Lattner677a3582009-02-14 08:09:34 +00002019 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2020 if (OCI == 0) {
2021 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2022 return;
2023 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002024
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002025 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00002026}
2027
Chandler Carruthedc2c642011-07-02 00:01:44 +00002028static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002029 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002030 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002031 return;
2032 }
Richard Smithdda56e42011-04-15 14:24:37 +00002033 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002034 QualType T = TD->getUnderlyingType();
2035 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002036 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002037 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2038 return;
2039 }
2040 }
Ted Kremenek05e916b2012-03-01 01:40:32 +00002041 else if (!isa<ObjCPropertyDecl>(D)) {
2042 // It is okay to include this attribute on properties, e.g.:
2043 //
2044 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2045 //
2046 // In this case it follows tradition and suppresses an error in the above
2047 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002048 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002049 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002050 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002051}
2052
Mike Stumpd3bb5572009-07-24 19:02:52 +00002053static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002054handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002055 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002056 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002057 return;
2058 }
2059
2060 if (!isa<FunctionDecl>(D)) {
2061 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2062 return;
2063 }
2064
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002065 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002066}
2067
Chandler Carruthedc2c642011-07-02 00:01:44 +00002068static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002069 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002070 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002071 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002072 return;
2073 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002074
Steve Naroff3405a732008-09-18 16:44:58 +00002075 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002076 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002077 return;
2078 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002079
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002080 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002081 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002082 type = BlocksAttr::ByRef;
2083 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002084 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002085 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002086 return;
2087 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002088
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002089 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002090}
2091
Chandler Carruthedc2c642011-07-02 00:01:44 +00002092static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002093 // check the attribute arguments.
2094 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002095 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002096 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002097 }
2098
John McCallb46f2872011-09-09 07:56:05 +00002099 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002100 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002101 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002102 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002103 if (E->isTypeDependent() || E->isValueDependent() ||
2104 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002106 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002107 return;
2108 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002109
John McCallb46f2872011-09-09 07:56:05 +00002110 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002111 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2112 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002113 return;
2114 }
John McCallb46f2872011-09-09 07:56:05 +00002115
2116 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002117 }
2118
John McCallb46f2872011-09-09 07:56:05 +00002119 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002120 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002121 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002122 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002123 if (E->isTypeDependent() || E->isValueDependent() ||
2124 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002125 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002126 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002127 return;
2128 }
2129 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002130
John McCallb46f2872011-09-09 07:56:05 +00002131 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 // FIXME: This error message could be improved, it would be nice
2133 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002134 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2135 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 return;
2137 }
2138 }
2139
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002140 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002141 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002142 if (isa<FunctionNoProtoType>(FT)) {
2143 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2144 return;
2145 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002146
Chris Lattner9363e312009-03-17 23:03:47 +00002147 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002148 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002149 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002150 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002151 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002152 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002153 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002154 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002155 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002156 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2157 if (!BD->isVariadic()) {
2158 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2159 return;
2160 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002161 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002162 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002163 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002164 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002165 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002166 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002167 int m = Ty->isFunctionPointerType() ? 0 : 1;
2168 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002169 return;
2170 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002171 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002173 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002174 return;
2175 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002176 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002178 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002179 return;
2180 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002181 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002182 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002183}
2184
Chandler Carruthedc2c642011-07-02 00:01:44 +00002185static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002186 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002187 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002188 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002189
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002190 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002192 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002193 return;
2194 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002195
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002196 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2197 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2198 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002199 return;
2200 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002201 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2202 if (MD->getResultType()->isVoidType()) {
2203 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2204 << Attr.getName() << 1;
2205 return;
2206 }
2207
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002208 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002209}
2210
Chandler Carruthedc2c642011-07-02 00:01:44 +00002211static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002212 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002213 if (Attr.hasParameterOrArguments()) {
2214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002215 return;
2216 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002217
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002218 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002219 if (isa<CXXRecordDecl>(D)) {
2220 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2221 return;
2222 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002223 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2224 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002225 return;
2226 }
2227
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002228 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002229
2230 // 'weak' only applies to declarations with external linkage.
2231 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002232 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002233 return;
2234 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002235
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002236 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002237}
2238
Chandler Carruthedc2c642011-07-02 00:01:44 +00002239static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002240 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002241 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002242 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002243
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002244
2245 // weak_import only applies to variable & function declarations.
2246 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002247 if (!D->canBeWeakImported(isDef)) {
2248 if (isDef)
2249 S.Diag(Attr.getLoc(),
2250 diag::warn_attribute_weak_import_invalid_on_definition)
2251 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002252 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002253 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002254 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002255 // Nothing to warn about here.
2256 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002258 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002259
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002260 return;
2261 }
2262
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002263 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002264}
2265
Chandler Carruthedc2c642011-07-02 00:01:44 +00002266static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2267 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002268 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002269 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00002270 return;
Nate Begemanf2758702009-06-26 06:32:41 +00002271
2272 unsigned WGSize[3];
2273 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002274 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002275 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002276 if (E->isTypeDependent() || E->isValueDependent() ||
2277 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2279 << "reqd_work_group_size" << E->getSourceRange();
2280 return;
2281 }
2282 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2283 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002284 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002285 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00002286 WGSize[2]));
2287}
2288
Chandler Carruthedc2c642011-07-02 00:01:44 +00002289static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002290 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002291 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002292 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002293
2294 // Make sure that there is a string literal as the sections's single
2295 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002296 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002297 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002298 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002299 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002300 return;
2301 }
Mike Stump11289f42009-09-09 15:08:12 +00002302
Chris Lattner30ba6742009-08-10 19:03:04 +00002303 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002304 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002305 if (!Error.empty()) {
2306 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2307 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002308 return;
2309 }
Mike Stump11289f42009-09-09 15:08:12 +00002310
Chris Lattner20aee9b2010-01-12 20:58:53 +00002311 // This attribute cannot be applied to local variables.
2312 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2313 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2314 return;
2315 }
2316
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002317 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002318 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00002319}
2320
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002321
Chandler Carruthedc2c642011-07-02 00:01:44 +00002322static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002323 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002324 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002326 return;
2327 }
Douglas Gregor88336832011-06-15 05:45:11 +00002328
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002329 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002330 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002331 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002332 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002333 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002334 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002335}
2336
Chandler Carruthedc2c642011-07-02 00:01:44 +00002337static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002338 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002339 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002341 return;
2342 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002344 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002345 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002346 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002347 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002348 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002349 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002350}
2351
Chandler Carruthedc2c642011-07-02 00:01:44 +00002352static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002353 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002354 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002355 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002356
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002357 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002358}
2359
Chandler Carruthedc2c642011-07-02 00:01:44 +00002360static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002361 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2363 return;
2364 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002365
Anders Carlssond277d792009-01-31 01:16:18 +00002366 if (Attr.getNumArgs() != 0) {
2367 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2368 return;
2369 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002370
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002371 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002372
Anders Carlssond277d792009-01-31 01:16:18 +00002373 if (!VD || !VD->hasLocalStorage()) {
2374 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2375 return;
2376 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002377
Anders Carlssond277d792009-01-31 01:16:18 +00002378 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002379 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002380 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002381 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2382 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002383 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002384 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002385 Attr.getParameterName();
2386 return;
2387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
Anders Carlssond277d792009-01-31 01:16:18 +00002389 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2390 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002391 S.Diag(Attr.getParameterLoc(),
2392 diag::err_attribute_cleanup_arg_not_function)
2393 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002394 return;
2395 }
2396
Anders Carlssond277d792009-01-31 01:16:18 +00002397 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002398 S.Diag(Attr.getParameterLoc(),
2399 diag::err_attribute_cleanup_func_must_take_one_arg)
2400 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002401 return;
2402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403
Anders Carlsson723f55d2009-02-07 23:16:50 +00002404 // We're currently more strict than GCC about what function types we accept.
2405 // If this ever proves to be a problem it should be easy to fix.
2406 QualType Ty = S.Context.getPointerType(VD->getType());
2407 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002408 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2409 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002410 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002411 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2412 Attr.getParameterName() << ParamTy << Ty;
2413 return;
2414 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002415
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002416 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002417 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002418}
2419
Mike Stumpd3bb5572009-07-24 19:02:52 +00002420/// Handle __attribute__((format_arg((idx)))) attribute based on
2421/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002422static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002423 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002424 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002425
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002426 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002428 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002429 return;
2430 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002431
2432 // In C++ the implicit 'this' function parameter also counts, and they are
2433 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002434 bool HasImplicitThisParam = isInstanceMethod(D);
2435 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002436 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002437
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002438 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002439 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002440 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002441 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2442 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002443 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2444 << "format" << 2 << IdxExpr->getSourceRange();
2445 return;
2446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002447
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002448 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2449 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2450 << "format" << 2 << IdxExpr->getSourceRange();
2451 return;
2452 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002453
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002454 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002455
Chandler Carruth743682b2010-11-16 08:35:43 +00002456 if (HasImplicitThisParam) {
2457 if (ArgIdx == 0) {
2458 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2459 << "format_arg" << IdxExpr->getSourceRange();
2460 return;
2461 }
2462 ArgIdx--;
2463 }
2464
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002465 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002466 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002467
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002468 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2469 if (not_nsstring_type &&
2470 !isCFStringType(Ty, S.Context) &&
2471 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002472 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002473 // FIXME: Should highlight the actual expression that has the wrong type.
2474 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002475 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002476 << IdxExpr->getSourceRange();
2477 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002478 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002479 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002480 if (!isNSStringType(Ty, S.Context) &&
2481 !isCFStringType(Ty, S.Context) &&
2482 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002483 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002484 // FIXME: Should highlight the actual expression that has the wrong type.
2485 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002486 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002487 << IdxExpr->getSourceRange();
2488 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002489 }
2490
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002491 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002492 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002493}
2494
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002495enum FormatAttrKind {
2496 CFStringFormat,
2497 NSStringFormat,
2498 StrftimeFormat,
2499 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002500 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002501 InvalidFormat
2502};
2503
2504/// getFormatAttrKind - Map from format attribute names to supported format
2505/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002506static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002507 // Check for formats that get handled specially.
2508 if (Format == "NSString")
2509 return NSStringFormat;
2510 if (Format == "CFString")
2511 return CFStringFormat;
2512 if (Format == "strftime")
2513 return StrftimeFormat;
2514
2515 // Otherwise, check for supported formats.
2516 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupasfc0da1a2012-01-27 09:14:17 +00002517 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00002518 Format == "zcmn_err" ||
2519 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002520 return SupportedFormat;
2521
Duncan Sandsde4fe352010-03-23 14:44:19 +00002522 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2523 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00002524 return IgnoredFormat;
2525
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002526 return InvalidFormat;
2527}
2528
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002529/// Handle __attribute__((init_priority(priority))) attributes based on
2530/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002531static void handleInitPriorityAttr(Sema &S, Decl *D,
2532 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002533 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002534 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2535 return;
2536 }
2537
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002538 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002539 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2540 Attr.setInvalid();
2541 return;
2542 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002543 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002544 if (S.Context.getAsArrayType(T))
2545 T = S.Context.getBaseElementType(T);
2546 if (!T->getAs<RecordType>()) {
2547 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2548 Attr.setInvalid();
2549 return;
2550 }
2551
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002552 if (Attr.getNumArgs() != 1) {
2553 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2554 Attr.setInvalid();
2555 return;
2556 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002557 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002558
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002559 llvm::APSInt priority(32);
2560 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2561 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2563 << "init_priority" << priorityExpr->getSourceRange();
2564 Attr.setInvalid();
2565 return;
2566 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002567 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002568 if (prioritynum < 101 || prioritynum > 65535) {
2569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2570 << priorityExpr->getSourceRange();
2571 Attr.setInvalid();
2572 return;
2573 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002574 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002575 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002576}
2577
Rafael Espindola92d49452012-05-11 00:36:07 +00002578bool Sema::mergeFormatAttr(Decl *D, SourceRange Range, bool Inherited,
2579 StringRef Format, int FormatIdx, int FirstArg) {
2580 // Check whether we already have an equivalent format attribute.
2581 for (specific_attr_iterator<FormatAttr>
2582 i = D->specific_attr_begin<FormatAttr>(),
2583 e = D->specific_attr_end<FormatAttr>();
2584 i != e ; ++i) {
2585 FormatAttr *f = *i;
2586 if (f->getType() == Format &&
2587 f->getFormatIdx() == FormatIdx &&
2588 f->getFirstArg() == FirstArg) {
2589 // If we don't have a valid location for this attribute, adopt the
2590 // location.
2591 if (f->getLocation().isInvalid())
2592 f->setRange(Range);
2593 return false;
2594 }
2595 }
2596
2597 FormatAttr *Attr = ::new (Context) FormatAttr(Range, Context, Format,
2598 FormatIdx, FirstArg);
2599 D->addAttr(Attr);
2600 return true;
2601}
2602
Mike Stumpd3bb5572009-07-24 19:02:52 +00002603/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2604/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002605static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606
Chris Lattner4a927cb2008-06-28 23:36:30 +00002607 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002608 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002609 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610 return;
2611 }
2612
Chris Lattner4a927cb2008-06-28 23:36:30 +00002613 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002615 return;
2616 }
2617
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002618 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002620 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621 return;
2622 }
2623
Chandler Carruth743682b2010-11-16 08:35:43 +00002624 // In C++ the implicit 'this' function parameter also counts, and they are
2625 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002626 bool HasImplicitThisParam = isInstanceMethod(D);
2627 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 unsigned FirstIdx = 1;
2629
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002630 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631
2632 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002633 if (Format.startswith("__") && Format.endswith("__"))
2634 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002636 // Check for supported formats.
2637 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002638
2639 if (Kind == IgnoredFormat)
2640 return;
2641
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002642 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002643 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002644 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002645 return;
2646 }
2647
2648 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002649 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002650 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002651 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2652 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002653 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002654 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002655 return;
2656 }
2657
2658 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002659 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002660 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002661 return;
2662 }
2663
2664 // FIXME: Do we need to bounds check?
2665 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002666
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002667 if (HasImplicitThisParam) {
2668 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002669 S.Diag(Attr.getLoc(),
2670 diag::err_format_attribute_implicit_this_format_string)
2671 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002672 return;
2673 }
2674 ArgIdx--;
2675 }
Mike Stump11289f42009-09-09 15:08:12 +00002676
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002677 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002678 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002679
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002680 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002681 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002682 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2683 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002684 return;
2685 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002686 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002687 // FIXME: do we need to check if the type is NSString*? What are the
2688 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002689 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002690 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002691 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2692 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002693 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002694 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002695 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002696 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002697 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002698 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2699 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002700 return;
2701 }
2702
2703 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002704 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002705 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002706 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2707 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002708 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002709 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002710 return;
2711 }
2712
2713 // check if the function is variadic if the 3rd argument non-zero
2714 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002715 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716 ++NumArgs; // +1 for ...
2717 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002718 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002719 return;
2720 }
2721 }
2722
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002723 // strftime requires FirstArg to be 0 because it doesn't read from any
2724 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002725 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002726 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002727 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2728 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002729 return;
2730 }
2731 // if 0 it disables parameter checking (to use with e.g. va_list)
2732 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002733 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002734 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002735 return;
2736 }
2737
Rafael Espindola92d49452012-05-11 00:36:07 +00002738 S.mergeFormatAttr(D, Attr.getRange(), false, Format, Idx.getZExtValue(),
2739 FirstArg.getZExtValue());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740}
2741
Chandler Carruthedc2c642011-07-02 00:01:44 +00002742static void handleTransparentUnionAttr(Sema &S, Decl *D,
2743 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002744 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002745 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002746 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002747
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002748
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002749 // Try to find the underlying union declaration.
2750 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002751 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002752 if (TD && TD->getUnderlyingType()->isUnionType())
2753 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2754 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002755 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002756
2757 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002759 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002760 return;
2761 }
2762
John McCallf937c022011-10-07 06:10:15 +00002763 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002764 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002765 diag::warn_transparent_union_attribute_not_definition);
2766 return;
2767 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002768
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002769 RecordDecl::field_iterator Field = RD->field_begin(),
2770 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002771 if (Field == FieldEnd) {
2772 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2773 return;
2774 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002775
David Blaikie2d7c57e2012-04-30 02:36:29 +00002776 FieldDecl *FirstField = &*Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002777 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002778 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002779 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002780 diag::warn_transparent_union_attribute_floating)
2781 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002782 return;
2783 }
2784
2785 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2786 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2787 for (; Field != FieldEnd; ++Field) {
2788 QualType FieldType = Field->getType();
2789 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2790 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2791 // Warn if we drop the attribute.
2792 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002793 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002794 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002795 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002796 diag::warn_transparent_union_attribute_field_size_align)
2797 << isSize << Field->getDeclName() << FieldBits;
2798 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002799 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002800 diag::note_transparent_union_first_field_size_align)
2801 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002802 return;
2803 }
2804 }
2805
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002806 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002807}
2808
Chandler Carruthedc2c642011-07-02 00:01:44 +00002809static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002810 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002811 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002812 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002813
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002814 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002815 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002816
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002817 // Make sure that there is a string literal as the annotation's single
2818 // argument.
2819 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002820 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002821 return;
2822 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002823
2824 // Don't duplicate annotations that are already set.
2825 for (specific_attr_iterator<AnnotateAttr>
2826 i = D->specific_attr_begin<AnnotateAttr>(),
2827 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2828 if ((*i)->getAnnotation() == SE->getString())
2829 return;
2830 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002831 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002832 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833}
2834
Chandler Carruthedc2c642011-07-02 00:01:44 +00002835static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002836 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002837 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002838 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002839 return;
2840 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002841
2842 //FIXME: The C++0x version of this attribute has more limited applicabilty
2843 // than GNU's, and should error out when it is used to specify a
2844 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002845
Chris Lattner4a927cb2008-06-28 23:36:30 +00002846 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002847 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002848 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002849 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002850
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002851 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002852}
2853
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002854void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002855 // FIXME: Handle pack-expansions here.
2856 if (DiagnoseUnexpandedParameterPack(E))
2857 return;
2858
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002859 if (E->isTypeDependent() || E->isValueDependent()) {
2860 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002861 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002862 return;
2863 }
2864
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002865 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002866 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002867 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002868 ExprResult ICE
2869 = VerifyIntegerConstantExpression(E, &Alignment,
2870 diag::err_aligned_attribute_argument_not_int,
2871 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002872 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002873 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002874 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002875 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2876 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002877 return;
2878 }
2879
Richard Smithf4c51d92012-02-04 09:53:13 +00002880 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002881}
2882
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002883void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002884 // FIXME: Cache the number on the Attr object if non-dependent?
2885 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002886 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002887 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002888}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002889
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002890/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002891/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002892///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002893/// Despite what would be logical, the mode attribute is a decl attribute, not a
2894/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2895/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002896static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002897 // This attribute isn't documented, but glibc uses it. It changes
2898 // the width of an int or unsigned int to the specified size.
2899
2900 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002901 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002902 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002903
Chris Lattneracbc2d22008-06-27 22:18:37 +00002904
2905 IdentifierInfo *Name = Attr.getParameterName();
2906 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002907 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002908 return;
2909 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002910
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002911 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002912
2913 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002914 if (Str.startswith("__") && Str.endswith("__"))
2915 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002916
2917 unsigned DestWidth = 0;
2918 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002919 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002920 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002921 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002922 switch (Str[0]) {
2923 case 'Q': DestWidth = 8; break;
2924 case 'H': DestWidth = 16; break;
2925 case 'S': DestWidth = 32; break;
2926 case 'D': DestWidth = 64; break;
2927 case 'X': DestWidth = 96; break;
2928 case 'T': DestWidth = 128; break;
2929 }
2930 if (Str[1] == 'F') {
2931 IntegerMode = false;
2932 } else if (Str[1] == 'C') {
2933 IntegerMode = false;
2934 ComplexMode = true;
2935 } else if (Str[1] != 'I') {
2936 DestWidth = 0;
2937 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002938 break;
2939 case 4:
2940 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2941 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002942 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002943 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002944 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002945 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002946 break;
2947 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002948 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002949 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002950 break;
2951 }
2952
2953 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002954 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002955 OldTy = TD->getUnderlyingType();
2956 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2957 OldTy = VD->getType();
2958 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002959 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002960 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002961 return;
2962 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002963
John McCall9dd450b2009-09-21 23:43:11 +00002964 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002965 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2966 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002967 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002968 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2969 } else if (ComplexMode) {
2970 if (!OldTy->isComplexType())
2971 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2972 } else {
2973 if (!OldTy->isFloatingType())
2974 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2975 }
2976
Mike Stump87c57ac2009-05-16 07:39:55 +00002977 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2978 // and friends, at least with glibc.
2979 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2980 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002981 // FIXME: Make sure floating-point mappings are accurate
2982 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002983 QualType NewTy;
2984 switch (DestWidth) {
2985 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002986 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002987 return;
2988 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002989 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002990 return;
2991 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002992 if (!IntegerMode) {
2993 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2994 return;
2995 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002996 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002997 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002998 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002999 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003000 break;
3001 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003002 if (!IntegerMode) {
3003 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3004 return;
3005 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003006 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003007 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003008 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003009 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003010 break;
3011 case 32:
3012 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003013 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003014 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003015 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003016 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003017 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003018 break;
3019 case 64:
3020 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003021 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003022 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003023 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003024 NewTy = S.Context.LongTy;
3025 else
3026 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003027 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003028 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003029 NewTy = S.Context.UnsignedLongTy;
3030 else
3031 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003032 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003033 case 96:
3034 NewTy = S.Context.LongDoubleTy;
3035 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003036 case 128:
3037 if (!IntegerMode) {
3038 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3039 return;
3040 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003041 if (OldTy->isSignedIntegerType())
3042 NewTy = S.Context.Int128Ty;
3043 else
3044 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003045 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003046 }
3047
Eli Friedman4735374e2009-03-03 06:41:03 +00003048 if (ComplexMode) {
3049 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003050 }
3051
3052 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003053 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003054 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003055 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003056 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003057 cast<ValueDecl>(D)->setType(NewTy);
3058}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003059
Chandler Carruthedc2c642011-07-02 00:01:44 +00003060static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003061 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003062 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003063 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003064
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003065 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003066 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003067 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00003068 return;
3069 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003070
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003071 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003072}
3073
Chandler Carruthedc2c642011-07-02 00:01:44 +00003074static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003075 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003076 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003077 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003078
Mike Stumpd3bb5572009-07-24 19:02:52 +00003079
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003080 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003081 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003082 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003083 return;
3084 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003085
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003086 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003087}
3088
Chandler Carruthedc2c642011-07-02 00:01:44 +00003089static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3090 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003091 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003092 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003093 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003094
Chris Lattner3c77a352010-06-22 00:03:40 +00003095
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003096 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003097 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003098 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003099 return;
3100 }
3101
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003102 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003103 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003104}
3105
Chandler Carruthedc2c642011-07-02 00:01:44 +00003106static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003107 if (S.LangOpts.CUDA) {
3108 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003109 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003110 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3111 return;
3112 }
3113
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003114 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003115 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003116 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003117 return;
3118 }
3119
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003120 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003121 } else {
3122 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3123 }
3124}
3125
Chandler Carruthedc2c642011-07-02 00:01:44 +00003126static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003127 if (S.LangOpts.CUDA) {
3128 // check the attribute arguments.
3129 if (Attr.getNumArgs() != 0) {
3130 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3131 return;
3132 }
3133
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003134 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003135 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003136 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003137 return;
3138 }
3139
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003140 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003141 } else {
3142 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3143 }
3144}
3145
Chandler Carruthedc2c642011-07-02 00:01:44 +00003146static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003147 if (S.LangOpts.CUDA) {
3148 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003149 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003150 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003151
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003152 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003153 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003154 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003155 return;
3156 }
3157
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003158 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003159 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003160 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003161 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3162 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3163 << FD->getType()
3164 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3165 "void");
3166 } else {
3167 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3168 << FD->getType();
3169 }
3170 return;
3171 }
3172
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003173 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003174 } else {
3175 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3176 }
3177}
3178
Chandler Carruthedc2c642011-07-02 00:01:44 +00003179static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003180 if (S.LangOpts.CUDA) {
3181 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003182 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003183 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003184
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003185
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003186 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003187 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003188 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003189 return;
3190 }
3191
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003192 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003193 } else {
3194 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3195 }
3196}
3197
Chandler Carruthedc2c642011-07-02 00:01:44 +00003198static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003199 if (S.LangOpts.CUDA) {
3200 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003201 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003202 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003203
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003204
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003205 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003206 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003207 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003208 return;
3209 }
3210
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003211 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003212 } else {
3213 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3214 }
3215}
3216
Chandler Carruthedc2c642011-07-02 00:01:44 +00003217static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003218 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003219 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003220 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003221
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003222 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003223 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003225 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003226 return;
3227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003228
Douglas Gregor35b57532009-10-27 21:01:01 +00003229 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003230 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003231 return;
3232 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003233
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003234 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003235}
3236
Chandler Carruthedc2c642011-07-02 00:01:44 +00003237static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003238 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003239
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003240 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003241 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3242 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003243 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003244 return;
3245
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003246 if (!isa<ObjCMethodDecl>(D)) {
3247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3248 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003249 return;
3250 }
3251
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003252 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00003253 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003254 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003255 return;
3256 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003257 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003258 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00003259 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003260 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003261 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003262 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003263 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003264 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003265 case AttributeList::AT_pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003266 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003267 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003268 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003269 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003270 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003271 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003272 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003273 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003274 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003275 return;
3276 }
3277
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003278 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003279 PcsAttr::PCSType PCS;
3280 if (StrRef == "aapcs")
3281 PCS = PcsAttr::AAPCS;
3282 else if (StrRef == "aapcs-vfp")
3283 PCS = PcsAttr::AAPCS_VFP;
3284 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003285 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3286 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003287 return;
3288 }
3289
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003290 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003291 }
Abramo Bagnara50099372010-04-30 13:10:51 +00003292 default:
3293 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003294 }
3295}
3296
Chandler Carruthedc2c642011-07-02 00:01:44 +00003297static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003298 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003299 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003300}
3301
John McCall3882ace2011-01-05 12:14:39 +00003302bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3303 if (attr.isInvalid())
3304 return true;
3305
Ted Kremenek1551d552011-04-15 05:49:29 +00003306 if ((attr.getNumArgs() != 0 &&
3307 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3308 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00003309 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3310 attr.setInvalid();
3311 return true;
3312 }
3313
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003314 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3315 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003316 switch (attr.getKind()) {
3317 case AttributeList::AT_cdecl: CC = CC_C; break;
3318 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3319 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3320 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3321 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003322 case AttributeList::AT_pcs: {
3323 Expr *Arg = attr.getArg(0);
3324 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003325 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003326 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3327 << "pcs" << 1;
3328 attr.setInvalid();
3329 return true;
3330 }
3331
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003332 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003333 if (StrRef == "aapcs") {
3334 CC = CC_AAPCS;
3335 break;
3336 } else if (StrRef == "aapcs-vfp") {
3337 CC = CC_AAPCS_VFP;
3338 break;
3339 }
3340 // FALLS THROUGH
3341 }
David Blaikie8a40f702012-01-17 06:56:22 +00003342 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003343 }
3344
3345 return false;
3346}
3347
Chandler Carruthedc2c642011-07-02 00:01:44 +00003348static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003349 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003350
3351 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003352 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003353 return;
3354
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003355 if (!isa<ObjCMethodDecl>(D)) {
3356 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3357 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003358 return;
3359 }
Eli Friedman7044b762009-03-27 21:06:47 +00003360
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003361 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003362}
3363
3364/// Checks a regparm attribute, returning true if it is ill-formed and
3365/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003366bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3367 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003368 return true;
3369
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003370 if (Attr.getNumArgs() != 1) {
3371 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3372 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003373 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003374 }
Eli Friedman7044b762009-03-27 21:06:47 +00003375
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003376 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003377 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003378 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003379 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003380 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003381 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003382 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003383 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003384 }
3385
Douglas Gregore8bbc122011-09-02 00:18:52 +00003386 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003387 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003388 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003389 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003390 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003391 }
3392
John McCall3882ace2011-01-05 12:14:39 +00003393 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003394 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003395 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003396 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003397 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003398 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003399 }
3400
John McCall3882ace2011-01-05 12:14:39 +00003401 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003402}
3403
Chandler Carruthedc2c642011-07-02 00:01:44 +00003404static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003405 if (S.LangOpts.CUDA) {
3406 // check the attribute arguments.
3407 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003408 // FIXME: 0 is not okay.
3409 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003410 return;
3411 }
3412
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003413 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003414 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003415 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003416 return;
3417 }
3418
3419 Expr *MaxThreadsExpr = Attr.getArg(0);
3420 llvm::APSInt MaxThreads(32);
3421 if (MaxThreadsExpr->isTypeDependent() ||
3422 MaxThreadsExpr->isValueDependent() ||
3423 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3424 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3425 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3426 return;
3427 }
3428
3429 llvm::APSInt MinBlocks(32);
3430 if (Attr.getNumArgs() > 1) {
3431 Expr *MinBlocksExpr = Attr.getArg(1);
3432 if (MinBlocksExpr->isTypeDependent() ||
3433 MinBlocksExpr->isValueDependent() ||
3434 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3435 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3436 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3437 return;
3438 }
3439 }
3440
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003441 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003442 MaxThreads.getZExtValue(),
3443 MinBlocks.getZExtValue()));
3444 } else {
3445 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3446 }
3447}
3448
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003449//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003450// Checker-specific attribute handlers.
3451//===----------------------------------------------------------------------===//
3452
John McCalled433932011-01-25 03:31:58 +00003453static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003454 return type->isDependentType() ||
3455 type->isObjCObjectPointerType() ||
3456 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003457}
3458static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003459 return type->isDependentType() ||
3460 type->isPointerType() ||
3461 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003462}
3463
Chandler Carruthedc2c642011-07-02 00:01:44 +00003464static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003465 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003466 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003467 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003468 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003469 return;
3470 }
3471
3472 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003473 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00003474 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3475 cf = false;
3476 } else {
3477 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3478 cf = true;
3479 }
3480
3481 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003482 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003483 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003484 return;
3485 }
3486
3487 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003488 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003489 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003490 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003491}
3492
Chandler Carruthedc2c642011-07-02 00:01:44 +00003493static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3494 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003495 if (!isa<ObjCMethodDecl>(D)) {
3496 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003497 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003498 return;
3499 }
3500
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003501 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003502}
3503
Chandler Carruthedc2c642011-07-02 00:01:44 +00003504static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3505 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003506
John McCalled433932011-01-25 03:31:58 +00003507 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003508
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003509 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003510 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003511 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00003512 returnType = PD->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003513 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003514 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00003515 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003516 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003517 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003518 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003519 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003520 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003521 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003522 return;
3523 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003524
John McCalled433932011-01-25 03:31:58 +00003525 bool typeOK;
3526 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003527 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003528 default: llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003529 case AttributeList::AT_ns_returns_autoreleased:
3530 case AttributeList::AT_ns_returns_retained:
3531 case AttributeList::AT_ns_returns_not_retained:
3532 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3533 cf = false;
3534 break;
3535
3536 case AttributeList::AT_cf_returns_retained:
3537 case AttributeList::AT_cf_returns_not_retained:
3538 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3539 cf = true;
3540 break;
3541 }
3542
3543 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003544 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003545 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003546 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003547 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003548
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003549 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003550 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003551 llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003552 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003553 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003554 S.Context));
3555 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003556 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003557 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003558 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003559 return;
3560 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003561 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003562 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003563 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003564 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003565 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003566 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003567 return;
3568 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003569 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003570 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003571 return;
3572 };
3573}
3574
John McCallcf166702011-07-22 08:53:00 +00003575static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3576 const AttributeList &attr) {
3577 SourceLocation loc = attr.getLoc();
3578
3579 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3580
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003581 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003582 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003583 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003584 return;
3585 }
3586
3587 // Check that the method returns a normal pointer.
3588 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003589
3590 if (!resultType->isReferenceType() &&
3591 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003592 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3593 << SourceRange(loc)
3594 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3595
3596 // Drop the attribute.
3597 return;
3598 }
3599
3600 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003601 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003602}
3603
John McCall32f5fe12011-09-30 05:12:12 +00003604/// Handle cf_audited_transfer and cf_unknown_transfer.
3605static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3606 if (!isa<FunctionDecl>(D)) {
3607 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003608 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00003609 return;
3610 }
3611
3612 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3613
3614 // Check whether there's a conflicting attribute already present.
3615 Attr *Existing;
3616 if (IsAudited) {
3617 Existing = D->getAttr<CFUnknownTransferAttr>();
3618 } else {
3619 Existing = D->getAttr<CFAuditedTransferAttr>();
3620 }
3621 if (Existing) {
3622 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3623 << A.getName()
3624 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3625 << A.getRange() << Existing->getRange();
3626 return;
3627 }
3628
3629 // All clear; add the attribute.
3630 if (IsAudited) {
3631 D->addAttr(
3632 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3633 } else {
3634 D->addAttr(
3635 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3636 }
3637}
3638
John McCallf1e8b342011-09-29 07:17:38 +00003639static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3640 const AttributeList &Attr) {
3641 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3642 if (!RD || RD->isUnion()) {
3643 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003644 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003645 }
3646
3647 IdentifierInfo *ParmName = Attr.getParameterName();
3648
3649 // In Objective-C, verify that the type names an Objective-C type.
3650 // We don't want to check this outside of ObjC because people sometimes
3651 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003652 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003653 // Check for an existing type with this name.
3654 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3655 Sema::LookupOrdinaryName);
3656 if (S.LookupName(R, Sc)) {
3657 NamedDecl *Target = R.getFoundDecl();
3658 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3659 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3660 S.Diag(Target->getLocStart(), diag::note_declared_at);
3661 }
3662 }
3663 }
3664
3665 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3666 ParmName));
3667}
3668
Chandler Carruthedc2c642011-07-02 00:01:44 +00003669static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3670 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003671 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003672
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003673 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003674 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003675}
3676
Chandler Carruthedc2c642011-07-02 00:01:44 +00003677static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3678 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003679 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003680 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003681 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003682 return;
3683 }
3684
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003685 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003686 QualType type = vd->getType();
3687
3688 if (!type->isDependentType() &&
3689 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003690 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003691 << type;
3692 return;
3693 }
3694
3695 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3696
3697 // If we have no lifetime yet, check the lifetime we're presumably
3698 // going to infer.
3699 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3700 lifetime = type->getObjCARCImplicitLifetime();
3701
3702 switch (lifetime) {
3703 case Qualifiers::OCL_None:
3704 assert(type->isDependentType() &&
3705 "didn't infer lifetime for non-dependent type?");
3706 break;
3707
3708 case Qualifiers::OCL_Weak: // meaningful
3709 case Qualifiers::OCL_Strong: // meaningful
3710 break;
3711
3712 case Qualifiers::OCL_ExplicitNone:
3713 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003714 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003715 << (lifetime == Qualifiers::OCL_Autoreleasing);
3716 break;
3717 }
3718
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003719 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003720 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003721}
3722
Charles Davis163855f2010-02-16 18:27:26 +00003723static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman0c84ebb2012-02-23 22:46:33 +00003724 switch (Attr.getKind()) {
3725 default:
3726 return false;
3727 case AttributeList::AT_dllimport:
3728 case AttributeList::AT_dllexport:
3729 case AttributeList::AT_uuid:
3730 case AttributeList::AT_deprecated:
3731 case AttributeList::AT_noreturn:
3732 case AttributeList::AT_nothrow:
3733 case AttributeList::AT_naked:
3734 case AttributeList::AT_noinline:
3735 return true;
3736 }
Francois Picheta83957a2010-12-19 06:50:37 +00003737}
3738
3739//===----------------------------------------------------------------------===//
3740// Microsoft specific attribute handlers.
3741//===----------------------------------------------------------------------===//
3742
Chandler Carruthedc2c642011-07-02 00:01:44 +00003743static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00003744 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00003745 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003746 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003747 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003748
Francois Picheta83957a2010-12-19 06:50:37 +00003749 Expr *Arg = Attr.getArg(0);
3750 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003751 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003752 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3753 << "uuid" << 1;
3754 return;
3755 }
3756
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003757 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003758
3759 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3760 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003761
Francois Pichet7da11662010-12-20 01:41:49 +00003762 // Validate GUID length.
3763 if (IsCurly && StrRef.size() != 38) {
3764 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3765 return;
3766 }
3767 if (!IsCurly && StrRef.size() != 36) {
3768 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3769 return;
3770 }
3771
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003772 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00003773 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003774 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003775 if (IsCurly) // Skip the optional '{'
3776 ++I;
3777
3778 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003779 if (i == 8 || i == 13 || i == 18 || i == 23) {
3780 if (*I != '-') {
3781 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3782 return;
3783 }
3784 } else if (!isxdigit(*I)) {
3785 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3786 return;
3787 }
3788 I++;
3789 }
Francois Picheta83957a2010-12-19 06:50:37 +00003790
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003791 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003792 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003793 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003794 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003795}
3796
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003797//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003798// Top Level Sema Entry Points
3799//===----------------------------------------------------------------------===//
3800
Chandler Carruthedc2c642011-07-02 00:01:44 +00003801static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3802 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003803 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003804 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3805 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3806 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003807 default:
3808 break;
3809 }
3810}
Abramo Bagnara50099372010-04-30 13:10:51 +00003811
Chandler Carruthedc2c642011-07-02 00:01:44 +00003812static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3813 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003814 switch (Attr.getKind()) {
Michael Han4a045172012-03-07 00:12:16 +00003815 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3816 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3817 case AttributeList::AT_iboutletcollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003818 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003819 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003820 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003821 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003822 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003823 case AttributeList::AT_neon_vector_type:
3824 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003825 // Ignore these, these are type attributes, handled by
3826 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003827 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003828 case AttributeList::AT_device:
3829 case AttributeList::AT_host:
3830 case AttributeList::AT_overloadable:
3831 // Ignore, this is a non-inheritable attribute, handled
3832 // by ProcessNonInheritableDeclAttr.
3833 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003834 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3835 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003836 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003837 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003838 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003839 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3840 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3841 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003842 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003843 handleDependencyAttr (S, D, Attr); break;
3844 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3845 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3846 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3847 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3848 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003849 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003850 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003851 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003852 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3853 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3854 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3855 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003856 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003857 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003858 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003859 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3860 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3861 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3862 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3863 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003864 case AttributeList::AT_ownership_returns:
3865 case AttributeList::AT_ownership_takes:
3866 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003867 handleOwnershipAttr (S, D, Attr); break;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00003868 case AttributeList::AT_cold: handleColdAttr (S, D, Attr); break;
3869 case AttributeList::AT_hot: handleHotAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003870 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3871 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3872 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3873 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3874 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003875
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003876 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003877 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003878 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003879 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003880
John McCallcf166702011-07-22 08:53:00 +00003881 case AttributeList::AT_objc_returns_inner_pointer:
3882 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3883
John McCallf1e8b342011-09-29 07:17:38 +00003884 case AttributeList::AT_ns_bridged:
3885 handleNSBridgedAttr(S, scope, D, Attr); break;
3886
John McCall32f5fe12011-09-30 05:12:12 +00003887 case AttributeList::AT_cf_audited_transfer:
3888 case AttributeList::AT_cf_unknown_transfer:
3889 handleCFTransferAttr(S, D, Attr); break;
3890
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003891 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003892 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003893 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003894 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003895 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003896
3897 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003898 case AttributeList::AT_ns_returns_not_retained:
3899 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003900 case AttributeList::AT_ns_returns_retained:
3901 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003902 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003903
Michael Han4a045172012-03-07 00:12:16 +00003904 case AttributeList::AT_reqd_work_group_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003905 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003906
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003907 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003908 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003909
Chandler Carruthedc2c642011-07-02 00:01:44 +00003910 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003911 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003912 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3913 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003914 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003915 handleArcWeakrefUnavailableAttr (S, D, Attr);
3916 break;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00003917 case AttributeList::AT_objc_root_class:
3918 handleObjCRootClassAttr(S, D, Attr);
3919 break;
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00003920 case AttributeList::AT_objc_requires_property_definitions:
3921 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00003922 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003923 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindola70107f92011-10-03 14:59:42 +00003924 case AttributeList::AT_returns_twice:
3925 handleReturnsTwiceAttr(S, D, Attr);
3926 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003927 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3928 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3929 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003930 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003931 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3932 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3933 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003934 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003935 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003936 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003937 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003938 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003939 break;
John McCall86bc21f2011-03-02 11:33:24 +00003940 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003941 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003942 break;
Michael Han4a045172012-03-07 00:12:16 +00003943 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003944 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3945 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3946 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3947 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3948 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3949 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3950 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3951 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003952 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003953 // Just ignore
3954 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003955 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003956 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003957 break;
John McCallab26cfa2010-02-05 21:31:56 +00003958 case AttributeList::AT_stdcall:
3959 case AttributeList::AT_cdecl:
3960 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003961 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003962 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003963 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003964 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003965 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003966 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003967 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003968 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003969 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003970 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003971 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003972
3973 // Thread safety attributes:
3974 case AttributeList::AT_guarded_var:
3975 handleGuardedVarAttr(S, D, Attr);
3976 break;
3977 case AttributeList::AT_pt_guarded_var:
3978 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3979 break;
3980 case AttributeList::AT_scoped_lockable:
3981 handleLockableAttr(S, D, Attr, /*scoped = */true);
3982 break;
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00003983 case AttributeList::AT_no_address_safety_analysis:
3984 handleNoAddressSafetyAttr(S, D, Attr);
3985 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003986 case AttributeList::AT_no_thread_safety_analysis:
3987 handleNoThreadSafetyAttr(S, D, Attr);
3988 break;
3989 case AttributeList::AT_lockable:
3990 handleLockableAttr(S, D, Attr);
3991 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00003992 case AttributeList::AT_guarded_by:
3993 handleGuardedByAttr(S, D, Attr);
3994 break;
3995 case AttributeList::AT_pt_guarded_by:
3996 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3997 break;
3998 case AttributeList::AT_exclusive_lock_function:
3999 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
4000 break;
4001 case AttributeList::AT_exclusive_locks_required:
4002 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
4003 break;
4004 case AttributeList::AT_exclusive_trylock_function:
4005 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
4006 break;
4007 case AttributeList::AT_lock_returned:
4008 handleLockReturnedAttr(S, D, Attr);
4009 break;
4010 case AttributeList::AT_locks_excluded:
4011 handleLocksExcludedAttr(S, D, Attr);
4012 break;
4013 case AttributeList::AT_shared_lock_function:
4014 handleLockFunAttr(S, D, Attr);
4015 break;
4016 case AttributeList::AT_shared_locks_required:
4017 handleLocksRequiredAttr(S, D, Attr);
4018 break;
4019 case AttributeList::AT_shared_trylock_function:
4020 handleTrylockFunAttr(S, D, Attr);
4021 break;
4022 case AttributeList::AT_unlock_function:
4023 handleUnlockFunAttr(S, D, Attr);
4024 break;
4025 case AttributeList::AT_acquired_before:
4026 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
4027 break;
4028 case AttributeList::AT_acquired_after:
4029 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
4030 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004031
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004032 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004033 // Ask target about the attribute.
4034 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4035 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00004036 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
4037 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004038 break;
4039 }
4040}
4041
Peter Collingbourneb331b262011-01-21 02:08:45 +00004042/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4043/// the attribute applies to decls. If the attribute is a type attribute, just
4044/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4045/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00004046static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4047 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004048 bool NonInheritable, bool Inheritable) {
4049 if (Attr.isInvalid())
4050 return;
4051
4052 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
4053 // FIXME: Try to deal with other __declspec attributes!
4054 return;
4055
4056 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004057 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004058
4059 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004060 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004061}
4062
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004063/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4064/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004065void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004066 const AttributeList *AttrList,
4067 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004068 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004069 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004070 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004071
4072 // GCC accepts
4073 // static int a9 __attribute__((weakref));
4074 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004075 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004076 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004077 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004078 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004079 }
4080}
4081
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004082// Annotation attributes are the only attributes allowed after an access
4083// specifier.
4084bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4085 const AttributeList *AttrList) {
4086 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
4087 if (l->getKind() == AttributeList::AT_annotate) {
4088 handleAnnotateAttr(*this, ASDecl, *l);
4089 } else {
4090 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4091 return true;
4092 }
4093 }
4094
4095 return false;
4096}
4097
John McCall42856de2011-10-01 05:17:03 +00004098/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4099/// contains any decl attributes that we should warn about.
4100static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4101 for ( ; A; A = A->getNext()) {
4102 // Only warn if the attribute is an unignored, non-type attribute.
4103 if (A->isUsedAsTypeAttr()) continue;
4104 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4105
4106 if (A->getKind() == AttributeList::UnknownAttribute) {
4107 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4108 << A->getName() << A->getRange();
4109 } else {
4110 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4111 << A->getName() << A->getRange();
4112 }
4113 }
4114}
4115
4116/// checkUnusedDeclAttributes - Given a declarator which is not being
4117/// used to build a declaration, complain about any decl attributes
4118/// which might be lying around on it.
4119void Sema::checkUnusedDeclAttributes(Declarator &D) {
4120 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4121 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4122 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4123 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4124}
4125
Ryan Flynn7d470f32009-07-30 03:15:39 +00004126/// DeclClonePragmaWeak - clone existing decl (maybe definition),
4127/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedmance3e2c82011-09-07 04:05:06 +00004128NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4129 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004130 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004131 NamedDecl *NewD = 0;
4132 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004133 FunctionDecl *NewFD;
4134 // FIXME: Missing call to CheckFunctionDeclaration().
4135 // FIXME: Mangling?
4136 // FIXME: Is the qualifier info correct?
4137 // FIXME: Is the DeclContext correct?
4138 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4139 Loc, Loc, DeclarationName(II),
4140 FD->getType(), FD->getTypeSourceInfo(),
4141 SC_None, SC_None,
4142 false/*isInlineSpecified*/,
4143 FD->hasPrototype(),
4144 false/*isConstexprSpecified*/);
4145 NewD = NewFD;
4146
4147 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004148 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004149
4150 // Fake up parameter variables; they are declared as if this were
4151 // a typedef.
4152 QualType FDTy = FD->getType();
4153 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4154 SmallVector<ParmVarDecl*, 16> Params;
4155 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4156 AE = FT->arg_type_end(); AI != AE; ++AI) {
4157 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4158 Param->setScopeInfo(0, Params.size());
4159 Params.push_back(Param);
4160 }
David Blaikie9c70e042011-09-21 18:16:56 +00004161 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004162 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004163 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4164 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004165 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004166 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004167 VD->getStorageClass(),
4168 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004169 if (VD->getQualifier()) {
4170 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004171 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004172 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004173 }
4174 return NewD;
4175}
4176
4177/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
4178/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004179void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004180 if (W.getUsed()) return; // only do this once
4181 W.setUsed(true);
4182 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4183 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004184 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004185 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4186 NDId->getName()));
4187 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004188 WeakTopLevelDecl.push_back(NewD);
4189 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4190 // to insert Decl at TU scope, sorry.
4191 DeclContext *SavedContext = CurContext;
4192 CurContext = Context.getTranslationUnitDecl();
4193 PushOnScopeChains(NewD, S);
4194 CurContext = SavedContext;
4195 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004196 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004197 }
4198}
4199
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004200/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4201/// it, apply them to D. This is a bit tricky because PD can have attributes
4202/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004203void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4204 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004205 // It's valid to "forward-declare" #pragma weak, in which case we
4206 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004207 if (Inheritable) {
4208 LoadExternalWeakUndeclaredIdentifiers();
4209 if (!WeakUndeclaredIdentifiers.empty()) {
4210 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4211 if (IdentifierInfo *Id = ND->getIdentifier()) {
4212 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4213 = WeakUndeclaredIdentifiers.find(Id);
4214 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4215 WeakInfo W = I->second;
4216 DeclApplyPragmaWeak(S, ND, W);
4217 WeakUndeclaredIdentifiers[Id] = W;
4218 }
John McCall6fe02402010-10-27 00:59:00 +00004219 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004220 }
4221 }
4222 }
4223
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004224 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004225 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004226 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004227
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004228 // Walk the declarator structure, applying decl attributes that were in a type
4229 // position to the decl itself. This handles cases like:
4230 // int *__attr__(x)** D;
4231 // when X is a decl attribute.
4232 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4233 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004234 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004235
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004236 // Finally, apply any attributes on the decl itself.
4237 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004238 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004239}
John McCall28a6aea2009-11-04 02:18:39 +00004240
John McCall31168b02011-06-15 23:02:42 +00004241/// Is the given declaration allowed to use a forbidden type?
4242static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4243 // Private ivars are always okay. Unfortunately, people don't
4244 // always properly make their ivars private, even in system headers.
4245 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004246 // Function declarations in sys headers will be marked unavailable.
4247 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4248 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004249 return false;
4250
4251 // Require it to be declared in a system header.
4252 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4253}
4254
4255/// Handle a delayed forbidden-type diagnostic.
4256static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4257 Decl *decl) {
4258 if (decl && isForbiddenTypeAllowed(S, decl)) {
4259 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4260 "this system declaration uses an unsupported type"));
4261 return;
4262 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004263 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004264 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4265 // FIXME. we may want to supress diagnostics for all
4266 // kind of forbidden type messages on unavailable functions.
4267 if (FD->hasAttr<UnavailableAttr>() &&
4268 diag.getForbiddenTypeDiagnostic() ==
4269 diag::err_arc_array_param_no_ownership) {
4270 diag.Triggered = true;
4271 return;
4272 }
4273 }
John McCall31168b02011-06-15 23:02:42 +00004274
4275 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4276 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4277 diag.Triggered = true;
4278}
4279
John McCall2ec85372012-05-07 06:16:41 +00004280void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4281 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004282 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004283 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004284
John McCall2ec85372012-05-07 06:16:41 +00004285 // When delaying diagnostics to run in the context of a parsed
4286 // declaration, we only want to actually emit anything if parsing
4287 // succeeds.
4288 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004289
John McCall2ec85372012-05-07 06:16:41 +00004290 // We emit all the active diagnostics in this pool or any of its
4291 // parents. In general, we'll get one pool for the decl spec
4292 // and a child pool for each declarator; in a decl group like:
4293 // deprecated_typedef foo, *bar, baz();
4294 // only the declarator pops will be passed decls. This is correct;
4295 // we really do need to consider delayed diagnostics from the decl spec
4296 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004297 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004298 do {
John McCall6347b682012-05-07 06:16:58 +00004299 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004300 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4301 // This const_cast is a bit lame. Really, Triggered should be mutable.
4302 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004303 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004304 continue;
4305
John McCallc1465822011-02-14 07:13:47 +00004306 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004307 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004308 // Don't bother giving deprecation diagnostics if the decl is invalid.
4309 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004310 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004311 break;
4312
4313 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004314 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004315 break;
John McCall31168b02011-06-15 23:02:42 +00004316
4317 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004318 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004319 break;
John McCall86121512010-01-27 03:50:35 +00004320 }
4321 }
John McCall2ec85372012-05-07 06:16:41 +00004322 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004323}
4324
John McCall6347b682012-05-07 06:16:58 +00004325/// Given a set of delayed diagnostics, re-emit them as if they had
4326/// been delayed in the current context instead of in the given pool.
4327/// Essentially, this just moves them to the current pool.
4328void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4329 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4330 assert(curPool && "re-emitting in undelayed context not supported");
4331 curPool->steal(pool);
4332}
4333
John McCall28a6aea2009-11-04 02:18:39 +00004334static bool isDeclDeprecated(Decl *D) {
4335 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004336 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004337 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004338 // A category implicitly has the availability of the interface.
4339 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4340 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004341 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4342 return false;
4343}
4344
John McCallb45a1e72010-08-26 02:13:20 +00004345void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004346 Decl *Ctx) {
4347 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004348 return;
4349
John McCall86121512010-01-27 03:50:35 +00004350 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004351 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004352 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004353 << DD.getDeprecationDecl()->getDeclName()
4354 << DD.getDeprecationMessage();
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004355 else if (DD.getUnknownObjCClass()) {
4356 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4357 << DD.getDeprecationDecl()->getDeclName();
4358 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4359 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00004360 else
4361 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004362 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00004363}
4364
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004365void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004366 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004367 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00004368 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004369 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004370 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4371 UnknownObjCClass,
4372 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004373 return;
4374 }
4375
4376 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004377 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004378 return;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004379 if (!Message.empty()) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00004380 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4381 << Message;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004382 Diag(D->getLocation(),
4383 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4384 : diag::note_previous_decl) << D->getDeclName();
4385 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004386 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00004387 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004388 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004389 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004390 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004391 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4392 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004393 }
John McCall28a6aea2009-11-04 02:18:39 +00004394}