blob: 6d0e30c77cd44cb5b356ca6d88a1bdee6e1319d4 [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
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001645template <typename AttrTy>
1646static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1647 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001648 unsigned NumArgs = Attr.getNumArgs();
1649 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001650 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001651 return;
1652 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001653
1654 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001655 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001656 if (NumArgs == 1) {
1657 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001658 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001659 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001660 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001661 return;
1662 }
Chris Lattner190aa102011-02-24 05:42:24 +00001663 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001664 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001665
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001666 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001667}
1668
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001669static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1670 const AttributeList &Attr) {
1671 unsigned NumArgs = Attr.getNumArgs();
1672 if (NumArgs > 0) {
1673 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1674 return;
1675 }
1676
1677 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001678 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001679}
1680
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001681static void handleObjCRootClassAttr(Sema &S, Decl *D,
1682 const AttributeList &Attr) {
1683 if (!isa<ObjCInterfaceDecl>(D)) {
1684 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1685 return;
1686 }
1687
1688 unsigned NumArgs = Attr.getNumArgs();
1689 if (NumArgs > 0) {
1690 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1691 return;
1692 }
1693
1694 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1695}
1696
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001697static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001698 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001699 if (!isa<ObjCInterfaceDecl>(D)) {
1700 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1701 return;
1702 }
1703
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001704 unsigned NumArgs = Attr.getNumArgs();
1705 if (NumArgs > 0) {
1706 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1707 return;
1708 }
1709
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001710 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001711 Attr.getRange(), S.Context));
1712}
1713
Jordy Rose740b0c22012-05-08 03:27:22 +00001714static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1715 IdentifierInfo *Platform,
1716 VersionTuple Introduced,
1717 VersionTuple Deprecated,
1718 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001719 StringRef PlatformName
1720 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1721 if (PlatformName.empty())
1722 PlatformName = Platform->getName();
1723
1724 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1725 // of these steps are needed).
1726 if (!Introduced.empty() && !Deprecated.empty() &&
1727 !(Introduced <= Deprecated)) {
1728 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1729 << 1 << PlatformName << Deprecated.getAsString()
1730 << 0 << Introduced.getAsString();
1731 return true;
1732 }
1733
1734 if (!Introduced.empty() && !Obsoleted.empty() &&
1735 !(Introduced <= Obsoleted)) {
1736 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1737 << 2 << PlatformName << Obsoleted.getAsString()
1738 << 0 << Introduced.getAsString();
1739 return true;
1740 }
1741
1742 if (!Deprecated.empty() && !Obsoleted.empty() &&
1743 !(Deprecated <= Obsoleted)) {
1744 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1745 << 2 << PlatformName << Obsoleted.getAsString()
1746 << 1 << Deprecated.getAsString();
1747 return true;
1748 }
1749
1750 return false;
1751}
1752
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001753AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1754 IdentifierInfo *Platform,
1755 VersionTuple Introduced,
1756 VersionTuple Deprecated,
1757 VersionTuple Obsoleted,
1758 bool IsUnavailable,
1759 StringRef Message) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001760 VersionTuple MergedIntroduced = Introduced;
1761 VersionTuple MergedDeprecated = Deprecated;
1762 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001763 bool FoundAny = false;
1764
Rafael Espindolac67f2232012-05-10 02:50:16 +00001765 if (D->hasAttrs()) {
1766 AttrVec &Attrs = D->getAttrs();
1767 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1768 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1769 if (!OldAA) {
1770 ++i;
1771 continue;
1772 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001773
Rafael Espindolac67f2232012-05-10 02:50:16 +00001774 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1775 if (OldPlatform != Platform) {
1776 ++i;
1777 continue;
1778 }
1779
1780 FoundAny = true;
1781 VersionTuple OldIntroduced = OldAA->getIntroduced();
1782 VersionTuple OldDeprecated = OldAA->getDeprecated();
1783 VersionTuple OldObsoleted = OldAA->getObsoleted();
1784 bool OldIsUnavailable = OldAA->getUnavailable();
1785 StringRef OldMessage = OldAA->getMessage();
1786
1787 if ((!OldIntroduced.empty() && !Introduced.empty() &&
1788 OldIntroduced != Introduced) ||
1789 (!OldDeprecated.empty() && !Deprecated.empty() &&
1790 OldDeprecated != Deprecated) ||
1791 (!OldObsoleted.empty() && !Obsoleted.empty() &&
1792 OldObsoleted != Obsoleted) ||
1793 (OldIsUnavailable != IsUnavailable) ||
1794 (OldMessage != Message)) {
1795 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1796 Diag(Range.getBegin(), diag::note_previous_attribute);
1797 Attrs.erase(Attrs.begin() + i);
1798 --e;
1799 continue;
1800 }
1801
1802 VersionTuple MergedIntroduced2 = MergedIntroduced;
1803 VersionTuple MergedDeprecated2 = MergedDeprecated;
1804 VersionTuple MergedObsoleted2 = MergedObsoleted;
1805
1806 if (MergedIntroduced2.empty())
1807 MergedIntroduced2 = OldIntroduced;
1808 if (MergedDeprecated2.empty())
1809 MergedDeprecated2 = OldDeprecated;
1810 if (MergedObsoleted2.empty())
1811 MergedObsoleted2 = OldObsoleted;
1812
1813 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1814 MergedIntroduced2, MergedDeprecated2,
1815 MergedObsoleted2)) {
1816 Attrs.erase(Attrs.begin() + i);
1817 --e;
1818 continue;
1819 }
1820
1821 MergedIntroduced = MergedIntroduced2;
1822 MergedDeprecated = MergedDeprecated2;
1823 MergedObsoleted = MergedObsoleted2;
1824 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001825 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001826 }
1827
1828 if (FoundAny &&
1829 MergedIntroduced == Introduced &&
1830 MergedDeprecated == Deprecated &&
1831 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001832 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001833
Rafael Espindolac67f2232012-05-10 02:50:16 +00001834 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001835 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001836 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1837 Introduced, Deprecated,
1838 Obsoleted, IsUnavailable, Message);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001839 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001840 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001841}
1842
Chandler Carruthedc2c642011-07-02 00:01:44 +00001843static void handleAvailabilityAttr(Sema &S, Decl *D,
1844 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001845 IdentifierInfo *Platform = Attr.getParameterName();
1846 SourceLocation PlatformLoc = Attr.getParameterLoc();
1847
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001848 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001849 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1850 << Platform;
1851
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001852 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1853 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1854 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001855 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001856 StringRef Str;
1857 const StringLiteral *SE =
1858 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1859 if (SE)
1860 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001861
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001862 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
1863 Platform,
1864 Introduced.Version,
1865 Deprecated.Version,
1866 Obsoleted.Version,
1867 IsUnavailable, Str);
1868 if (NewAttr)
1869 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001870}
1871
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001872VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
1873 VisibilityAttr::VisibilityType Vis) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00001874 if (isa<TypedefNameDecl>(D)) {
1875 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001876 return NULL;
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00001877 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00001878 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
1879 if (ExistingAttr) {
1880 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
1881 if (ExistingVis == Vis)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001882 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +00001883 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
1884 Diag(Range.getBegin(), diag::note_previous_attribute);
1885 D->dropAttr<VisibilityAttr>();
1886 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001887 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001888}
1889
Chandler Carruthedc2c642011-07-02 00:01:44 +00001890static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001891 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001892 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001893 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001894
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001895 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001896 Arg = Arg->IgnoreParenCasts();
1897 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001898
Douglas Gregorfb65e592011-07-27 05:40:30 +00001899 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001901 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001902 return;
1903 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001904
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001905 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001906 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001907
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001908 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001909 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001910 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001911 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001912 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001913 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00001914 else if (TypeStr == "protected") {
1915 // Complain about attempts to use protected visibility on targets
1916 // (like Darwin) that don't support it.
1917 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1918 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1919 type = VisibilityAttr::Default;
1920 } else {
1921 type = VisibilityAttr::Protected;
1922 }
1923 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001924 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001925 return;
1926 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001927
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001928 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
1929 if (NewAttr)
1930 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001931}
1932
Chandler Carruthedc2c642011-07-02 00:01:44 +00001933static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1934 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001935 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1936 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001938 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001939 return;
1940 }
1941
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001942 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1943 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1944 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001945 << "objc_method_family" << 1;
1946 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001948 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001949 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001950 return;
1951 }
1952
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001953 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001954 ObjCMethodFamilyAttr::FamilyKind family;
1955 if (param == "none")
1956 family = ObjCMethodFamilyAttr::OMF_None;
1957 else if (param == "alloc")
1958 family = ObjCMethodFamilyAttr::OMF_alloc;
1959 else if (param == "copy")
1960 family = ObjCMethodFamilyAttr::OMF_copy;
1961 else if (param == "init")
1962 family = ObjCMethodFamilyAttr::OMF_init;
1963 else if (param == "mutableCopy")
1964 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1965 else if (param == "new")
1966 family = ObjCMethodFamilyAttr::OMF_new;
1967 else {
1968 // Just warn and ignore it. This is future-proof against new
1969 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001970 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001971 return;
1972 }
1973
John McCall31168b02011-06-15 23:02:42 +00001974 if (family == ObjCMethodFamilyAttr::OMF_init &&
1975 !method->getResultType()->isObjCObjectPointerType()) {
1976 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1977 << method->getResultType();
1978 // Ignore the attribute.
1979 return;
1980 }
1981
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001982 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00001983 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001984}
1985
Chandler Carruthedc2c642011-07-02 00:01:44 +00001986static void handleObjCExceptionAttr(Sema &S, Decl *D,
1987 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001988 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00001989 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001990
Chris Lattner677a3582009-02-14 08:09:34 +00001991 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1992 if (OCI == 0) {
1993 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1994 return;
1995 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001996
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001997 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001998}
1999
Chandler Carruthedc2c642011-07-02 00:01:44 +00002000static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002001 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002002 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002003 return;
2004 }
Richard Smithdda56e42011-04-15 14:24:37 +00002005 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002006 QualType T = TD->getUnderlyingType();
2007 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002008 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002009 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2010 return;
2011 }
2012 }
Ted Kremenek05e916b2012-03-01 01:40:32 +00002013 else if (!isa<ObjCPropertyDecl>(D)) {
2014 // It is okay to include this attribute on properties, e.g.:
2015 //
2016 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2017 //
2018 // In this case it follows tradition and suppresses an error in the above
2019 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002020 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002021 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002022 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002023}
2024
Mike Stumpd3bb5572009-07-24 19:02:52 +00002025static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002026handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002027 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002028 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002029 return;
2030 }
2031
2032 if (!isa<FunctionDecl>(D)) {
2033 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2034 return;
2035 }
2036
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002037 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002038}
2039
Chandler Carruthedc2c642011-07-02 00:01:44 +00002040static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002041 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002042 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002043 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002044 return;
2045 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002046
Steve Naroff3405a732008-09-18 16:44:58 +00002047 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002048 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002049 return;
2050 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002051
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002052 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002053 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002054 type = BlocksAttr::ByRef;
2055 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002056 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002057 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002058 return;
2059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002060
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002061 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002062}
2063
Chandler Carruthedc2c642011-07-02 00:01:44 +00002064static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002065 // check the attribute arguments.
2066 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002067 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002068 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002069 }
2070
John McCallb46f2872011-09-09 07:56:05 +00002071 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002072 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002073 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002074 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002075 if (E->isTypeDependent() || E->isValueDependent() ||
2076 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002078 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002079 return;
2080 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002081
John McCallb46f2872011-09-09 07:56:05 +00002082 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002083 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2084 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002085 return;
2086 }
John McCallb46f2872011-09-09 07:56:05 +00002087
2088 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002089 }
2090
John McCallb46f2872011-09-09 07:56:05 +00002091 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002092 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002093 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002094 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002095 if (E->isTypeDependent() || E->isValueDependent() ||
2096 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002097 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002098 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002099 return;
2100 }
2101 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002102
John McCallb46f2872011-09-09 07:56:05 +00002103 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002104 // FIXME: This error message could be improved, it would be nice
2105 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002106 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2107 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002108 return;
2109 }
2110 }
2111
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002112 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002113 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002114 if (isa<FunctionNoProtoType>(FT)) {
2115 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2116 return;
2117 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002118
Chris Lattner9363e312009-03-17 23:03:47 +00002119 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002120 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002121 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002122 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002123 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002124 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002125 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002126 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002127 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002128 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2129 if (!BD->isVariadic()) {
2130 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2131 return;
2132 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002133 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002134 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002135 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002136 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002137 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002138 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002139 int m = Ty->isFunctionPointerType() ? 0 : 1;
2140 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002141 return;
2142 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002143 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002144 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002145 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002146 return;
2147 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002148 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002149 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002150 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002151 return;
2152 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002153 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002154 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002155}
2156
Chandler Carruthedc2c642011-07-02 00:01:44 +00002157static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002158 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002159 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002160 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002161
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002162 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002163 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002164 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002165 return;
2166 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002167
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002168 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2169 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2170 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002171 return;
2172 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002173 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2174 if (MD->getResultType()->isVoidType()) {
2175 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2176 << Attr.getName() << 1;
2177 return;
2178 }
2179
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002180 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002181}
2182
Chandler Carruthedc2c642011-07-02 00:01:44 +00002183static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002184 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002185 if (Attr.hasParameterOrArguments()) {
2186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002187 return;
2188 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002189
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002190 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002191 if (isa<CXXRecordDecl>(D)) {
2192 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2193 return;
2194 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002195 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2196 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002197 return;
2198 }
2199
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002200 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002201
2202 // 'weak' only applies to declarations with external linkage.
2203 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002204 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002205 return;
2206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002207
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002208 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002209}
2210
Chandler Carruthedc2c642011-07-02 00:01:44 +00002211static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002212 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002213 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002214 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002215
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002216
2217 // weak_import only applies to variable & function declarations.
2218 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002219 if (!D->canBeWeakImported(isDef)) {
2220 if (isDef)
2221 S.Diag(Attr.getLoc(),
2222 diag::warn_attribute_weak_import_invalid_on_definition)
2223 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002224 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002225 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002226 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002227 // Nothing to warn about here.
2228 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002229 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002230 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002231
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002232 return;
2233 }
2234
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002235 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002236}
2237
Chandler Carruthedc2c642011-07-02 00:01:44 +00002238static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2239 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002240 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002241 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00002242 return;
Nate Begemanf2758702009-06-26 06:32:41 +00002243
2244 unsigned WGSize[3];
2245 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002246 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002247 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002248 if (E->isTypeDependent() || E->isValueDependent() ||
2249 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2251 << "reqd_work_group_size" << E->getSourceRange();
2252 return;
2253 }
2254 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2255 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002256 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002257 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00002258 WGSize[2]));
2259}
2260
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002261SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2262 StringRef Name) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002263 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2264 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002265 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002266 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2267 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002268 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002269 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002270 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002271}
2272
Chandler Carruthedc2c642011-07-02 00:01:44 +00002273static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002274 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002275 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002276 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002277
2278 // Make sure that there is a string literal as the sections's single
2279 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002280 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002281 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002282 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002283 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002284 return;
2285 }
Mike Stump11289f42009-09-09 15:08:12 +00002286
Chris Lattner30ba6742009-08-10 19:03:04 +00002287 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002288 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002289 if (!Error.empty()) {
2290 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2291 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002292 return;
2293 }
Mike Stump11289f42009-09-09 15:08:12 +00002294
Chris Lattner20aee9b2010-01-12 20:58:53 +00002295 // This attribute cannot be applied to local variables.
2296 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2297 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2298 return;
2299 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002300 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2301 SE->getString());
2302 if (NewAttr)
2303 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002304}
2305
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002306
Chandler Carruthedc2c642011-07-02 00:01:44 +00002307static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002308 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002309 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002311 return;
2312 }
Douglas Gregor88336832011-06-15 05:45:11 +00002313
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002314 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002315 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002316 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002317 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002318 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002319 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002320}
2321
Chandler Carruthedc2c642011-07-02 00:01:44 +00002322static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +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;
Anders Carlssonb8316282008-10-05 23:32:53 +00002326 return;
2327 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002328
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002329 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
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) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002334 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002335}
2336
Chandler Carruthedc2c642011-07-02 00:01:44 +00002337static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002338 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002339 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002340 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002341
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002342 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002343}
2344
Chandler Carruthedc2c642011-07-02 00:01:44 +00002345static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002346 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002347 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2348 return;
2349 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002350
Anders Carlssond277d792009-01-31 01:16:18 +00002351 if (Attr.getNumArgs() != 0) {
2352 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2353 return;
2354 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002355
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002356 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002357
Anders Carlssond277d792009-01-31 01:16:18 +00002358 if (!VD || !VD->hasLocalStorage()) {
2359 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2360 return;
2361 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002362
Anders Carlssond277d792009-01-31 01:16:18 +00002363 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002364 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002365 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002366 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2367 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002368 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002369 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002370 Attr.getParameterName();
2371 return;
2372 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002373
Anders Carlssond277d792009-01-31 01:16:18 +00002374 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2375 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002376 S.Diag(Attr.getParameterLoc(),
2377 diag::err_attribute_cleanup_arg_not_function)
2378 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002379 return;
2380 }
2381
Anders Carlssond277d792009-01-31 01:16:18 +00002382 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002383 S.Diag(Attr.getParameterLoc(),
2384 diag::err_attribute_cleanup_func_must_take_one_arg)
2385 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002386 return;
2387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
Anders Carlsson723f55d2009-02-07 23:16:50 +00002389 // We're currently more strict than GCC about what function types we accept.
2390 // If this ever proves to be a problem it should be easy to fix.
2391 QualType Ty = S.Context.getPointerType(VD->getType());
2392 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002393 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2394 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002395 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002396 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2397 Attr.getParameterName() << ParamTy << Ty;
2398 return;
2399 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002400
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002401 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002402 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002403}
2404
Mike Stumpd3bb5572009-07-24 19:02:52 +00002405/// Handle __attribute__((format_arg((idx)))) attribute based on
2406/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002407static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002408 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002409 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002410
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002411 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002413 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002414 return;
2415 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002416
2417 // In C++ the implicit 'this' function parameter also counts, and they are
2418 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002419 bool HasImplicitThisParam = isInstanceMethod(D);
2420 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002421 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002422
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002423 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002424 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002425 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002426 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2427 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002428 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2429 << "format" << 2 << IdxExpr->getSourceRange();
2430 return;
2431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002432
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002433 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2434 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2435 << "format" << 2 << IdxExpr->getSourceRange();
2436 return;
2437 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002438
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002439 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002440
Chandler Carruth743682b2010-11-16 08:35:43 +00002441 if (HasImplicitThisParam) {
2442 if (ArgIdx == 0) {
2443 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2444 << "format_arg" << IdxExpr->getSourceRange();
2445 return;
2446 }
2447 ArgIdx--;
2448 }
2449
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002450 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002451 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002452
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002453 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2454 if (not_nsstring_type &&
2455 !isCFStringType(Ty, S.Context) &&
2456 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002457 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002458 // FIXME: Should highlight the actual expression that has the wrong type.
2459 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002460 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002461 << IdxExpr->getSourceRange();
2462 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002463 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002464 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002465 if (!isNSStringType(Ty, S.Context) &&
2466 !isCFStringType(Ty, S.Context) &&
2467 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002468 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002469 // FIXME: Should highlight the actual expression that has the wrong type.
2470 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002471 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002472 << IdxExpr->getSourceRange();
2473 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474 }
2475
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002476 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002477 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002478}
2479
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002480enum FormatAttrKind {
2481 CFStringFormat,
2482 NSStringFormat,
2483 StrftimeFormat,
2484 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002485 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002486 InvalidFormat
2487};
2488
2489/// getFormatAttrKind - Map from format attribute names to supported format
2490/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002491static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002492 return llvm::StringSwitch<FormatAttrKind>(Format)
2493 // Check for formats that get handled specially.
2494 .Case("NSString", NSStringFormat)
2495 .Case("CFString", CFStringFormat)
2496 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002497
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002498 // Otherwise, check for supported formats.
2499 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2500 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2501 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002502
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002503 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2504 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002505}
2506
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002507/// Handle __attribute__((init_priority(priority))) attributes based on
2508/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002509static void handleInitPriorityAttr(Sema &S, Decl *D,
2510 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002511 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002512 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2513 return;
2514 }
2515
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002516 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002517 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2518 Attr.setInvalid();
2519 return;
2520 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002521 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002522 if (S.Context.getAsArrayType(T))
2523 T = S.Context.getBaseElementType(T);
2524 if (!T->getAs<RecordType>()) {
2525 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2526 Attr.setInvalid();
2527 return;
2528 }
2529
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002530 if (Attr.getNumArgs() != 1) {
2531 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2532 Attr.setInvalid();
2533 return;
2534 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002535 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002536
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002537 llvm::APSInt priority(32);
2538 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2539 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2541 << "init_priority" << priorityExpr->getSourceRange();
2542 Attr.setInvalid();
2543 return;
2544 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002545 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002546 if (prioritynum < 101 || prioritynum > 65535) {
2547 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2548 << priorityExpr->getSourceRange();
2549 Attr.setInvalid();
2550 return;
2551 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002552 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002553 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002554}
2555
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002556FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2557 int FormatIdx, int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002558 // Check whether we already have an equivalent format attribute.
2559 for (specific_attr_iterator<FormatAttr>
2560 i = D->specific_attr_begin<FormatAttr>(),
2561 e = D->specific_attr_end<FormatAttr>();
2562 i != e ; ++i) {
2563 FormatAttr *f = *i;
2564 if (f->getType() == Format &&
2565 f->getFormatIdx() == FormatIdx &&
2566 f->getFirstArg() == FirstArg) {
2567 // If we don't have a valid location for this attribute, adopt the
2568 // location.
2569 if (f->getLocation().isInvalid())
2570 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002571 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002572 }
2573 }
2574
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002575 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2576 FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00002577}
2578
Mike Stumpd3bb5572009-07-24 19:02:52 +00002579/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2580/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002581static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002582
Chris Lattner4a927cb2008-06-28 23:36:30 +00002583 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002585 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002586 return;
2587 }
2588
Chris Lattner4a927cb2008-06-28 23:36:30 +00002589 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002590 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 return;
2592 }
2593
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002594 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002595 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002596 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002597 return;
2598 }
2599
Chandler Carruth743682b2010-11-16 08:35:43 +00002600 // In C++ the implicit 'this' function parameter also counts, and they are
2601 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002602 bool HasImplicitThisParam = isInstanceMethod(D);
2603 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002604 unsigned FirstIdx = 1;
2605
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002606 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002607
2608 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002609 if (Format.startswith("__") && Format.endswith("__"))
2610 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002611
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002612 // Check for supported formats.
2613 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002614
2615 if (Kind == IgnoredFormat)
2616 return;
2617
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002618 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002620 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621 return;
2622 }
2623
2624 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002625 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002626 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002627 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2628 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002630 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631 return;
2632 }
2633
2634 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002635 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002636 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637 return;
2638 }
2639
2640 // FIXME: Do we need to bounds check?
2641 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002642
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002643 if (HasImplicitThisParam) {
2644 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002645 S.Diag(Attr.getLoc(),
2646 diag::err_format_attribute_implicit_this_format_string)
2647 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002648 return;
2649 }
2650 ArgIdx--;
2651 }
Mike Stump11289f42009-09-09 15:08:12 +00002652
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002654 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002655
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002656 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002657 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002658 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2659 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002660 return;
2661 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002662 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002663 // FIXME: do we need to check if the type is NSString*? What are the
2664 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002665 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002666 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002667 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2668 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002669 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002670 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002671 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002672 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002673 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002674 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2675 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002676 return;
2677 }
2678
2679 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002680 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002681 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002682 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2683 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002685 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002686 return;
2687 }
2688
2689 // check if the function is variadic if the 3rd argument non-zero
2690 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002691 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692 ++NumArgs; // +1 for ...
2693 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002694 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002695 return;
2696 }
2697 }
2698
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002699 // strftime requires FirstArg to be 0 because it doesn't read from any
2700 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002701 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002702 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002703 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2704 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002705 return;
2706 }
2707 // if 0 it disables parameter checking (to use with e.g. va_list)
2708 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002709 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002710 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002711 return;
2712 }
2713
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002714 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
2715 Idx.getZExtValue(),
2716 FirstArg.getZExtValue());
2717 if (NewAttr)
2718 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002719}
2720
Chandler Carruthedc2c642011-07-02 00:01:44 +00002721static void handleTransparentUnionAttr(Sema &S, Decl *D,
2722 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002723 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002724 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002725 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002726
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002727
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002728 // Try to find the underlying union declaration.
2729 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002730 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 if (TD && TD->getUnderlyingType()->isUnionType())
2732 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2733 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002734 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002735
2736 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002737 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002738 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002739 return;
2740 }
2741
John McCallf937c022011-10-07 06:10:15 +00002742 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002743 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002744 diag::warn_transparent_union_attribute_not_definition);
2745 return;
2746 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002748 RecordDecl::field_iterator Field = RD->field_begin(),
2749 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002750 if (Field == FieldEnd) {
2751 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2752 return;
2753 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002754
David Blaikie2d7c57e2012-04-30 02:36:29 +00002755 FieldDecl *FirstField = &*Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002756 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002757 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002758 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002759 diag::warn_transparent_union_attribute_floating)
2760 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002761 return;
2762 }
2763
2764 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2765 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2766 for (; Field != FieldEnd; ++Field) {
2767 QualType FieldType = Field->getType();
2768 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2769 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2770 // Warn if we drop the attribute.
2771 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002772 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002773 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002774 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002775 diag::warn_transparent_union_attribute_field_size_align)
2776 << isSize << Field->getDeclName() << FieldBits;
2777 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002778 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002779 diag::note_transparent_union_first_field_size_align)
2780 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002781 return;
2782 }
2783 }
2784
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002785 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002786}
2787
Chandler Carruthedc2c642011-07-02 00:01:44 +00002788static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002789 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002790 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002791 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002792
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002793 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002794 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002795
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002796 // Make sure that there is a string literal as the annotation's single
2797 // argument.
2798 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002799 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002800 return;
2801 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002802
2803 // Don't duplicate annotations that are already set.
2804 for (specific_attr_iterator<AnnotateAttr>
2805 i = D->specific_attr_begin<AnnotateAttr>(),
2806 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2807 if ((*i)->getAnnotation() == SE->getString())
2808 return;
2809 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002810 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002811 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002812}
2813
Chandler Carruthedc2c642011-07-02 00:01:44 +00002814static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002815 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002816 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818 return;
2819 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002820
2821 //FIXME: The C++0x version of this attribute has more limited applicabilty
2822 // than GNU's, and should error out when it is used to specify a
2823 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002824
Chris Lattner4a927cb2008-06-28 23:36:30 +00002825 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002826 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002827 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002828 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002829
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002830 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002831}
2832
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002833void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002834 // FIXME: Handle pack-expansions here.
2835 if (DiagnoseUnexpandedParameterPack(E))
2836 return;
2837
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002838 if (E->isTypeDependent() || E->isValueDependent()) {
2839 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002840 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002841 return;
2842 }
2843
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002844 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002845 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002846 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002847 ExprResult ICE
2848 = VerifyIntegerConstantExpression(E, &Alignment,
2849 diag::err_aligned_attribute_argument_not_int,
2850 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002851 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002852 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002853 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002854 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2855 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002856 return;
2857 }
2858
Richard Smithf4c51d92012-02-04 09:53:13 +00002859 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002860}
2861
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002862void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002863 // FIXME: Cache the number on the Attr object if non-dependent?
2864 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002865 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002866 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002867}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002868
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002869/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002870/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002871///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002872/// Despite what would be logical, the mode attribute is a decl attribute, not a
2873/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2874/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002875static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002876 // This attribute isn't documented, but glibc uses it. It changes
2877 // the width of an int or unsigned int to the specified size.
2878
2879 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002880 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002881 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002882
Chris Lattneracbc2d22008-06-27 22:18:37 +00002883
2884 IdentifierInfo *Name = Attr.getParameterName();
2885 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002886 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002887 return;
2888 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002889
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002890 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002891
2892 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002893 if (Str.startswith("__") && Str.endswith("__"))
2894 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002895
2896 unsigned DestWidth = 0;
2897 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002898 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002899 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002900 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002901 switch (Str[0]) {
2902 case 'Q': DestWidth = 8; break;
2903 case 'H': DestWidth = 16; break;
2904 case 'S': DestWidth = 32; break;
2905 case 'D': DestWidth = 64; break;
2906 case 'X': DestWidth = 96; break;
2907 case 'T': DestWidth = 128; break;
2908 }
2909 if (Str[1] == 'F') {
2910 IntegerMode = false;
2911 } else if (Str[1] == 'C') {
2912 IntegerMode = false;
2913 ComplexMode = true;
2914 } else if (Str[1] != 'I') {
2915 DestWidth = 0;
2916 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002917 break;
2918 case 4:
2919 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2920 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002921 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002922 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002923 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002924 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002925 break;
2926 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002927 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002928 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002929 break;
2930 }
2931
2932 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002933 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002934 OldTy = TD->getUnderlyingType();
2935 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2936 OldTy = VD->getType();
2937 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002938 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002939 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002940 return;
2941 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002942
John McCall9dd450b2009-09-21 23:43:11 +00002943 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002944 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2945 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002946 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002947 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2948 } else if (ComplexMode) {
2949 if (!OldTy->isComplexType())
2950 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2951 } else {
2952 if (!OldTy->isFloatingType())
2953 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2954 }
2955
Mike Stump87c57ac2009-05-16 07:39:55 +00002956 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2957 // and friends, at least with glibc.
2958 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2959 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002960 // FIXME: Make sure floating-point mappings are accurate
2961 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002962 QualType NewTy;
2963 switch (DestWidth) {
2964 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002965 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966 return;
2967 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002968 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002969 return;
2970 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002971 if (!IntegerMode) {
2972 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2973 return;
2974 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002975 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002976 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002977 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002978 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002979 break;
2980 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002981 if (!IntegerMode) {
2982 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2983 return;
2984 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002985 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002986 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002987 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002988 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002989 break;
2990 case 32:
2991 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002992 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002993 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002994 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002995 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002996 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002997 break;
2998 case 64:
2999 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003000 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003001 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003002 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003003 NewTy = S.Context.LongTy;
3004 else
3005 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003006 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003007 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003008 NewTy = S.Context.UnsignedLongTy;
3009 else
3010 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003011 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003012 case 96:
3013 NewTy = S.Context.LongDoubleTy;
3014 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003015 case 128:
3016 if (!IntegerMode) {
3017 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3018 return;
3019 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003020 if (OldTy->isSignedIntegerType())
3021 NewTy = S.Context.Int128Ty;
3022 else
3023 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003024 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003025 }
3026
Eli Friedman4735374e2009-03-03 06:41:03 +00003027 if (ComplexMode) {
3028 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003029 }
3030
3031 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003032 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003033 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003034 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003035 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003036 cast<ValueDecl>(D)->setType(NewTy);
3037}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003038
Chandler Carruthedc2c642011-07-02 00:01:44 +00003039static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003040 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003041 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003042 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003043
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003044 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003045 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003046 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00003047 return;
3048 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003049
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003050 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003051}
3052
Chandler Carruthedc2c642011-07-02 00:01:44 +00003053static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003054 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003055 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003056 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003057
Mike Stumpd3bb5572009-07-24 19:02:52 +00003058
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003059 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003061 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003062 return;
3063 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003064
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003065 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003066}
3067
Chandler Carruthedc2c642011-07-02 00:01:44 +00003068static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3069 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003070 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003071 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003072 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003073
Chris Lattner3c77a352010-06-22 00:03:40 +00003074
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003075 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003076 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003077 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003078 return;
3079 }
3080
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003081 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003082 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003083}
3084
Chandler Carruthedc2c642011-07-02 00:01:44 +00003085static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003086 if (S.LangOpts.CUDA) {
3087 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003088 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003089 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3090 return;
3091 }
3092
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003093 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003094 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003095 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003096 return;
3097 }
3098
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003099 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003100 } else {
3101 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3102 }
3103}
3104
Chandler Carruthedc2c642011-07-02 00:01:44 +00003105static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003106 if (S.LangOpts.CUDA) {
3107 // check the attribute arguments.
3108 if (Attr.getNumArgs() != 0) {
3109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3110 return;
3111 }
3112
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003113 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003114 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003115 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003116 return;
3117 }
3118
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003119 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003120 } else {
3121 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3122 }
3123}
3124
Chandler Carruthedc2c642011-07-02 00:01:44 +00003125static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003126 if (S.LangOpts.CUDA) {
3127 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003128 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003129 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003130
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003131 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003132 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003133 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003134 return;
3135 }
3136
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003137 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003138 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003139 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003140 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3141 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3142 << FD->getType()
3143 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3144 "void");
3145 } else {
3146 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3147 << FD->getType();
3148 }
3149 return;
3150 }
3151
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003152 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003153 } else {
3154 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3155 }
3156}
3157
Chandler Carruthedc2c642011-07-02 00:01:44 +00003158static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003159 if (S.LangOpts.CUDA) {
3160 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003161 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003162 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003163
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003164
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003165 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003167 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003168 return;
3169 }
3170
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003171 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003172 } else {
3173 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3174 }
3175}
3176
Chandler Carruthedc2c642011-07-02 00:01:44 +00003177static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003178 if (S.LangOpts.CUDA) {
3179 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003180 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003181 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003182
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003183
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003184 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003185 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003186 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003187 return;
3188 }
3189
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003190 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003191 } else {
3192 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3193 }
3194}
3195
Chandler Carruthedc2c642011-07-02 00:01:44 +00003196static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003197 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003198 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003199 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003200
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003201 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003202 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003203 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003204 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003205 return;
3206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003207
Douglas Gregor35b57532009-10-27 21:01:01 +00003208 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003209 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003210 return;
3211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003212
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003213 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003214}
3215
Chandler Carruthedc2c642011-07-02 00:01:44 +00003216static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003217 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003218
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003219 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003220 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3221 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003222 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003223 return;
3224
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003225 if (!isa<ObjCMethodDecl>(D)) {
3226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3227 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003228 return;
3229 }
3230
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003231 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00003232 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003233 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003234 return;
3235 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003236 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003237 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00003238 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003239 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003240 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003241 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003242 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003243 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003244 case AttributeList::AT_pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003245 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003246 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003247 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003248 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003249 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003250 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003251 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003252 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003253 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003254 return;
3255 }
3256
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003257 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003258 PcsAttr::PCSType PCS;
3259 if (StrRef == "aapcs")
3260 PCS = PcsAttr::AAPCS;
3261 else if (StrRef == "aapcs-vfp")
3262 PCS = PcsAttr::AAPCS_VFP;
3263 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003264 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3265 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003266 return;
3267 }
3268
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003269 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003270 }
Abramo Bagnara50099372010-04-30 13:10:51 +00003271 default:
3272 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003273 }
3274}
3275
Chandler Carruthedc2c642011-07-02 00:01:44 +00003276static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003277 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003278 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003279}
3280
John McCall3882ace2011-01-05 12:14:39 +00003281bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3282 if (attr.isInvalid())
3283 return true;
3284
Ted Kremenek1551d552011-04-15 05:49:29 +00003285 if ((attr.getNumArgs() != 0 &&
3286 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3287 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00003288 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3289 attr.setInvalid();
3290 return true;
3291 }
3292
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003293 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3294 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003295 switch (attr.getKind()) {
3296 case AttributeList::AT_cdecl: CC = CC_C; break;
3297 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3298 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3299 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3300 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003301 case AttributeList::AT_pcs: {
3302 Expr *Arg = attr.getArg(0);
3303 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003304 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003305 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3306 << "pcs" << 1;
3307 attr.setInvalid();
3308 return true;
3309 }
3310
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003311 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003312 if (StrRef == "aapcs") {
3313 CC = CC_AAPCS;
3314 break;
3315 } else if (StrRef == "aapcs-vfp") {
3316 CC = CC_AAPCS_VFP;
3317 break;
3318 }
3319 // FALLS THROUGH
3320 }
David Blaikie8a40f702012-01-17 06:56:22 +00003321 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003322 }
3323
3324 return false;
3325}
3326
Chandler Carruthedc2c642011-07-02 00:01:44 +00003327static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003328 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003329
3330 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003331 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003332 return;
3333
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003334 if (!isa<ObjCMethodDecl>(D)) {
3335 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3336 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003337 return;
3338 }
Eli Friedman7044b762009-03-27 21:06:47 +00003339
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003340 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003341}
3342
3343/// Checks a regparm attribute, returning true if it is ill-formed and
3344/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003345bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3346 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003347 return true;
3348
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003349 if (Attr.getNumArgs() != 1) {
3350 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3351 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003352 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003353 }
Eli Friedman7044b762009-03-27 21:06:47 +00003354
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003355 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003356 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003357 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003358 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003359 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003360 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003361 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003362 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003363 }
3364
Douglas Gregore8bbc122011-09-02 00:18:52 +00003365 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003366 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003367 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003368 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003369 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003370 }
3371
John McCall3882ace2011-01-05 12:14:39 +00003372 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003373 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003374 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003375 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003376 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003377 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003378 }
3379
John McCall3882ace2011-01-05 12:14:39 +00003380 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003381}
3382
Chandler Carruthedc2c642011-07-02 00:01:44 +00003383static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003384 if (S.LangOpts.CUDA) {
3385 // check the attribute arguments.
3386 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003387 // FIXME: 0 is not okay.
3388 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003389 return;
3390 }
3391
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003392 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003393 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003394 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003395 return;
3396 }
3397
3398 Expr *MaxThreadsExpr = Attr.getArg(0);
3399 llvm::APSInt MaxThreads(32);
3400 if (MaxThreadsExpr->isTypeDependent() ||
3401 MaxThreadsExpr->isValueDependent() ||
3402 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3403 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3404 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3405 return;
3406 }
3407
3408 llvm::APSInt MinBlocks(32);
3409 if (Attr.getNumArgs() > 1) {
3410 Expr *MinBlocksExpr = Attr.getArg(1);
3411 if (MinBlocksExpr->isTypeDependent() ||
3412 MinBlocksExpr->isValueDependent() ||
3413 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3414 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3415 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3416 return;
3417 }
3418 }
3419
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003420 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003421 MaxThreads.getZExtValue(),
3422 MinBlocks.getZExtValue()));
3423 } else {
3424 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3425 }
3426}
3427
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003428//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003429// Checker-specific attribute handlers.
3430//===----------------------------------------------------------------------===//
3431
John McCalled433932011-01-25 03:31:58 +00003432static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003433 return type->isDependentType() ||
3434 type->isObjCObjectPointerType() ||
3435 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003436}
3437static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003438 return type->isDependentType() ||
3439 type->isPointerType() ||
3440 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003441}
3442
Chandler Carruthedc2c642011-07-02 00:01:44 +00003443static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003444 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003445 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003446 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003447 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003448 return;
3449 }
3450
3451 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00003453 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3454 cf = false;
3455 } else {
3456 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3457 cf = true;
3458 }
3459
3460 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003461 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003462 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003463 return;
3464 }
3465
3466 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003467 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003468 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003469 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003470}
3471
Chandler Carruthedc2c642011-07-02 00:01:44 +00003472static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3473 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003474 if (!isa<ObjCMethodDecl>(D)) {
3475 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003476 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003477 return;
3478 }
3479
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003480 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003481}
3482
Chandler Carruthedc2c642011-07-02 00:01:44 +00003483static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3484 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003485
John McCalled433932011-01-25 03:31:58 +00003486 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003487
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003488 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003489 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003490 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00003491 returnType = PD->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003492 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003493 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00003494 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003495 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003496 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003497 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003498 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003499 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003500 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003501 return;
3502 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003503
John McCalled433932011-01-25 03:31:58 +00003504 bool typeOK;
3505 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003506 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003507 default: llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003508 case AttributeList::AT_ns_returns_autoreleased:
3509 case AttributeList::AT_ns_returns_retained:
3510 case AttributeList::AT_ns_returns_not_retained:
3511 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3512 cf = false;
3513 break;
3514
3515 case AttributeList::AT_cf_returns_retained:
3516 case AttributeList::AT_cf_returns_not_retained:
3517 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3518 cf = true;
3519 break;
3520 }
3521
3522 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003523 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003524 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003525 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003526 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003527
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003528 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003529 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003530 llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003531 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003532 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003533 S.Context));
3534 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003535 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003536 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003537 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003538 return;
3539 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003540 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003541 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003542 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003543 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003544 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003545 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003546 return;
3547 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003548 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003549 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003550 return;
3551 };
3552}
3553
John McCallcf166702011-07-22 08:53:00 +00003554static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3555 const AttributeList &attr) {
3556 SourceLocation loc = attr.getLoc();
3557
3558 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3559
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003560 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003561 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003562 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003563 return;
3564 }
3565
3566 // Check that the method returns a normal pointer.
3567 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003568
3569 if (!resultType->isReferenceType() &&
3570 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003571 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3572 << SourceRange(loc)
3573 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3574
3575 // Drop the attribute.
3576 return;
3577 }
3578
3579 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003580 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003581}
3582
John McCall32f5fe12011-09-30 05:12:12 +00003583/// Handle cf_audited_transfer and cf_unknown_transfer.
3584static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3585 if (!isa<FunctionDecl>(D)) {
3586 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003587 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00003588 return;
3589 }
3590
3591 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3592
3593 // Check whether there's a conflicting attribute already present.
3594 Attr *Existing;
3595 if (IsAudited) {
3596 Existing = D->getAttr<CFUnknownTransferAttr>();
3597 } else {
3598 Existing = D->getAttr<CFAuditedTransferAttr>();
3599 }
3600 if (Existing) {
3601 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3602 << A.getName()
3603 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3604 << A.getRange() << Existing->getRange();
3605 return;
3606 }
3607
3608 // All clear; add the attribute.
3609 if (IsAudited) {
3610 D->addAttr(
3611 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3612 } else {
3613 D->addAttr(
3614 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3615 }
3616}
3617
John McCallf1e8b342011-09-29 07:17:38 +00003618static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3619 const AttributeList &Attr) {
3620 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3621 if (!RD || RD->isUnion()) {
3622 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003623 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003624 }
3625
3626 IdentifierInfo *ParmName = Attr.getParameterName();
3627
3628 // In Objective-C, verify that the type names an Objective-C type.
3629 // We don't want to check this outside of ObjC because people sometimes
3630 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003631 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003632 // Check for an existing type with this name.
3633 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3634 Sema::LookupOrdinaryName);
3635 if (S.LookupName(R, Sc)) {
3636 NamedDecl *Target = R.getFoundDecl();
3637 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3638 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3639 S.Diag(Target->getLocStart(), diag::note_declared_at);
3640 }
3641 }
3642 }
3643
3644 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3645 ParmName));
3646}
3647
Chandler Carruthedc2c642011-07-02 00:01:44 +00003648static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3649 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003650 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003651
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003652 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003653 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003654}
3655
Chandler Carruthedc2c642011-07-02 00:01:44 +00003656static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3657 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003658 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003659 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003660 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003661 return;
3662 }
3663
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003664 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003665 QualType type = vd->getType();
3666
3667 if (!type->isDependentType() &&
3668 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003669 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003670 << type;
3671 return;
3672 }
3673
3674 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3675
3676 // If we have no lifetime yet, check the lifetime we're presumably
3677 // going to infer.
3678 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3679 lifetime = type->getObjCARCImplicitLifetime();
3680
3681 switch (lifetime) {
3682 case Qualifiers::OCL_None:
3683 assert(type->isDependentType() &&
3684 "didn't infer lifetime for non-dependent type?");
3685 break;
3686
3687 case Qualifiers::OCL_Weak: // meaningful
3688 case Qualifiers::OCL_Strong: // meaningful
3689 break;
3690
3691 case Qualifiers::OCL_ExplicitNone:
3692 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003693 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003694 << (lifetime == Qualifiers::OCL_Autoreleasing);
3695 break;
3696 }
3697
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003698 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003699 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003700}
3701
Charles Davis163855f2010-02-16 18:27:26 +00003702static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman0c84ebb2012-02-23 22:46:33 +00003703 switch (Attr.getKind()) {
3704 default:
3705 return false;
3706 case AttributeList::AT_dllimport:
3707 case AttributeList::AT_dllexport:
3708 case AttributeList::AT_uuid:
3709 case AttributeList::AT_deprecated:
3710 case AttributeList::AT_noreturn:
3711 case AttributeList::AT_nothrow:
3712 case AttributeList::AT_naked:
3713 case AttributeList::AT_noinline:
3714 return true;
3715 }
Francois Picheta83957a2010-12-19 06:50:37 +00003716}
3717
3718//===----------------------------------------------------------------------===//
3719// Microsoft specific attribute handlers.
3720//===----------------------------------------------------------------------===//
3721
Chandler Carruthedc2c642011-07-02 00:01:44 +00003722static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00003723 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00003724 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003725 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003726 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003727
Francois Picheta83957a2010-12-19 06:50:37 +00003728 Expr *Arg = Attr.getArg(0);
3729 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003730 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003731 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3732 << "uuid" << 1;
3733 return;
3734 }
3735
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003736 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003737
3738 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3739 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003740
Francois Pichet7da11662010-12-20 01:41:49 +00003741 // Validate GUID length.
3742 if (IsCurly && StrRef.size() != 38) {
3743 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3744 return;
3745 }
3746 if (!IsCurly && StrRef.size() != 36) {
3747 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3748 return;
3749 }
3750
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003751 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00003752 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003753 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003754 if (IsCurly) // Skip the optional '{'
3755 ++I;
3756
3757 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003758 if (i == 8 || i == 13 || i == 18 || i == 23) {
3759 if (*I != '-') {
3760 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3761 return;
3762 }
3763 } else if (!isxdigit(*I)) {
3764 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3765 return;
3766 }
3767 I++;
3768 }
Francois Picheta83957a2010-12-19 06:50:37 +00003769
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003770 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003771 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003772 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003773 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003774}
3775
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003776//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003777// Top Level Sema Entry Points
3778//===----------------------------------------------------------------------===//
3779
Chandler Carruthedc2c642011-07-02 00:01:44 +00003780static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3781 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003782 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003783 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3784 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3785 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003786 default:
3787 break;
3788 }
3789}
Abramo Bagnara50099372010-04-30 13:10:51 +00003790
Chandler Carruthedc2c642011-07-02 00:01:44 +00003791static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3792 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003793 switch (Attr.getKind()) {
Michael Han4a045172012-03-07 00:12:16 +00003794 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3795 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3796 case AttributeList::AT_iboutletcollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003797 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003798 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003799 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003800 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003801 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003802 case AttributeList::AT_neon_vector_type:
3803 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003804 // Ignore these, these are type attributes, handled by
3805 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003806 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003807 case AttributeList::AT_device:
3808 case AttributeList::AT_host:
3809 case AttributeList::AT_overloadable:
3810 // Ignore, this is a non-inheritable attribute, handled
3811 // by ProcessNonInheritableDeclAttr.
3812 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003813 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3814 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003815 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003816 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003817 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003818 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3819 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3820 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003821 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003822 handleDependencyAttr (S, D, Attr); break;
3823 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3824 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3825 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003826 case AttributeList::AT_deprecated:
3827 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
3828 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003829 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003830 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003831 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003832 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003833 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3834 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3835 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3836 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003837 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003838 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003839 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003840 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3841 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3842 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3843 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3844 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003845 case AttributeList::AT_ownership_returns:
3846 case AttributeList::AT_ownership_takes:
3847 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003848 handleOwnershipAttr (S, D, Attr); break;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00003849 case AttributeList::AT_cold: handleColdAttr (S, D, Attr); break;
3850 case AttributeList::AT_hot: handleHotAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003851 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3852 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3853 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3854 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3855 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003856
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003857 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003858 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003859 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003860 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003861
John McCallcf166702011-07-22 08:53:00 +00003862 case AttributeList::AT_objc_returns_inner_pointer:
3863 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3864
John McCallf1e8b342011-09-29 07:17:38 +00003865 case AttributeList::AT_ns_bridged:
3866 handleNSBridgedAttr(S, scope, D, Attr); break;
3867
John McCall32f5fe12011-09-30 05:12:12 +00003868 case AttributeList::AT_cf_audited_transfer:
3869 case AttributeList::AT_cf_unknown_transfer:
3870 handleCFTransferAttr(S, D, Attr); break;
3871
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003872 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003873 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003874 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003875 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003876 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003877
3878 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003879 case AttributeList::AT_ns_returns_not_retained:
3880 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003881 case AttributeList::AT_ns_returns_retained:
3882 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003883 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003884
Michael Han4a045172012-03-07 00:12:16 +00003885 case AttributeList::AT_reqd_work_group_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003886 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003887
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003888 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003889 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003890
Chandler Carruthedc2c642011-07-02 00:01:44 +00003891 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003892 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003893 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003894 case AttributeList::AT_unavailable:
3895 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
3896 break;
Michael Han4a045172012-03-07 00:12:16 +00003897 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003898 handleArcWeakrefUnavailableAttr (S, D, Attr);
3899 break;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00003900 case AttributeList::AT_objc_root_class:
3901 handleObjCRootClassAttr(S, D, Attr);
3902 break;
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00003903 case AttributeList::AT_objc_requires_property_definitions:
3904 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00003905 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003906 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindola70107f92011-10-03 14:59:42 +00003907 case AttributeList::AT_returns_twice:
3908 handleReturnsTwiceAttr(S, D, Attr);
3909 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003910 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3911 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3912 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003913 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003914 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3915 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3916 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003917 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003918 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003919 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003920 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003921 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003922 break;
John McCall86bc21f2011-03-02 11:33:24 +00003923 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003924 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003925 break;
Michael Han4a045172012-03-07 00:12:16 +00003926 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003927 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3928 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3929 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3930 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3931 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3932 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3933 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3934 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003935 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003936 // Just ignore
3937 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003938 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003939 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003940 break;
John McCallab26cfa2010-02-05 21:31:56 +00003941 case AttributeList::AT_stdcall:
3942 case AttributeList::AT_cdecl:
3943 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003944 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003945 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003946 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003947 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003948 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003949 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003950 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003951 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003952 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003953 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003954 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003955
3956 // Thread safety attributes:
3957 case AttributeList::AT_guarded_var:
3958 handleGuardedVarAttr(S, D, Attr);
3959 break;
3960 case AttributeList::AT_pt_guarded_var:
3961 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3962 break;
3963 case AttributeList::AT_scoped_lockable:
3964 handleLockableAttr(S, D, Attr, /*scoped = */true);
3965 break;
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00003966 case AttributeList::AT_no_address_safety_analysis:
3967 handleNoAddressSafetyAttr(S, D, Attr);
3968 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003969 case AttributeList::AT_no_thread_safety_analysis:
3970 handleNoThreadSafetyAttr(S, D, Attr);
3971 break;
3972 case AttributeList::AT_lockable:
3973 handleLockableAttr(S, D, Attr);
3974 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00003975 case AttributeList::AT_guarded_by:
3976 handleGuardedByAttr(S, D, Attr);
3977 break;
3978 case AttributeList::AT_pt_guarded_by:
3979 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3980 break;
3981 case AttributeList::AT_exclusive_lock_function:
3982 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3983 break;
3984 case AttributeList::AT_exclusive_locks_required:
3985 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3986 break;
3987 case AttributeList::AT_exclusive_trylock_function:
3988 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3989 break;
3990 case AttributeList::AT_lock_returned:
3991 handleLockReturnedAttr(S, D, Attr);
3992 break;
3993 case AttributeList::AT_locks_excluded:
3994 handleLocksExcludedAttr(S, D, Attr);
3995 break;
3996 case AttributeList::AT_shared_lock_function:
3997 handleLockFunAttr(S, D, Attr);
3998 break;
3999 case AttributeList::AT_shared_locks_required:
4000 handleLocksRequiredAttr(S, D, Attr);
4001 break;
4002 case AttributeList::AT_shared_trylock_function:
4003 handleTrylockFunAttr(S, D, Attr);
4004 break;
4005 case AttributeList::AT_unlock_function:
4006 handleUnlockFunAttr(S, D, Attr);
4007 break;
4008 case AttributeList::AT_acquired_before:
4009 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
4010 break;
4011 case AttributeList::AT_acquired_after:
4012 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
4013 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004014
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004015 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004016 // Ask target about the attribute.
4017 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4018 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00004019 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
4020 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004021 break;
4022 }
4023}
4024
Peter Collingbourneb331b262011-01-21 02:08:45 +00004025/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4026/// the attribute applies to decls. If the attribute is a type attribute, just
4027/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4028/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00004029static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4030 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004031 bool NonInheritable, bool Inheritable) {
4032 if (Attr.isInvalid())
4033 return;
4034
4035 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
4036 // FIXME: Try to deal with other __declspec attributes!
4037 return;
4038
4039 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004040 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004041
4042 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004043 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004044}
4045
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004046/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4047/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004048void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004049 const AttributeList *AttrList,
4050 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004051 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004052 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004053 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004054
4055 // GCC accepts
4056 // static int a9 __attribute__((weakref));
4057 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004058 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004059 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004060 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004061 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004062 }
4063}
4064
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004065// Annotation attributes are the only attributes allowed after an access
4066// specifier.
4067bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4068 const AttributeList *AttrList) {
4069 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
4070 if (l->getKind() == AttributeList::AT_annotate) {
4071 handleAnnotateAttr(*this, ASDecl, *l);
4072 } else {
4073 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4074 return true;
4075 }
4076 }
4077
4078 return false;
4079}
4080
John McCall42856de2011-10-01 05:17:03 +00004081/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4082/// contains any decl attributes that we should warn about.
4083static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4084 for ( ; A; A = A->getNext()) {
4085 // Only warn if the attribute is an unignored, non-type attribute.
4086 if (A->isUsedAsTypeAttr()) continue;
4087 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4088
4089 if (A->getKind() == AttributeList::UnknownAttribute) {
4090 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4091 << A->getName() << A->getRange();
4092 } else {
4093 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4094 << A->getName() << A->getRange();
4095 }
4096 }
4097}
4098
4099/// checkUnusedDeclAttributes - Given a declarator which is not being
4100/// used to build a declaration, complain about any decl attributes
4101/// which might be lying around on it.
4102void Sema::checkUnusedDeclAttributes(Declarator &D) {
4103 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4104 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4105 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4106 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4107}
4108
Ryan Flynn7d470f32009-07-30 03:15:39 +00004109/// DeclClonePragmaWeak - clone existing decl (maybe definition),
4110/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedmance3e2c82011-09-07 04:05:06 +00004111NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4112 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004113 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004114 NamedDecl *NewD = 0;
4115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004116 FunctionDecl *NewFD;
4117 // FIXME: Missing call to CheckFunctionDeclaration().
4118 // FIXME: Mangling?
4119 // FIXME: Is the qualifier info correct?
4120 // FIXME: Is the DeclContext correct?
4121 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4122 Loc, Loc, DeclarationName(II),
4123 FD->getType(), FD->getTypeSourceInfo(),
4124 SC_None, SC_None,
4125 false/*isInlineSpecified*/,
4126 FD->hasPrototype(),
4127 false/*isConstexprSpecified*/);
4128 NewD = NewFD;
4129
4130 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004131 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004132
4133 // Fake up parameter variables; they are declared as if this were
4134 // a typedef.
4135 QualType FDTy = FD->getType();
4136 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4137 SmallVector<ParmVarDecl*, 16> Params;
4138 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4139 AE = FT->arg_type_end(); AI != AE; ++AI) {
4140 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4141 Param->setScopeInfo(0, Params.size());
4142 Params.push_back(Param);
4143 }
David Blaikie9c70e042011-09-21 18:16:56 +00004144 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004145 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004146 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4147 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004148 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004149 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004150 VD->getStorageClass(),
4151 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004152 if (VD->getQualifier()) {
4153 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004154 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004155 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004156 }
4157 return NewD;
4158}
4159
4160/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
4161/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004162void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004163 if (W.getUsed()) return; // only do this once
4164 W.setUsed(true);
4165 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4166 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004167 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004168 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4169 NDId->getName()));
4170 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004171 WeakTopLevelDecl.push_back(NewD);
4172 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4173 // to insert Decl at TU scope, sorry.
4174 DeclContext *SavedContext = CurContext;
4175 CurContext = Context.getTranslationUnitDecl();
4176 PushOnScopeChains(NewD, S);
4177 CurContext = SavedContext;
4178 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004179 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004180 }
4181}
4182
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004183/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4184/// it, apply them to D. This is a bit tricky because PD can have attributes
4185/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004186void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4187 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004188 // It's valid to "forward-declare" #pragma weak, in which case we
4189 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004190 if (Inheritable) {
4191 LoadExternalWeakUndeclaredIdentifiers();
4192 if (!WeakUndeclaredIdentifiers.empty()) {
4193 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4194 if (IdentifierInfo *Id = ND->getIdentifier()) {
4195 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4196 = WeakUndeclaredIdentifiers.find(Id);
4197 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4198 WeakInfo W = I->second;
4199 DeclApplyPragmaWeak(S, ND, W);
4200 WeakUndeclaredIdentifiers[Id] = W;
4201 }
John McCall6fe02402010-10-27 00:59:00 +00004202 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004203 }
4204 }
4205 }
4206
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004207 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004208 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004209 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004210
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004211 // Walk the declarator structure, applying decl attributes that were in a type
4212 // position to the decl itself. This handles cases like:
4213 // int *__attr__(x)** D;
4214 // when X is a decl attribute.
4215 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4216 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004217 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004218
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004219 // Finally, apply any attributes on the decl itself.
4220 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004221 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004222}
John McCall28a6aea2009-11-04 02:18:39 +00004223
John McCall31168b02011-06-15 23:02:42 +00004224/// Is the given declaration allowed to use a forbidden type?
4225static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4226 // Private ivars are always okay. Unfortunately, people don't
4227 // always properly make their ivars private, even in system headers.
4228 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004229 // Function declarations in sys headers will be marked unavailable.
4230 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4231 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004232 return false;
4233
4234 // Require it to be declared in a system header.
4235 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4236}
4237
4238/// Handle a delayed forbidden-type diagnostic.
4239static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4240 Decl *decl) {
4241 if (decl && isForbiddenTypeAllowed(S, decl)) {
4242 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4243 "this system declaration uses an unsupported type"));
4244 return;
4245 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004246 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004247 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4248 // FIXME. we may want to supress diagnostics for all
4249 // kind of forbidden type messages on unavailable functions.
4250 if (FD->hasAttr<UnavailableAttr>() &&
4251 diag.getForbiddenTypeDiagnostic() ==
4252 diag::err_arc_array_param_no_ownership) {
4253 diag.Triggered = true;
4254 return;
4255 }
4256 }
John McCall31168b02011-06-15 23:02:42 +00004257
4258 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4259 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4260 diag.Triggered = true;
4261}
4262
John McCall2ec85372012-05-07 06:16:41 +00004263void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4264 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004265 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004266 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004267
John McCall2ec85372012-05-07 06:16:41 +00004268 // When delaying diagnostics to run in the context of a parsed
4269 // declaration, we only want to actually emit anything if parsing
4270 // succeeds.
4271 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004272
John McCall2ec85372012-05-07 06:16:41 +00004273 // We emit all the active diagnostics in this pool or any of its
4274 // parents. In general, we'll get one pool for the decl spec
4275 // and a child pool for each declarator; in a decl group like:
4276 // deprecated_typedef foo, *bar, baz();
4277 // only the declarator pops will be passed decls. This is correct;
4278 // we really do need to consider delayed diagnostics from the decl spec
4279 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004280 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004281 do {
John McCall6347b682012-05-07 06:16:58 +00004282 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004283 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4284 // This const_cast is a bit lame. Really, Triggered should be mutable.
4285 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004286 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004287 continue;
4288
John McCallc1465822011-02-14 07:13:47 +00004289 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004290 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004291 // Don't bother giving deprecation diagnostics if the decl is invalid.
4292 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004293 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004294 break;
4295
4296 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004297 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004298 break;
John McCall31168b02011-06-15 23:02:42 +00004299
4300 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004301 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004302 break;
John McCall86121512010-01-27 03:50:35 +00004303 }
4304 }
John McCall2ec85372012-05-07 06:16:41 +00004305 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004306}
4307
John McCall6347b682012-05-07 06:16:58 +00004308/// Given a set of delayed diagnostics, re-emit them as if they had
4309/// been delayed in the current context instead of in the given pool.
4310/// Essentially, this just moves them to the current pool.
4311void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4312 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4313 assert(curPool && "re-emitting in undelayed context not supported");
4314 curPool->steal(pool);
4315}
4316
John McCall28a6aea2009-11-04 02:18:39 +00004317static bool isDeclDeprecated(Decl *D) {
4318 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004319 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004320 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004321 // A category implicitly has the availability of the interface.
4322 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4323 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004324 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4325 return false;
4326}
4327
John McCallb45a1e72010-08-26 02:13:20 +00004328void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004329 Decl *Ctx) {
4330 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004331 return;
4332
John McCall86121512010-01-27 03:50:35 +00004333 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004334 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004335 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004336 << DD.getDeprecationDecl()->getDeclName()
4337 << DD.getDeprecationMessage();
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004338 else if (DD.getUnknownObjCClass()) {
4339 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4340 << DD.getDeprecationDecl()->getDeclName();
4341 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4342 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00004343 else
4344 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004345 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00004346}
4347
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004348void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004349 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004350 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00004351 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004352 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004353 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4354 UnknownObjCClass,
4355 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004356 return;
4357 }
4358
4359 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004360 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004361 return;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004362 if (!Message.empty()) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00004363 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4364 << Message;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004365 Diag(D->getLocation(),
4366 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4367 : diag::note_previous_decl) << D->getDeclName();
4368 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004369 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00004370 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004371 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004372 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004373 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004374 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4375 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004376 }
John McCall28a6aea2009-11-04 02:18:39 +00004377}