blob: 561b624167a7a17d6e3062bff7d69e9348c1d593 [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
Nuno Lopes5c7ad162012-05-24 00:22:00 +0000925static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
926 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
927 return;
928
929 // In C++ the implicit 'this' function parameter also counts, and they are
930 // counted from one.
931 bool HasImplicitThisParam = isInstanceMethod(D);
932 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
933
934 SmallVector<unsigned, 8> SizeArgs;
935
936 for (AttributeList::arg_iterator I = Attr.arg_begin(),
937 E = Attr.arg_end(); I!=E; ++I) {
938 // The argument must be an integer constant expression.
939 Expr *Ex = *I;
940 llvm::APSInt ArgNum;
941 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
942 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
943 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
944 << "alloc_size" << Ex->getSourceRange();
945 return;
946 }
947
948 uint64_t x = ArgNum.getZExtValue();
949
950 if (x < 1 || x > NumArgs) {
951 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
952 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
953 return;
954 }
955
956 --x;
957 if (HasImplicitThisParam) {
958 if (x == 0) {
959 S.Diag(Attr.getLoc(),
960 diag::err_attribute_invalid_implicit_this_argument)
961 << "alloc_size" << Ex->getSourceRange();
962 return;
963 }
964 --x;
965 }
966
967 // check if the function argument is of an integer type
968 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
969 if (!T->isIntegerType()) {
970 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
971 << "alloc_size" << Ex->getSourceRange();
972 return;
973 }
974
Nuno Lopes5c7ad162012-05-24 00:22:00 +0000975 SizeArgs.push_back(x);
976 }
977
978 // check if the function returns a pointer
979 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
980 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
981 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
982 }
983
Nuno Lopese44e93a2012-06-18 16:27:56 +0000984 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
985 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +0000986}
987
Chandler Carruthedc2c642011-07-02 00:01:44 +0000988static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000989 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
990 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000991 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000992 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000993 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000994 return;
995 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000996
Chandler Carruth743682b2010-11-16 08:35:43 +0000997 // In C++ the implicit 'this' function parameter also counts, and they are
998 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000999 bool HasImplicitThisParam = isInstanceMethod(D);
1000 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001001
1002 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001003 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001004
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001005 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1006 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001007
1008
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001009 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001010 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001011 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001012 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1013 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001014 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1015 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001016 return;
1017 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001018
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001019 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001020
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001021 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001023 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001024 return;
1025 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001026
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001027 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001028 if (HasImplicitThisParam) {
1029 if (x == 0) {
1030 S.Diag(Attr.getLoc(),
1031 diag::err_attribute_invalid_implicit_this_argument)
1032 << "nonnull" << Ex->getSourceRange();
1033 return;
1034 }
1035 --x;
1036 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001037
1038 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001039 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001040 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001041
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001042 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001043 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001044 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001045 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001046 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001047 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001048
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001049 NonNullArgs.push_back(x);
1050 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001051
1052 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1053 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001054 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001055 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1056 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001057 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001058 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +00001059 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001060 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001061
Ted Kremenek22813f42010-10-21 18:49:36 +00001062 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001063 if (NonNullArgs.empty()) {
1064 // Warn the trivial case only if attribute is not coming from a
1065 // macro instantiation.
1066 if (Attr.getLoc().isFileID())
1067 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001068 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001069 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001070 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001071
1072 unsigned* start = &NonNullArgs[0];
1073 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001074 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001075 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001076 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001077}
1078
Chandler Carruthedc2c642011-07-02 00:01:44 +00001079static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001080 // This attribute must be applied to a function declaration.
1081 // The first argument to the attribute must be a string,
1082 // the name of the resource, for example "malloc".
1083 // The following arguments must be argument indexes, the arguments must be
1084 // of integer type for Returns, otherwise of pointer type.
1085 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001086 // after being held. free() should be __attribute((ownership_takes)), whereas
1087 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001088
1089 if (!AL.getParameterName()) {
1090 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1091 << AL.getName()->getName() << 1;
1092 return;
1093 }
1094 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001095 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001096 switch (AL.getKind()) {
1097 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001098 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001099 if (AL.getNumArgs() < 1) {
1100 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1101 return;
1102 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001103 break;
1104 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001105 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001106 if (AL.getNumArgs() < 1) {
1107 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1108 return;
1109 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001110 break;
1111 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001112 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001113 if (AL.getNumArgs() > 1) {
1114 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1115 << AL.getNumArgs() + 1;
1116 return;
1117 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001118 break;
1119 default:
1120 // This should never happen given how we are called.
1121 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001122 }
1123
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001124 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001125 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1126 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001127 return;
1128 }
1129
Chandler Carruth743682b2010-11-16 08:35:43 +00001130 // In C++ the implicit 'this' function parameter also counts, and they are
1131 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001132 bool HasImplicitThisParam = isInstanceMethod(D);
1133 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001134
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001135 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001136
1137 // Normalize the argument, __foo__ becomes foo.
1138 if (Module.startswith("__") && Module.endswith("__"))
1139 Module = Module.substr(2, Module.size() - 4);
1140
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001141 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001142
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001143 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1144 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001145
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001146 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001147 llvm::APSInt ArgNum(32);
1148 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1149 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1150 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1151 << AL.getName()->getName() << IdxExpr->getSourceRange();
1152 continue;
1153 }
1154
1155 unsigned x = (unsigned) ArgNum.getZExtValue();
1156
1157 if (x > NumArgs || x < 1) {
1158 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1159 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1160 continue;
1161 }
1162 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001163 if (HasImplicitThisParam) {
1164 if (x == 0) {
1165 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1166 << "ownership" << IdxExpr->getSourceRange();
1167 return;
1168 }
1169 --x;
1170 }
1171
Ted Kremenekd21139a2010-07-31 01:52:11 +00001172 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001173 case OwnershipAttr::Takes:
1174 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001175 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001176 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001177 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1178 // FIXME: Should also highlight argument in decl.
1179 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001180 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001181 << "pointer"
1182 << IdxExpr->getSourceRange();
1183 continue;
1184 }
1185 break;
1186 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001187 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001188 if (AL.getNumArgs() > 1) {
1189 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001190 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001191 llvm::APSInt ArgNum(32);
1192 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1193 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1194 S.Diag(AL.getLoc(), diag::err_ownership_type)
1195 << "ownership_returns" << "integer"
1196 << IdxExpr->getSourceRange();
1197 return;
1198 }
1199 }
1200 break;
1201 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001202 } // switch
1203
1204 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001205 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001206 i = D->specific_attr_begin<OwnershipAttr>(),
1207 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001208 i != e; ++i) {
1209 if ((*i)->getOwnKind() != K) {
1210 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1211 I!=E; ++I) {
1212 if (x == *I) {
1213 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1214 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001215 }
1216 }
1217 }
1218 }
1219 OwnershipArgs.push_back(x);
1220 }
1221
1222 unsigned* start = OwnershipArgs.data();
1223 unsigned size = OwnershipArgs.size();
1224 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001225
1226 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1227 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1228 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001229 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001230
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001231 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001232 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001233}
1234
John McCall7a198ce2011-02-08 22:35:49 +00001235/// Whether this declaration has internal linkage for the purposes of
1236/// things that want to complain about things not have internal linkage.
1237static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1238 switch (D->getLinkage()) {
1239 case NoLinkage:
1240 case InternalLinkage:
1241 return true;
1242
1243 // Template instantiations that go from external to unique-external
1244 // shouldn't get diagnosed.
1245 case UniqueExternalLinkage:
1246 return true;
1247
1248 case ExternalLinkage:
1249 return false;
1250 }
1251 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001252}
1253
Chandler Carruthedc2c642011-07-02 00:01:44 +00001254static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001255 // Check the attribute arguments.
1256 if (Attr.getNumArgs() > 1) {
1257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1258 return;
1259 }
1260
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001261 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001263 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001264 return;
1265 }
1266
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001267 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001268
Rafael Espindolac18086a2010-02-23 22:00:30 +00001269 // gcc rejects
1270 // class c {
1271 // static int a __attribute__((weakref ("v2")));
1272 // static int b() __attribute__((weakref ("f3")));
1273 // };
1274 // and ignores the attributes of
1275 // void f(void) {
1276 // static int a __attribute__((weakref ("v2")));
1277 // }
1278 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001279 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001280 if (!Ctx->isFileContext()) {
1281 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001282 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001283 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001284 }
1285
1286 // The GCC manual says
1287 //
1288 // At present, a declaration to which `weakref' is attached can only
1289 // be `static'.
1290 //
1291 // It also says
1292 //
1293 // Without a TARGET,
1294 // given as an argument to `weakref' or to `alias', `weakref' is
1295 // equivalent to `weak'.
1296 //
1297 // gcc 4.4.1 will accept
1298 // int a7 __attribute__((weakref));
1299 // as
1300 // int a7 __attribute__((weak));
1301 // This looks like a bug in gcc. We reject that for now. We should revisit
1302 // it if this behaviour is actually used.
1303
John McCall7a198ce2011-02-08 22:35:49 +00001304 if (!hasEffectivelyInternalLinkage(nd)) {
1305 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001306 return;
1307 }
1308
1309 // GCC rejects
1310 // static ((alias ("y"), weakref)).
1311 // Should we? How to check that weakref is before or after alias?
1312
1313 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001314 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001315 Arg = Arg->IgnoreParenCasts();
1316 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1317
Douglas Gregorfb65e592011-07-27 05:40:30 +00001318 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1320 << "weakref" << 1;
1321 return;
1322 }
1323 // GCC will accept anything as the argument of weakref. Should we
1324 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001325 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001326 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001327 }
1328
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001329 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001330}
1331
Chandler Carruthedc2c642011-07-02 00:01:44 +00001332static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001333 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001334 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001336 return;
1337 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001338
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001339 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001340 Arg = Arg->IgnoreParenCasts();
1341 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001342
Douglas Gregorfb65e592011-07-27 05:40:30 +00001343 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001345 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001346 return;
1347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001348
Douglas Gregore8bbc122011-09-02 00:18:52 +00001349 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001350 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1351 return;
1352 }
1353
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001354 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001355
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001356 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001357 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001358}
1359
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001360static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1361 // Check the attribute arguments.
1362 if (!checkAttributeNumArgs(S, Attr, 0))
1363 return;
1364
1365 if (!isa<FunctionDecl>(D)) {
1366 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1367 << Attr.getName() << ExpectedFunction;
1368 return;
1369 }
1370
1371 if (D->hasAttr<HotAttr>()) {
1372 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1373 << Attr.getName() << "hot";
1374 return;
1375 }
1376
1377 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1378}
1379
1380static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1381 // Check the attribute arguments.
1382 if (!checkAttributeNumArgs(S, Attr, 0))
1383 return;
1384
1385 if (!isa<FunctionDecl>(D)) {
1386 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1387 << Attr.getName() << ExpectedFunction;
1388 return;
1389 }
1390
1391 if (D->hasAttr<ColdAttr>()) {
1392 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1393 << Attr.getName() << "cold";
1394 return;
1395 }
1396
1397 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1398}
1399
Chandler Carruthedc2c642011-07-02 00:01:44 +00001400static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001401 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001402 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001403 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001404
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001405 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001406 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001407 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001408 return;
1409 }
1410
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001411 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001412}
1413
Chandler Carruthedc2c642011-07-02 00:01:44 +00001414static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1415 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001416 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001417 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001418 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1419 return;
1420 }
1421
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001422 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001423 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001424 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001425 return;
1426 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001427
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001428 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001429}
1430
Chandler Carruthedc2c642011-07-02 00:01:44 +00001431static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001432 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001433 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1435 return;
1436 }
Mike Stump11289f42009-09-09 15:08:12 +00001437
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001438 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001439 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001440 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001441 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001442 return;
1443 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001444 }
1445
Ted Kremenek08479ae2009-08-15 00:51:46 +00001446 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001447}
1448
Chandler Carruthedc2c642011-07-02 00:01:44 +00001449static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001450 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001451 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001452 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001453
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001454 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001455}
1456
Chandler Carruthedc2c642011-07-02 00:01:44 +00001457static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001458 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001459 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001460 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001461 else
1462 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001463 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001464}
1465
Chandler Carruthedc2c642011-07-02 00:01:44 +00001466static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001467 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001468 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001469 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001470 else
1471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001472 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001473}
1474
Chandler Carruthedc2c642011-07-02 00:01:44 +00001475static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001476 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001477
1478 if (S.CheckNoReturnAttr(attr)) return;
1479
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001480 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001481 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001482 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001483 return;
1484 }
1485
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001486 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001487}
1488
1489bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001490 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001491 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1492 attr.setInvalid();
1493 return true;
1494 }
1495
1496 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001497}
1498
Chandler Carruthedc2c642011-07-02 00:01:44 +00001499static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1500 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001501
1502 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1503 // because 'analyzer_noreturn' does not impact the type.
1504
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001505 if(!checkAttributeNumArgs(S, Attr, 0))
1506 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001507
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001508 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1509 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001510 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1511 && !VD->getType()->isFunctionPointerType())) {
1512 S.Diag(Attr.getLoc(),
1513 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1514 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001515 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001516 return;
1517 }
1518 }
1519
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001520 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001521}
1522
John Thompsoncdb847ba2010-08-09 21:53:52 +00001523// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001524static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001525/*
1526 Returning a Vector Class in Registers
1527
Eric Christopherbc638a82010-12-01 22:13:54 +00001528 According to the PPU ABI specifications, a class with a single member of
1529 vector type is returned in memory when used as the return value of a function.
1530 This results in inefficient code when implementing vector classes. To return
1531 the value in a single vector register, add the vecreturn attribute to the
1532 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001533
1534 Example:
1535
1536 struct Vector
1537 {
1538 __vector float xyzw;
1539 } __attribute__((vecreturn));
1540
1541 Vector Add(Vector lhs, Vector rhs)
1542 {
1543 Vector result;
1544 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1545 return result; // This will be returned in a register
1546 }
1547*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001548 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001550 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001551 return;
1552 }
1553
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001554 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001555 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1556 return;
1557 }
1558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001559 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001560 int count = 0;
1561
1562 if (!isa<CXXRecordDecl>(record)) {
1563 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1564 return;
1565 }
1566
1567 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1568 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1569 return;
1570 }
1571
Eric Christopherbc638a82010-12-01 22:13:54 +00001572 for (RecordDecl::field_iterator iter = record->field_begin();
1573 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001574 if ((count == 1) || !iter->getType()->isVectorType()) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1576 return;
1577 }
1578 count++;
1579 }
1580
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001581 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001582}
1583
Chandler Carruthedc2c642011-07-02 00:01:44 +00001584static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001585 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001587 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001588 return;
1589 }
1590 // FIXME: Actually store the attribute on the declaration
1591}
1592
Chandler Carruthedc2c642011-07-02 00:01:44 +00001593static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001594 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001595 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001597 return;
1598 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001599
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001600 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001601 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001602 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001603 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001604 return;
1605 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001606
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001607 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001608}
1609
Rafael Espindola70107f92011-10-03 14:59:42 +00001610static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1611 const AttributeList &Attr) {
1612 // check the attribute arguments.
1613 if (Attr.hasParameterOrArguments()) {
1614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1615 return;
1616 }
1617
1618 if (!isa<FunctionDecl>(D)) {
1619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1620 << Attr.getName() << ExpectedFunction;
1621 return;
1622 }
1623
1624 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1625}
1626
Chandler Carruthedc2c642011-07-02 00:01:44 +00001627static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001628 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001629 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001630 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1631 return;
1632 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001633
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001634 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001635 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001636 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1637 return;
1638 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001639 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001641 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001642 return;
1643 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001644
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001645 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001646}
1647
Chandler Carruthedc2c642011-07-02 00:01:44 +00001648static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001649 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001650 if (Attr.getNumArgs() > 1) {
1651 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001652 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001653 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001654
1655 int priority = 65535; // FIXME: Do not hardcode such constants.
1656 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001657 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001658 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001659 if (E->isTypeDependent() || E->isValueDependent() ||
1660 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001662 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001663 return;
1664 }
1665 priority = Idx.getZExtValue();
1666 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001667
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001668 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001670 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001671 return;
1672 }
1673
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001674 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001675 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001676}
1677
Chandler Carruthedc2c642011-07-02 00:01:44 +00001678static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001679 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001680 if (Attr.getNumArgs() > 1) {
1681 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001682 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001683 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001684
1685 int priority = 65535; // FIXME: Do not hardcode such constants.
1686 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001687 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001688 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001689 if (E->isTypeDependent() || E->isValueDependent() ||
1690 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001691 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001692 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001693 return;
1694 }
1695 priority = Idx.getZExtValue();
1696 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001697
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001698 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001700 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001701 return;
1702 }
1703
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001704 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001705 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001706}
1707
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001708template <typename AttrTy>
1709static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1710 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001711 unsigned NumArgs = Attr.getNumArgs();
1712 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001713 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001714 return;
1715 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001716
1717 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001718 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001719 if (NumArgs == 1) {
1720 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001721 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001722 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001723 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001724 return;
1725 }
Chris Lattner190aa102011-02-24 05:42:24 +00001726 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001727 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001728
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001729 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001730}
1731
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001732static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1733 const AttributeList &Attr) {
1734 unsigned NumArgs = Attr.getNumArgs();
1735 if (NumArgs > 0) {
1736 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1737 return;
1738 }
1739
1740 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001741 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001742}
1743
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001744static void handleObjCRootClassAttr(Sema &S, Decl *D,
1745 const AttributeList &Attr) {
1746 if (!isa<ObjCInterfaceDecl>(D)) {
1747 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1748 return;
1749 }
1750
1751 unsigned NumArgs = Attr.getNumArgs();
1752 if (NumArgs > 0) {
1753 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1754 return;
1755 }
1756
1757 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1758}
1759
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001760static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001761 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001762 if (!isa<ObjCInterfaceDecl>(D)) {
1763 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1764 return;
1765 }
1766
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001767 unsigned NumArgs = Attr.getNumArgs();
1768 if (NumArgs > 0) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1770 return;
1771 }
1772
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001773 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001774 Attr.getRange(), S.Context));
1775}
1776
Jordy Rose740b0c22012-05-08 03:27:22 +00001777static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1778 IdentifierInfo *Platform,
1779 VersionTuple Introduced,
1780 VersionTuple Deprecated,
1781 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001782 StringRef PlatformName
1783 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1784 if (PlatformName.empty())
1785 PlatformName = Platform->getName();
1786
1787 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1788 // of these steps are needed).
1789 if (!Introduced.empty() && !Deprecated.empty() &&
1790 !(Introduced <= Deprecated)) {
1791 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1792 << 1 << PlatformName << Deprecated.getAsString()
1793 << 0 << Introduced.getAsString();
1794 return true;
1795 }
1796
1797 if (!Introduced.empty() && !Obsoleted.empty() &&
1798 !(Introduced <= Obsoleted)) {
1799 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1800 << 2 << PlatformName << Obsoleted.getAsString()
1801 << 0 << Introduced.getAsString();
1802 return true;
1803 }
1804
1805 if (!Deprecated.empty() && !Obsoleted.empty() &&
1806 !(Deprecated <= Obsoleted)) {
1807 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1808 << 2 << PlatformName << Obsoleted.getAsString()
1809 << 1 << Deprecated.getAsString();
1810 return true;
1811 }
1812
1813 return false;
1814}
1815
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001816AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1817 IdentifierInfo *Platform,
1818 VersionTuple Introduced,
1819 VersionTuple Deprecated,
1820 VersionTuple Obsoleted,
1821 bool IsUnavailable,
1822 StringRef Message) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001823 VersionTuple MergedIntroduced = Introduced;
1824 VersionTuple MergedDeprecated = Deprecated;
1825 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001826 bool FoundAny = false;
1827
Rafael Espindolac67f2232012-05-10 02:50:16 +00001828 if (D->hasAttrs()) {
1829 AttrVec &Attrs = D->getAttrs();
1830 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1831 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1832 if (!OldAA) {
1833 ++i;
1834 continue;
1835 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001836
Rafael Espindolac67f2232012-05-10 02:50:16 +00001837 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1838 if (OldPlatform != Platform) {
1839 ++i;
1840 continue;
1841 }
1842
1843 FoundAny = true;
1844 VersionTuple OldIntroduced = OldAA->getIntroduced();
1845 VersionTuple OldDeprecated = OldAA->getDeprecated();
1846 VersionTuple OldObsoleted = OldAA->getObsoleted();
1847 bool OldIsUnavailable = OldAA->getUnavailable();
1848 StringRef OldMessage = OldAA->getMessage();
1849
1850 if ((!OldIntroduced.empty() && !Introduced.empty() &&
1851 OldIntroduced != Introduced) ||
1852 (!OldDeprecated.empty() && !Deprecated.empty() &&
1853 OldDeprecated != Deprecated) ||
1854 (!OldObsoleted.empty() && !Obsoleted.empty() &&
1855 OldObsoleted != Obsoleted) ||
1856 (OldIsUnavailable != IsUnavailable) ||
1857 (OldMessage != Message)) {
1858 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1859 Diag(Range.getBegin(), diag::note_previous_attribute);
1860 Attrs.erase(Attrs.begin() + i);
1861 --e;
1862 continue;
1863 }
1864
1865 VersionTuple MergedIntroduced2 = MergedIntroduced;
1866 VersionTuple MergedDeprecated2 = MergedDeprecated;
1867 VersionTuple MergedObsoleted2 = MergedObsoleted;
1868
1869 if (MergedIntroduced2.empty())
1870 MergedIntroduced2 = OldIntroduced;
1871 if (MergedDeprecated2.empty())
1872 MergedDeprecated2 = OldDeprecated;
1873 if (MergedObsoleted2.empty())
1874 MergedObsoleted2 = OldObsoleted;
1875
1876 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1877 MergedIntroduced2, MergedDeprecated2,
1878 MergedObsoleted2)) {
1879 Attrs.erase(Attrs.begin() + i);
1880 --e;
1881 continue;
1882 }
1883
1884 MergedIntroduced = MergedIntroduced2;
1885 MergedDeprecated = MergedDeprecated2;
1886 MergedObsoleted = MergedObsoleted2;
1887 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001888 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001889 }
1890
1891 if (FoundAny &&
1892 MergedIntroduced == Introduced &&
1893 MergedDeprecated == Deprecated &&
1894 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001895 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001896
Rafael Espindolac67f2232012-05-10 02:50:16 +00001897 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001898 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001899 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1900 Introduced, Deprecated,
1901 Obsoleted, IsUnavailable, Message);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001902 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001903 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001904}
1905
Chandler Carruthedc2c642011-07-02 00:01:44 +00001906static void handleAvailabilityAttr(Sema &S, Decl *D,
1907 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001908 IdentifierInfo *Platform = Attr.getParameterName();
1909 SourceLocation PlatformLoc = Attr.getParameterLoc();
1910
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001911 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001912 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1913 << Platform;
1914
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001915 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1916 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1917 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001918 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001919 StringRef Str;
1920 const StringLiteral *SE =
1921 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1922 if (SE)
1923 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001925 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
1926 Platform,
1927 Introduced.Version,
1928 Deprecated.Version,
1929 Obsoleted.Version,
1930 IsUnavailable, Str);
1931 if (NewAttr)
1932 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001933}
1934
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001935VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
1936 VisibilityAttr::VisibilityType Vis) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00001937 if (isa<TypedefNameDecl>(D)) {
1938 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001939 return NULL;
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00001940 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00001941 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
1942 if (ExistingAttr) {
1943 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
1944 if (ExistingVis == Vis)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001945 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +00001946 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
1947 Diag(Range.getBegin(), diag::note_previous_attribute);
1948 D->dropAttr<VisibilityAttr>();
1949 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001950 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001951}
1952
Chandler Carruthedc2c642011-07-02 00:01:44 +00001953static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001954 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001955 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001956 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001957
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001958 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001959 Arg = Arg->IgnoreParenCasts();
1960 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001961
Douglas Gregorfb65e592011-07-27 05:40:30 +00001962 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001964 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001965 return;
1966 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001967
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001968 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001969 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001970
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001971 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001972 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001973 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001974 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001975 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001976 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00001977 else if (TypeStr == "protected") {
1978 // Complain about attempts to use protected visibility on targets
1979 // (like Darwin) that don't support it.
1980 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1981 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1982 type = VisibilityAttr::Default;
1983 } else {
1984 type = VisibilityAttr::Protected;
1985 }
1986 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001987 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001988 return;
1989 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001990
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001991 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
1992 if (NewAttr)
1993 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001994}
1995
Chandler Carruthedc2c642011-07-02 00:01:44 +00001996static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1997 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001998 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1999 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002000 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002001 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002002 return;
2003 }
2004
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002005 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2006 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2007 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002008 << "objc_method_family" << 1;
2009 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002010 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002011 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002012 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002013 return;
2014 }
2015
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002016 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002017 ObjCMethodFamilyAttr::FamilyKind family;
2018 if (param == "none")
2019 family = ObjCMethodFamilyAttr::OMF_None;
2020 else if (param == "alloc")
2021 family = ObjCMethodFamilyAttr::OMF_alloc;
2022 else if (param == "copy")
2023 family = ObjCMethodFamilyAttr::OMF_copy;
2024 else if (param == "init")
2025 family = ObjCMethodFamilyAttr::OMF_init;
2026 else if (param == "mutableCopy")
2027 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2028 else if (param == "new")
2029 family = ObjCMethodFamilyAttr::OMF_new;
2030 else {
2031 // Just warn and ignore it. This is future-proof against new
2032 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002033 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002034 return;
2035 }
2036
John McCall31168b02011-06-15 23:02:42 +00002037 if (family == ObjCMethodFamilyAttr::OMF_init &&
2038 !method->getResultType()->isObjCObjectPointerType()) {
2039 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2040 << method->getResultType();
2041 // Ignore the attribute.
2042 return;
2043 }
2044
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002045 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002046 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002047}
2048
Chandler Carruthedc2c642011-07-02 00:01:44 +00002049static void handleObjCExceptionAttr(Sema &S, Decl *D,
2050 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002051 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002052 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002053
Chris Lattner677a3582009-02-14 08:09:34 +00002054 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2055 if (OCI == 0) {
2056 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2057 return;
2058 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002059
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002060 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00002061}
2062
Chandler Carruthedc2c642011-07-02 00:01:44 +00002063static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002064 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002065 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002066 return;
2067 }
Richard Smithdda56e42011-04-15 14:24:37 +00002068 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002069 QualType T = TD->getUnderlyingType();
2070 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002071 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002072 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2073 return;
2074 }
2075 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002076 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2077 QualType T = PD->getType();
2078 if (!T->isPointerType() ||
2079 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
2080 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2081 return;
2082 }
2083 }
2084 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002085 // It is okay to include this attribute on properties, e.g.:
2086 //
2087 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2088 //
2089 // In this case it follows tradition and suppresses an error in the above
2090 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002091 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002092 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002093 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002094}
2095
Mike Stumpd3bb5572009-07-24 19:02:52 +00002096static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002097handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002098 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002099 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002100 return;
2101 }
2102
2103 if (!isa<FunctionDecl>(D)) {
2104 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2105 return;
2106 }
2107
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002108 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002109}
2110
Chandler Carruthedc2c642011-07-02 00:01:44 +00002111static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002112 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002113 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002114 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002115 return;
2116 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002117
Steve Naroff3405a732008-09-18 16:44:58 +00002118 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002119 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002120 return;
2121 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002122
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002123 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002124 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002125 type = BlocksAttr::ByRef;
2126 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002127 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002128 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002129 return;
2130 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002131
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002132 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002133}
2134
Chandler Carruthedc2c642011-07-02 00:01:44 +00002135static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 // check the attribute arguments.
2137 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002138 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002139 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002140 }
2141
John McCallb46f2872011-09-09 07:56:05 +00002142 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002143 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002144 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002145 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002146 if (E->isTypeDependent() || E->isValueDependent() ||
2147 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002149 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002150 return;
2151 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002152
John McCallb46f2872011-09-09 07:56:05 +00002153 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002154 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2155 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002156 return;
2157 }
John McCallb46f2872011-09-09 07:56:05 +00002158
2159 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002160 }
2161
John McCallb46f2872011-09-09 07:56:05 +00002162 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002163 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002164 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002165 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002166 if (E->isTypeDependent() || E->isValueDependent() ||
2167 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002169 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002170 return;
2171 }
2172 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002173
John McCallb46f2872011-09-09 07:56:05 +00002174 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002175 // FIXME: This error message could be improved, it would be nice
2176 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002177 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2178 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002179 return;
2180 }
2181 }
2182
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002183 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002184 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002185 if (isa<FunctionNoProtoType>(FT)) {
2186 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2187 return;
2188 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002189
Chris Lattner9363e312009-03-17 23:03:47 +00002190 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002191 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002192 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002193 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002194 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002195 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002196 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002197 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002198 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002199 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2200 if (!BD->isVariadic()) {
2201 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2202 return;
2203 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002204 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002205 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002206 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002207 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002208 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002209 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002210 int m = Ty->isFunctionPointerType() ? 0 : 1;
2211 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002212 return;
2213 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002214 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002215 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002216 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002217 return;
2218 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002219 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002220 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002221 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002222 return;
2223 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002224 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002225 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002226}
2227
Chandler Carruthedc2c642011-07-02 00:01:44 +00002228static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002229 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002230 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002231 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002232
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002233 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002234 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002235 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002236 return;
2237 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002238
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002239 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2240 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2241 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002242 return;
2243 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002244 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2245 if (MD->getResultType()->isVoidType()) {
2246 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2247 << Attr.getName() << 1;
2248 return;
2249 }
2250
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002251 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002252}
2253
Chandler Carruthedc2c642011-07-02 00:01:44 +00002254static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002255 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002256 if (Attr.hasParameterOrArguments()) {
2257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002258 return;
2259 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002260
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002261 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002262 if (isa<CXXRecordDecl>(D)) {
2263 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2264 return;
2265 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2267 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002268 return;
2269 }
2270
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002271 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002272
2273 // 'weak' only applies to declarations with external linkage.
2274 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002275 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002276 return;
2277 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002278
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002279 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002280}
2281
Chandler Carruthedc2c642011-07-02 00:01:44 +00002282static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002283 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002284 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002285 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002286
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002287
2288 // weak_import only applies to variable & function declarations.
2289 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002290 if (!D->canBeWeakImported(isDef)) {
2291 if (isDef)
2292 S.Diag(Attr.getLoc(),
2293 diag::warn_attribute_weak_import_invalid_on_definition)
2294 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002295 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002296 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002297 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002298 // Nothing to warn about here.
2299 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002301 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002302
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002303 return;
2304 }
2305
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002306 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002307}
2308
Chandler Carruthedc2c642011-07-02 00:01:44 +00002309static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2310 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002311 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002312 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00002313 return;
Nate Begemanf2758702009-06-26 06:32:41 +00002314
2315 unsigned WGSize[3];
2316 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002317 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002318 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002319 if (E->isTypeDependent() || E->isValueDependent() ||
2320 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2322 << "reqd_work_group_size" << E->getSourceRange();
2323 return;
2324 }
2325 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2326 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002327 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002328 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00002329 WGSize[2]));
2330}
2331
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002332SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2333 StringRef Name) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002334 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2335 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002336 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002337 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2338 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002339 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002340 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002341 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002342}
2343
Chandler Carruthedc2c642011-07-02 00:01:44 +00002344static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002345 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002346 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002347 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002348
2349 // Make sure that there is a string literal as the sections's single
2350 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002351 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002352 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002353 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002354 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002355 return;
2356 }
Mike Stump11289f42009-09-09 15:08:12 +00002357
Chris Lattner30ba6742009-08-10 19:03:04 +00002358 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002359 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002360 if (!Error.empty()) {
2361 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2362 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002363 return;
2364 }
Mike Stump11289f42009-09-09 15:08:12 +00002365
Chris Lattner20aee9b2010-01-12 20:58:53 +00002366 // This attribute cannot be applied to local variables.
2367 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2368 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2369 return;
2370 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002371 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2372 SE->getString());
2373 if (NewAttr)
2374 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002375}
2376
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002377
Chandler Carruthedc2c642011-07-02 00:01:44 +00002378static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002379 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002380 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002381 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002382 return;
2383 }
Douglas Gregor88336832011-06-15 05:45:11 +00002384
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002385 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002386 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002387 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002388 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002389 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002390 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002391}
2392
Chandler Carruthedc2c642011-07-02 00:01:44 +00002393static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002394 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002395 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002396 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002397 return;
2398 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002399
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002400 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002401 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002402 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002403 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002404 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002405 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002406}
2407
Chandler Carruthedc2c642011-07-02 00:01:44 +00002408static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002409 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002410 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002411 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002412
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002413 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002414}
2415
Chandler Carruthedc2c642011-07-02 00:01:44 +00002416static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002417 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002418 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2419 return;
2420 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002421
Anders Carlssond277d792009-01-31 01:16:18 +00002422 if (Attr.getNumArgs() != 0) {
2423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2424 return;
2425 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002426
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002427 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002428
Anders Carlssond277d792009-01-31 01:16:18 +00002429 if (!VD || !VD->hasLocalStorage()) {
2430 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2431 return;
2432 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002433
Anders Carlssond277d792009-01-31 01:16:18 +00002434 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002435 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002436 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002437 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2438 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002439 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002440 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002441 Attr.getParameterName();
2442 return;
2443 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002444
Anders Carlssond277d792009-01-31 01:16:18 +00002445 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2446 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002447 S.Diag(Attr.getParameterLoc(),
2448 diag::err_attribute_cleanup_arg_not_function)
2449 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002450 return;
2451 }
2452
Anders Carlssond277d792009-01-31 01:16:18 +00002453 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002454 S.Diag(Attr.getParameterLoc(),
2455 diag::err_attribute_cleanup_func_must_take_one_arg)
2456 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002457 return;
2458 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459
Anders Carlsson723f55d2009-02-07 23:16:50 +00002460 // We're currently more strict than GCC about what function types we accept.
2461 // If this ever proves to be a problem it should be easy to fix.
2462 QualType Ty = S.Context.getPointerType(VD->getType());
2463 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002464 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2465 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002466 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002467 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2468 Attr.getParameterName() << ParamTy << Ty;
2469 return;
2470 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002471
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002472 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002473 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002474}
2475
Mike Stumpd3bb5572009-07-24 19:02:52 +00002476/// Handle __attribute__((format_arg((idx)))) attribute based on
2477/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002478static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002479 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002480 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002481
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002482 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002484 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002485 return;
2486 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002487
2488 // In C++ the implicit 'this' function parameter also counts, and they are
2489 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002490 bool HasImplicitThisParam = isInstanceMethod(D);
2491 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002492 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002493
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002494 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002495 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002496 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002497 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2498 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2500 << "format" << 2 << IdxExpr->getSourceRange();
2501 return;
2502 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002503
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002504 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2506 << "format" << 2 << IdxExpr->getSourceRange();
2507 return;
2508 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002509
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002510 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002511
Chandler Carruth743682b2010-11-16 08:35:43 +00002512 if (HasImplicitThisParam) {
2513 if (ArgIdx == 0) {
2514 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2515 << "format_arg" << IdxExpr->getSourceRange();
2516 return;
2517 }
2518 ArgIdx--;
2519 }
2520
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002521 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002522 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002523
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002524 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2525 if (not_nsstring_type &&
2526 !isCFStringType(Ty, S.Context) &&
2527 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002528 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002529 // FIXME: Should highlight the actual expression that has the wrong type.
2530 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002531 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002532 << IdxExpr->getSourceRange();
2533 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002534 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002535 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002536 if (!isNSStringType(Ty, S.Context) &&
2537 !isCFStringType(Ty, S.Context) &&
2538 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002539 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002540 // FIXME: Should highlight the actual expression that has the wrong type.
2541 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002542 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002543 << IdxExpr->getSourceRange();
2544 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002545 }
2546
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002547 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002548 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002549}
2550
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002551enum FormatAttrKind {
2552 CFStringFormat,
2553 NSStringFormat,
2554 StrftimeFormat,
2555 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002556 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002557 InvalidFormat
2558};
2559
2560/// getFormatAttrKind - Map from format attribute names to supported format
2561/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002562static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002563 return llvm::StringSwitch<FormatAttrKind>(Format)
2564 // Check for formats that get handled specially.
2565 .Case("NSString", NSStringFormat)
2566 .Case("CFString", CFStringFormat)
2567 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002568
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002569 // Otherwise, check for supported formats.
2570 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2571 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2572 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002573
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002574 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2575 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002576}
2577
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002578/// Handle __attribute__((init_priority(priority))) attributes based on
2579/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002580static void handleInitPriorityAttr(Sema &S, Decl *D,
2581 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002582 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002583 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2584 return;
2585 }
2586
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002587 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002588 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2589 Attr.setInvalid();
2590 return;
2591 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002592 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002593 if (S.Context.getAsArrayType(T))
2594 T = S.Context.getBaseElementType(T);
2595 if (!T->getAs<RecordType>()) {
2596 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2597 Attr.setInvalid();
2598 return;
2599 }
2600
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002601 if (Attr.getNumArgs() != 1) {
2602 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2603 Attr.setInvalid();
2604 return;
2605 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002606 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002607
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002608 llvm::APSInt priority(32);
2609 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2610 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2612 << "init_priority" << priorityExpr->getSourceRange();
2613 Attr.setInvalid();
2614 return;
2615 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002616 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002617 if (prioritynum < 101 || prioritynum > 65535) {
2618 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2619 << priorityExpr->getSourceRange();
2620 Attr.setInvalid();
2621 return;
2622 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002623 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002624 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002625}
2626
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002627FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2628 int FormatIdx, int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002629 // Check whether we already have an equivalent format attribute.
2630 for (specific_attr_iterator<FormatAttr>
2631 i = D->specific_attr_begin<FormatAttr>(),
2632 e = D->specific_attr_end<FormatAttr>();
2633 i != e ; ++i) {
2634 FormatAttr *f = *i;
2635 if (f->getType() == Format &&
2636 f->getFormatIdx() == FormatIdx &&
2637 f->getFirstArg() == FirstArg) {
2638 // If we don't have a valid location for this attribute, adopt the
2639 // location.
2640 if (f->getLocation().isInvalid())
2641 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002642 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002643 }
2644 }
2645
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002646 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2647 FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00002648}
2649
Mike Stumpd3bb5572009-07-24 19:02:52 +00002650/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2651/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002652static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653
Chris Lattner4a927cb2008-06-28 23:36:30 +00002654 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002655 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002656 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 return;
2658 }
2659
Chris Lattner4a927cb2008-06-28 23:36:30 +00002660 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662 return;
2663 }
2664
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002665 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002667 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002668 return;
2669 }
2670
Chandler Carruth743682b2010-11-16 08:35:43 +00002671 // In C++ the implicit 'this' function parameter also counts, and they are
2672 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002673 bool HasImplicitThisParam = isInstanceMethod(D);
2674 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675 unsigned FirstIdx = 1;
2676
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002677 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002678
2679 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002680 if (Format.startswith("__") && Format.endswith("__"))
2681 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002682
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002683 // Check for supported formats.
2684 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002685
2686 if (Kind == IgnoredFormat)
2687 return;
2688
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002689 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002690 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002691 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692 return;
2693 }
2694
2695 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002696 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002697 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002698 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2699 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002700 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002701 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002702 return;
2703 }
2704
2705 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002706 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002707 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002708 return;
2709 }
2710
2711 // FIXME: Do we need to bounds check?
2712 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002713
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002714 if (HasImplicitThisParam) {
2715 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002716 S.Diag(Attr.getLoc(),
2717 diag::err_format_attribute_implicit_this_format_string)
2718 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002719 return;
2720 }
2721 ArgIdx--;
2722 }
Mike Stump11289f42009-09-09 15:08:12 +00002723
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002724 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002725 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002726
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002727 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002728 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002729 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2730 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002731 return;
2732 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002733 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002734 // FIXME: do we need to check if the type is NSString*? What are the
2735 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002736 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002737 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002738 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2739 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002741 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002742 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002743 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002744 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002745 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2746 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747 return;
2748 }
2749
2750 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002751 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002752 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002753 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2754 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002755 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002756 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002757 return;
2758 }
2759
2760 // check if the function is variadic if the 3rd argument non-zero
2761 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002762 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002763 ++NumArgs; // +1 for ...
2764 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002765 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002766 return;
2767 }
2768 }
2769
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002770 // strftime requires FirstArg to be 0 because it doesn't read from any
2771 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002772 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002773 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002774 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2775 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002776 return;
2777 }
2778 // if 0 it disables parameter checking (to use with e.g. va_list)
2779 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002780 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002781 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782 return;
2783 }
2784
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002785 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
2786 Idx.getZExtValue(),
2787 FirstArg.getZExtValue());
2788 if (NewAttr)
2789 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002790}
2791
Chandler Carruthedc2c642011-07-02 00:01:44 +00002792static void handleTransparentUnionAttr(Sema &S, Decl *D,
2793 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002794 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002795 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002796 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002797
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002798
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002799 // Try to find the underlying union declaration.
2800 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002801 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002802 if (TD && TD->getUnderlyingType()->isUnionType())
2803 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2804 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002805 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002806
2807 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002808 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002809 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002810 return;
2811 }
2812
John McCallf937c022011-10-07 06:10:15 +00002813 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002814 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002815 diag::warn_transparent_union_attribute_not_definition);
2816 return;
2817 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002819 RecordDecl::field_iterator Field = RD->field_begin(),
2820 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002821 if (Field == FieldEnd) {
2822 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2823 return;
2824 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002825
David Blaikie40ed2972012-06-06 20:45:41 +00002826 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002827 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002828 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002829 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002830 diag::warn_transparent_union_attribute_floating)
2831 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002832 return;
2833 }
2834
2835 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2836 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2837 for (; Field != FieldEnd; ++Field) {
2838 QualType FieldType = Field->getType();
2839 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2840 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2841 // Warn if we drop the attribute.
2842 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002843 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002844 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002845 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002846 diag::warn_transparent_union_attribute_field_size_align)
2847 << isSize << Field->getDeclName() << FieldBits;
2848 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002849 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002850 diag::note_transparent_union_first_field_size_align)
2851 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002852 return;
2853 }
2854 }
2855
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002856 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002857}
2858
Chandler Carruthedc2c642011-07-02 00:01:44 +00002859static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002860 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002861 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002862 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002863
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002864 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002865 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002866
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002867 // Make sure that there is a string literal as the annotation's single
2868 // argument.
2869 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002870 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002871 return;
2872 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002873
2874 // Don't duplicate annotations that are already set.
2875 for (specific_attr_iterator<AnnotateAttr>
2876 i = D->specific_attr_begin<AnnotateAttr>(),
2877 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2878 if ((*i)->getAnnotation() == SE->getString())
2879 return;
2880 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002881 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002882 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002883}
2884
Chandler Carruthedc2c642011-07-02 00:01:44 +00002885static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002886 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002887 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002889 return;
2890 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002891
2892 //FIXME: The C++0x version of this attribute has more limited applicabilty
2893 // than GNU's, and should error out when it is used to specify a
2894 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002895
Chris Lattner4a927cb2008-06-28 23:36:30 +00002896 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002897 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002898 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002899 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002900
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002901 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002902}
2903
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002904void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002905 // FIXME: Handle pack-expansions here.
2906 if (DiagnoseUnexpandedParameterPack(E))
2907 return;
2908
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002909 if (E->isTypeDependent() || E->isValueDependent()) {
2910 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002911 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002912 return;
2913 }
2914
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002915 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002916 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002917 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002918 ExprResult ICE
2919 = VerifyIntegerConstantExpression(E, &Alignment,
2920 diag::err_aligned_attribute_argument_not_int,
2921 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002922 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002923 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002924 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002925 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2926 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002927 return;
2928 }
2929
Richard Smithf4c51d92012-02-04 09:53:13 +00002930 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002931}
2932
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002933void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002934 // FIXME: Cache the number on the Attr object if non-dependent?
2935 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002936 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002937 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002938}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002940/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002941/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002942///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002943/// Despite what would be logical, the mode attribute is a decl attribute, not a
2944/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2945/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002946static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002947 // This attribute isn't documented, but glibc uses it. It changes
2948 // the width of an int or unsigned int to the specified size.
2949
2950 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002951 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002952 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002953
Chris Lattneracbc2d22008-06-27 22:18:37 +00002954
2955 IdentifierInfo *Name = Attr.getParameterName();
2956 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002957 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002958 return;
2959 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002960
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002961 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002962
2963 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002964 if (Str.startswith("__") && Str.endswith("__"))
2965 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966
2967 unsigned DestWidth = 0;
2968 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002969 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002970 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002971 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002972 switch (Str[0]) {
2973 case 'Q': DestWidth = 8; break;
2974 case 'H': DestWidth = 16; break;
2975 case 'S': DestWidth = 32; break;
2976 case 'D': DestWidth = 64; break;
2977 case 'X': DestWidth = 96; break;
2978 case 'T': DestWidth = 128; break;
2979 }
2980 if (Str[1] == 'F') {
2981 IntegerMode = false;
2982 } else if (Str[1] == 'C') {
2983 IntegerMode = false;
2984 ComplexMode = true;
2985 } else if (Str[1] != 'I') {
2986 DestWidth = 0;
2987 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002988 break;
2989 case 4:
2990 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2991 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002992 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002993 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002994 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002995 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002996 break;
2997 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002998 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002999 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003000 break;
3001 }
3002
3003 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003004 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003005 OldTy = TD->getUnderlyingType();
3006 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3007 OldTy = VD->getType();
3008 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003009 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003010 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003011 return;
3012 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003013
John McCall9dd450b2009-09-21 23:43:11 +00003014 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003015 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3016 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003017 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003018 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3019 } else if (ComplexMode) {
3020 if (!OldTy->isComplexType())
3021 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3022 } else {
3023 if (!OldTy->isFloatingType())
3024 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3025 }
3026
Mike Stump87c57ac2009-05-16 07:39:55 +00003027 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3028 // and friends, at least with glibc.
3029 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3030 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003031 // FIXME: Make sure floating-point mappings are accurate
3032 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003033 QualType NewTy;
3034 switch (DestWidth) {
3035 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003036 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003037 return;
3038 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003039 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003040 return;
3041 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003042 if (!IntegerMode) {
3043 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3044 return;
3045 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003046 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003047 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003048 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003049 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003050 break;
3051 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003052 if (!IntegerMode) {
3053 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3054 return;
3055 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003056 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003057 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003058 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003059 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003060 break;
3061 case 32:
3062 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003063 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003064 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003065 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003066 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003067 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003068 break;
3069 case 64:
3070 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003071 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003072 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003073 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003074 NewTy = S.Context.LongTy;
3075 else
3076 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003077 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003078 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003079 NewTy = S.Context.UnsignedLongTy;
3080 else
3081 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003082 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003083 case 96:
3084 NewTy = S.Context.LongDoubleTy;
3085 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003086 case 128:
3087 if (!IntegerMode) {
3088 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3089 return;
3090 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003091 if (OldTy->isSignedIntegerType())
3092 NewTy = S.Context.Int128Ty;
3093 else
3094 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003095 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003096 }
3097
Eli Friedman4735374e2009-03-03 06:41:03 +00003098 if (ComplexMode) {
3099 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003100 }
3101
3102 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003103 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003104 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003105 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003106 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003107 cast<ValueDecl>(D)->setType(NewTy);
3108}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003109
Chandler Carruthedc2c642011-07-02 00:01:44 +00003110static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003111 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003112 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003113 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003114
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003115 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003116 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003117 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00003118 return;
3119 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003120
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003121 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003122}
3123
Chandler Carruthedc2c642011-07-02 00:01:44 +00003124static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003125 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003126 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003127 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003128
Mike Stumpd3bb5572009-07-24 19:02:52 +00003129
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003130 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003131 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003132 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003133 return;
3134 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003135
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003136 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003137}
3138
Chandler Carruthedc2c642011-07-02 00:01:44 +00003139static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3140 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003141 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003142 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003143 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003144
Chris Lattner3c77a352010-06-22 00:03:40 +00003145
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003146 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003147 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003148 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003149 return;
3150 }
3151
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003152 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003153 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003154}
3155
Chandler Carruthedc2c642011-07-02 00:01:44 +00003156static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003157 if (S.LangOpts.CUDA) {
3158 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003159 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003160 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3161 return;
3162 }
3163
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003164 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003165 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003166 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003167 return;
3168 }
3169
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003170 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003171 } else {
3172 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3173 }
3174}
3175
Chandler Carruthedc2c642011-07-02 00:01:44 +00003176static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003177 if (S.LangOpts.CUDA) {
3178 // check the attribute arguments.
3179 if (Attr.getNumArgs() != 0) {
3180 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3181 return;
3182 }
3183
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003184 if (!isa<FunctionDecl>(D) && !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() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003187 return;
3188 }
3189
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003190 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003191 } else {
3192 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3193 }
3194}
3195
Chandler Carruthedc2c642011-07-02 00:01:44 +00003196static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003197 if (S.LangOpts.CUDA) {
3198 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003199 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003200 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003201
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003202 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003203 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003204 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003205 return;
3206 }
3207
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003208 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003209 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003210 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003211 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3212 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3213 << FD->getType()
3214 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3215 "void");
3216 } else {
3217 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3218 << FD->getType();
3219 }
3220 return;
3221 }
3222
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003223 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003224 } else {
3225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3226 }
3227}
3228
Chandler Carruthedc2c642011-07-02 00:01:44 +00003229static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003230 if (S.LangOpts.CUDA) {
3231 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003232 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003233 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003234
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003235
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003236 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003237 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003238 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003239 return;
3240 }
3241
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003242 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003243 } else {
3244 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3245 }
3246}
3247
Chandler Carruthedc2c642011-07-02 00:01:44 +00003248static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003249 if (S.LangOpts.CUDA) {
3250 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003251 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003252 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003253
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003254
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003255 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003256 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003257 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003258 return;
3259 }
3260
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003261 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003262 } else {
3263 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3264 }
3265}
3266
Chandler Carruthedc2c642011-07-02 00:01:44 +00003267static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003268 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003269 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003270 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003271
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003272 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003273 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003274 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003275 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003276 return;
3277 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003278
Douglas Gregor35b57532009-10-27 21:01:01 +00003279 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003280 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003281 return;
3282 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003283
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003284 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003285}
3286
Chandler Carruthedc2c642011-07-02 00:01:44 +00003287static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003288 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003289
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003290 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003291 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3292 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003293 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003294 return;
3295
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003296 if (!isa<ObjCMethodDecl>(D)) {
3297 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3298 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003299 return;
3300 }
3301
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003302 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00003303 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003304 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003305 return;
3306 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003307 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003308 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00003309 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003310 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003311 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003312 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003313 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003314 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003315 case AttributeList::AT_pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003316 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003317 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003318 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003319 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003320 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003321 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003323 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003324 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003325 return;
3326 }
3327
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003328 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003329 PcsAttr::PCSType PCS;
3330 if (StrRef == "aapcs")
3331 PCS = PcsAttr::AAPCS;
3332 else if (StrRef == "aapcs-vfp")
3333 PCS = PcsAttr::AAPCS_VFP;
3334 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003335 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3336 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003337 return;
3338 }
3339
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003340 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003341 }
Abramo Bagnara50099372010-04-30 13:10:51 +00003342 default:
3343 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003344 }
3345}
3346
Chandler Carruthedc2c642011-07-02 00:01:44 +00003347static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003348 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003349 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003350}
3351
John McCall3882ace2011-01-05 12:14:39 +00003352bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3353 if (attr.isInvalid())
3354 return true;
3355
Ted Kremenek1551d552011-04-15 05:49:29 +00003356 if ((attr.getNumArgs() != 0 &&
3357 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3358 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00003359 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3360 attr.setInvalid();
3361 return true;
3362 }
3363
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003364 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3365 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003366 switch (attr.getKind()) {
3367 case AttributeList::AT_cdecl: CC = CC_C; break;
3368 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3369 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3370 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3371 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003372 case AttributeList::AT_pcs: {
3373 Expr *Arg = attr.getArg(0);
3374 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003375 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003376 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3377 << "pcs" << 1;
3378 attr.setInvalid();
3379 return true;
3380 }
3381
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003382 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003383 if (StrRef == "aapcs") {
3384 CC = CC_AAPCS;
3385 break;
3386 } else if (StrRef == "aapcs-vfp") {
3387 CC = CC_AAPCS_VFP;
3388 break;
3389 }
3390 // FALLS THROUGH
3391 }
David Blaikie8a40f702012-01-17 06:56:22 +00003392 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003393 }
3394
3395 return false;
3396}
3397
Chandler Carruthedc2c642011-07-02 00:01:44 +00003398static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003399 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003400
3401 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003402 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003403 return;
3404
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003405 if (!isa<ObjCMethodDecl>(D)) {
3406 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3407 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003408 return;
3409 }
Eli Friedman7044b762009-03-27 21:06:47 +00003410
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003411 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003412}
3413
3414/// Checks a regparm attribute, returning true if it is ill-formed and
3415/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003416bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3417 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003418 return true;
3419
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003420 if (Attr.getNumArgs() != 1) {
3421 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3422 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003423 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003424 }
Eli Friedman7044b762009-03-27 21:06:47 +00003425
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003426 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003427 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003428 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003429 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003430 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003431 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003432 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003433 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003434 }
3435
Douglas Gregore8bbc122011-09-02 00:18:52 +00003436 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003437 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003438 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003439 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003440 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003441 }
3442
John McCall3882ace2011-01-05 12:14:39 +00003443 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003444 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003445 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003446 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003447 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003448 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003449 }
3450
John McCall3882ace2011-01-05 12:14:39 +00003451 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003452}
3453
Chandler Carruthedc2c642011-07-02 00:01:44 +00003454static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003455 if (S.LangOpts.CUDA) {
3456 // check the attribute arguments.
3457 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003458 // FIXME: 0 is not okay.
3459 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003460 return;
3461 }
3462
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003463 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003465 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003466 return;
3467 }
3468
3469 Expr *MaxThreadsExpr = Attr.getArg(0);
3470 llvm::APSInt MaxThreads(32);
3471 if (MaxThreadsExpr->isTypeDependent() ||
3472 MaxThreadsExpr->isValueDependent() ||
3473 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3475 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3476 return;
3477 }
3478
3479 llvm::APSInt MinBlocks(32);
3480 if (Attr.getNumArgs() > 1) {
3481 Expr *MinBlocksExpr = Attr.getArg(1);
3482 if (MinBlocksExpr->isTypeDependent() ||
3483 MinBlocksExpr->isValueDependent() ||
3484 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3486 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3487 return;
3488 }
3489 }
3490
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003491 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003492 MaxThreads.getZExtValue(),
3493 MinBlocks.getZExtValue()));
3494 } else {
3495 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3496 }
3497}
3498
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003499//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003500// Checker-specific attribute handlers.
3501//===----------------------------------------------------------------------===//
3502
John McCalled433932011-01-25 03:31:58 +00003503static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003504 return type->isDependentType() ||
3505 type->isObjCObjectPointerType() ||
3506 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003507}
3508static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003509 return type->isDependentType() ||
3510 type->isPointerType() ||
3511 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003512}
3513
Chandler Carruthedc2c642011-07-02 00:01:44 +00003514static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003515 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003516 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003517 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003518 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003519 return;
3520 }
3521
3522 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003523 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00003524 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3525 cf = false;
3526 } else {
3527 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3528 cf = true;
3529 }
3530
3531 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003532 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003533 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003534 return;
3535 }
3536
3537 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003538 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003539 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003540 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003541}
3542
Chandler Carruthedc2c642011-07-02 00:01:44 +00003543static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3544 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003545 if (!isa<ObjCMethodDecl>(D)) {
3546 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003547 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003548 return;
3549 }
3550
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003551 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003552}
3553
Chandler Carruthedc2c642011-07-02 00:01:44 +00003554static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3555 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003556
John McCalled433932011-01-25 03:31:58 +00003557 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003559 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003560 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003561 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00003562 returnType = PD->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003563 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003564 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00003565 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003566 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003567 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003568 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003569 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003570 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003571 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003572 return;
3573 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003574
John McCalled433932011-01-25 03:31:58 +00003575 bool typeOK;
3576 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003577 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003578 default: llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003579 case AttributeList::AT_ns_returns_autoreleased:
3580 case AttributeList::AT_ns_returns_retained:
3581 case AttributeList::AT_ns_returns_not_retained:
3582 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3583 cf = false;
3584 break;
3585
3586 case AttributeList::AT_cf_returns_retained:
3587 case AttributeList::AT_cf_returns_not_retained:
3588 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3589 cf = true;
3590 break;
3591 }
3592
3593 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003594 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003595 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003596 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003597 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003598
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003599 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003600 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003601 llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003602 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003603 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003604 S.Context));
3605 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003606 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003607 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003608 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003609 return;
3610 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003611 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003612 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003613 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003614 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003615 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003616 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003617 return;
3618 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003619 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003620 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003621 return;
3622 };
3623}
3624
John McCallcf166702011-07-22 08:53:00 +00003625static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3626 const AttributeList &attr) {
3627 SourceLocation loc = attr.getLoc();
3628
3629 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3630
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003631 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003632 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003633 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003634 return;
3635 }
3636
3637 // Check that the method returns a normal pointer.
3638 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003639
3640 if (!resultType->isReferenceType() &&
3641 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003642 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3643 << SourceRange(loc)
3644 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3645
3646 // Drop the attribute.
3647 return;
3648 }
3649
3650 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003651 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003652}
3653
John McCall32f5fe12011-09-30 05:12:12 +00003654/// Handle cf_audited_transfer and cf_unknown_transfer.
3655static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3656 if (!isa<FunctionDecl>(D)) {
3657 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003658 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00003659 return;
3660 }
3661
3662 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3663
3664 // Check whether there's a conflicting attribute already present.
3665 Attr *Existing;
3666 if (IsAudited) {
3667 Existing = D->getAttr<CFUnknownTransferAttr>();
3668 } else {
3669 Existing = D->getAttr<CFAuditedTransferAttr>();
3670 }
3671 if (Existing) {
3672 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3673 << A.getName()
3674 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3675 << A.getRange() << Existing->getRange();
3676 return;
3677 }
3678
3679 // All clear; add the attribute.
3680 if (IsAudited) {
3681 D->addAttr(
3682 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3683 } else {
3684 D->addAttr(
3685 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3686 }
3687}
3688
John McCallf1e8b342011-09-29 07:17:38 +00003689static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3690 const AttributeList &Attr) {
3691 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3692 if (!RD || RD->isUnion()) {
3693 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003694 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003695 }
3696
3697 IdentifierInfo *ParmName = Attr.getParameterName();
3698
3699 // In Objective-C, verify that the type names an Objective-C type.
3700 // We don't want to check this outside of ObjC because people sometimes
3701 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003702 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003703 // Check for an existing type with this name.
3704 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3705 Sema::LookupOrdinaryName);
3706 if (S.LookupName(R, Sc)) {
3707 NamedDecl *Target = R.getFoundDecl();
3708 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3709 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3710 S.Diag(Target->getLocStart(), diag::note_declared_at);
3711 }
3712 }
3713 }
3714
3715 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3716 ParmName));
3717}
3718
Chandler Carruthedc2c642011-07-02 00:01:44 +00003719static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3720 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003721 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003722
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003723 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003724 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003725}
3726
Chandler Carruthedc2c642011-07-02 00:01:44 +00003727static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3728 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003729 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003730 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003731 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003732 return;
3733 }
3734
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003735 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003736 QualType type = vd->getType();
3737
3738 if (!type->isDependentType() &&
3739 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003740 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003741 << type;
3742 return;
3743 }
3744
3745 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3746
3747 // If we have no lifetime yet, check the lifetime we're presumably
3748 // going to infer.
3749 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3750 lifetime = type->getObjCARCImplicitLifetime();
3751
3752 switch (lifetime) {
3753 case Qualifiers::OCL_None:
3754 assert(type->isDependentType() &&
3755 "didn't infer lifetime for non-dependent type?");
3756 break;
3757
3758 case Qualifiers::OCL_Weak: // meaningful
3759 case Qualifiers::OCL_Strong: // meaningful
3760 break;
3761
3762 case Qualifiers::OCL_ExplicitNone:
3763 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003764 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003765 << (lifetime == Qualifiers::OCL_Autoreleasing);
3766 break;
3767 }
3768
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003769 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003770 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003771}
3772
Charles Davis163855f2010-02-16 18:27:26 +00003773static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman0c84ebb2012-02-23 22:46:33 +00003774 switch (Attr.getKind()) {
3775 default:
3776 return false;
3777 case AttributeList::AT_dllimport:
3778 case AttributeList::AT_dllexport:
3779 case AttributeList::AT_uuid:
3780 case AttributeList::AT_deprecated:
3781 case AttributeList::AT_noreturn:
3782 case AttributeList::AT_nothrow:
3783 case AttributeList::AT_naked:
3784 case AttributeList::AT_noinline:
3785 return true;
3786 }
Francois Picheta83957a2010-12-19 06:50:37 +00003787}
3788
3789//===----------------------------------------------------------------------===//
3790// Microsoft specific attribute handlers.
3791//===----------------------------------------------------------------------===//
3792
Chandler Carruthedc2c642011-07-02 00:01:44 +00003793static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00003794 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00003795 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003796 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003797 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003798
Francois Picheta83957a2010-12-19 06:50:37 +00003799 Expr *Arg = Attr.getArg(0);
3800 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003801 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003802 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3803 << "uuid" << 1;
3804 return;
3805 }
3806
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003807 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003808
3809 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3810 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003811
Francois Pichet7da11662010-12-20 01:41:49 +00003812 // Validate GUID length.
3813 if (IsCurly && StrRef.size() != 38) {
3814 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3815 return;
3816 }
3817 if (!IsCurly && StrRef.size() != 36) {
3818 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3819 return;
3820 }
3821
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003822 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00003823 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003824 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003825 if (IsCurly) // Skip the optional '{'
3826 ++I;
3827
3828 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003829 if (i == 8 || i == 13 || i == 18 || i == 23) {
3830 if (*I != '-') {
3831 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3832 return;
3833 }
3834 } else if (!isxdigit(*I)) {
3835 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3836 return;
3837 }
3838 I++;
3839 }
Francois Picheta83957a2010-12-19 06:50:37 +00003840
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003841 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003842 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003843 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003844 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003845}
3846
John McCall8d32c052012-05-22 21:28:12 +00003847static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3848 if (S.LangOpts.MicrosoftExt) {
3849 AttributeList::Kind Kind = Attr.getKind();
3850 if (Kind == AttributeList::AT_single_inheritance)
3851 D->addAttr(
3852 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
3853 else if (Kind == AttributeList::AT_multiple_inheritance)
3854 D->addAttr(
3855 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
3856 else if (Kind == AttributeList::AT_virtual_inheritance)
3857 D->addAttr(
3858 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
3859 } else
3860 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3861}
3862
3863static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3864 if (S.LangOpts.MicrosoftExt) {
3865 AttributeList::Kind Kind = Attr.getKind();
3866 if (Kind == AttributeList::AT_ptr32)
3867 D->addAttr(
3868 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
3869 else if (Kind == AttributeList::AT_ptr64)
3870 D->addAttr(
3871 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
3872 else if (Kind == AttributeList::AT_w64)
3873 D->addAttr(
3874 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
3875 } else
3876 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3877}
3878
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003879static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3880 if (S.LangOpts.MicrosoftExt)
3881 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
3882 else
3883 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3884}
3885
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003886//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003887// Top Level Sema Entry Points
3888//===----------------------------------------------------------------------===//
3889
Chandler Carruthedc2c642011-07-02 00:01:44 +00003890static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3891 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003892 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003893 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3894 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3895 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003896 default:
3897 break;
3898 }
3899}
Abramo Bagnara50099372010-04-30 13:10:51 +00003900
Chandler Carruthedc2c642011-07-02 00:01:44 +00003901static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3902 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003903 switch (Attr.getKind()) {
Michael Han4a045172012-03-07 00:12:16 +00003904 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3905 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3906 case AttributeList::AT_iboutletcollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003907 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003908 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003909 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003910 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003911 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003912 case AttributeList::AT_neon_vector_type:
3913 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003914 // Ignore these, these are type attributes, handled by
3915 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003916 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003917 case AttributeList::AT_device:
3918 case AttributeList::AT_host:
3919 case AttributeList::AT_overloadable:
3920 // Ignore, this is a non-inheritable attribute, handled
3921 // by ProcessNonInheritableDeclAttr.
3922 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003923 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3924 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00003925 case AttributeList::AT_alloc_size: handleAllocSizeAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003926 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003927 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003928 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003929 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3930 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3931 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003932 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003933 handleDependencyAttr (S, D, Attr); break;
3934 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3935 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3936 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003937 case AttributeList::AT_deprecated:
3938 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
3939 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003940 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003941 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003942 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003943 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003944 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3945 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3946 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3947 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003948 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003949 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003950 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003951 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3952 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3953 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3954 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3955 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003956 case AttributeList::AT_ownership_returns:
3957 case AttributeList::AT_ownership_takes:
3958 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003959 handleOwnershipAttr (S, D, Attr); break;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00003960 case AttributeList::AT_cold: handleColdAttr (S, D, Attr); break;
3961 case AttributeList::AT_hot: handleHotAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003962 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3963 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3964 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3965 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3966 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003967
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003968 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003969 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003970 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003971 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003972
John McCallcf166702011-07-22 08:53:00 +00003973 case AttributeList::AT_objc_returns_inner_pointer:
3974 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3975
John McCallf1e8b342011-09-29 07:17:38 +00003976 case AttributeList::AT_ns_bridged:
3977 handleNSBridgedAttr(S, scope, D, Attr); break;
3978
John McCall32f5fe12011-09-30 05:12:12 +00003979 case AttributeList::AT_cf_audited_transfer:
3980 case AttributeList::AT_cf_unknown_transfer:
3981 handleCFTransferAttr(S, D, Attr); break;
3982
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003983 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003984 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003985 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003986 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003987 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003988
3989 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003990 case AttributeList::AT_ns_returns_not_retained:
3991 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003992 case AttributeList::AT_ns_returns_retained:
3993 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003994 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003995
Michael Han4a045172012-03-07 00:12:16 +00003996 case AttributeList::AT_reqd_work_group_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003997 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003998
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003999 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004000 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004001
Chandler Carruthedc2c642011-07-02 00:01:44 +00004002 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00004003 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004004 case AttributeList::AT_unavailable:
4005 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4006 break;
Michael Han4a045172012-03-07 00:12:16 +00004007 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004008 handleArcWeakrefUnavailableAttr (S, D, Attr);
4009 break;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004010 case AttributeList::AT_objc_root_class:
4011 handleObjCRootClassAttr(S, D, Attr);
4012 break;
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004013 case AttributeList::AT_objc_requires_property_definitions:
4014 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004015 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00004016 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindola70107f92011-10-03 14:59:42 +00004017 case AttributeList::AT_returns_twice:
4018 handleReturnsTwiceAttr(S, D, Attr);
4019 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00004020 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
4021 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
4022 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004023 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00004024 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
4025 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
4026 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004027 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004028 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004029 break;
Chris Lattner677a3582009-02-14 08:09:34 +00004030 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004031 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004032 break;
John McCall86bc21f2011-03-02 11:33:24 +00004033 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004034 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004035 break;
Michael Han4a045172012-03-07 00:12:16 +00004036 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00004037 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
4038 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
4039 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
4040 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
4041 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
4042 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
4043 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
4044 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004045 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004046 // Just ignore
4047 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00004048 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004049 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004050 break;
John McCallab26cfa2010-02-05 21:31:56 +00004051 case AttributeList::AT_stdcall:
4052 case AttributeList::AT_cdecl:
4053 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004054 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004055 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004056 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004057 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004058 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004059 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004060 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004061 break;
John McCall8d32c052012-05-22 21:28:12 +00004062
4063 // Microsoft attributes:
4064 case AttributeList::AT_ms_struct:
4065 handleMsStructAttr(S, D, Attr);
4066 break;
Francois Picheta83957a2010-12-19 06:50:37 +00004067 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004068 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004069 break;
John McCall8d32c052012-05-22 21:28:12 +00004070 case AttributeList::AT_single_inheritance:
4071 case AttributeList::AT_multiple_inheritance:
4072 case AttributeList::AT_virtual_inheritance:
4073 handleInheritanceAttr(S, D, Attr);
4074 break;
4075 case AttributeList::AT_w64:
4076 case AttributeList::AT_ptr32:
4077 case AttributeList::AT_ptr64:
4078 handlePortabilityAttr(S, D, Attr);
4079 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004080 case AttributeList::AT_forceinline:
4081 handleForceInlineAttr(S, D, Attr);
4082 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004083
4084 // Thread safety attributes:
4085 case AttributeList::AT_guarded_var:
4086 handleGuardedVarAttr(S, D, Attr);
4087 break;
4088 case AttributeList::AT_pt_guarded_var:
4089 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
4090 break;
4091 case AttributeList::AT_scoped_lockable:
4092 handleLockableAttr(S, D, Attr, /*scoped = */true);
4093 break;
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004094 case AttributeList::AT_no_address_safety_analysis:
4095 handleNoAddressSafetyAttr(S, D, Attr);
4096 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004097 case AttributeList::AT_no_thread_safety_analysis:
4098 handleNoThreadSafetyAttr(S, D, Attr);
4099 break;
4100 case AttributeList::AT_lockable:
4101 handleLockableAttr(S, D, Attr);
4102 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004103 case AttributeList::AT_guarded_by:
4104 handleGuardedByAttr(S, D, Attr);
4105 break;
4106 case AttributeList::AT_pt_guarded_by:
4107 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
4108 break;
4109 case AttributeList::AT_exclusive_lock_function:
4110 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
4111 break;
4112 case AttributeList::AT_exclusive_locks_required:
4113 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
4114 break;
4115 case AttributeList::AT_exclusive_trylock_function:
4116 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
4117 break;
4118 case AttributeList::AT_lock_returned:
4119 handleLockReturnedAttr(S, D, Attr);
4120 break;
4121 case AttributeList::AT_locks_excluded:
4122 handleLocksExcludedAttr(S, D, Attr);
4123 break;
4124 case AttributeList::AT_shared_lock_function:
4125 handleLockFunAttr(S, D, Attr);
4126 break;
4127 case AttributeList::AT_shared_locks_required:
4128 handleLocksRequiredAttr(S, D, Attr);
4129 break;
4130 case AttributeList::AT_shared_trylock_function:
4131 handleTrylockFunAttr(S, D, Attr);
4132 break;
4133 case AttributeList::AT_unlock_function:
4134 handleUnlockFunAttr(S, D, Attr);
4135 break;
4136 case AttributeList::AT_acquired_before:
4137 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
4138 break;
4139 case AttributeList::AT_acquired_after:
4140 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
4141 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004142
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004143 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004144 // Ask target about the attribute.
4145 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4146 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00004147 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
4148 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004149 break;
4150 }
4151}
4152
Peter Collingbourneb331b262011-01-21 02:08:45 +00004153/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4154/// the attribute applies to decls. If the attribute is a type attribute, just
4155/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4156/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00004157static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4158 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004159 bool NonInheritable, bool Inheritable) {
4160 if (Attr.isInvalid())
4161 return;
4162
4163 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
4164 // FIXME: Try to deal with other __declspec attributes!
4165 return;
4166
4167 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004168 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004169
4170 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004171 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004172}
4173
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004174/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4175/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004176void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004177 const AttributeList *AttrList,
4178 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004179 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004180 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004181 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004182
4183 // GCC accepts
4184 // static int a9 __attribute__((weakref));
4185 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004186 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004187 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004188 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004189 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004190 }
4191}
4192
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004193// Annotation attributes are the only attributes allowed after an access
4194// specifier.
4195bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4196 const AttributeList *AttrList) {
4197 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
4198 if (l->getKind() == AttributeList::AT_annotate) {
4199 handleAnnotateAttr(*this, ASDecl, *l);
4200 } else {
4201 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4202 return true;
4203 }
4204 }
4205
4206 return false;
4207}
4208
John McCall42856de2011-10-01 05:17:03 +00004209/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4210/// contains any decl attributes that we should warn about.
4211static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4212 for ( ; A; A = A->getNext()) {
4213 // Only warn if the attribute is an unignored, non-type attribute.
4214 if (A->isUsedAsTypeAttr()) continue;
4215 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4216
4217 if (A->getKind() == AttributeList::UnknownAttribute) {
4218 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4219 << A->getName() << A->getRange();
4220 } else {
4221 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4222 << A->getName() << A->getRange();
4223 }
4224 }
4225}
4226
4227/// checkUnusedDeclAttributes - Given a declarator which is not being
4228/// used to build a declaration, complain about any decl attributes
4229/// which might be lying around on it.
4230void Sema::checkUnusedDeclAttributes(Declarator &D) {
4231 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4232 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4233 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4234 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4235}
4236
Ryan Flynn7d470f32009-07-30 03:15:39 +00004237/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004238/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004239NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4240 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004241 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004242 NamedDecl *NewD = 0;
4243 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004244 FunctionDecl *NewFD;
4245 // FIXME: Missing call to CheckFunctionDeclaration().
4246 // FIXME: Mangling?
4247 // FIXME: Is the qualifier info correct?
4248 // FIXME: Is the DeclContext correct?
4249 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4250 Loc, Loc, DeclarationName(II),
4251 FD->getType(), FD->getTypeSourceInfo(),
4252 SC_None, SC_None,
4253 false/*isInlineSpecified*/,
4254 FD->hasPrototype(),
4255 false/*isConstexprSpecified*/);
4256 NewD = NewFD;
4257
4258 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004259 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004260
4261 // Fake up parameter variables; they are declared as if this were
4262 // a typedef.
4263 QualType FDTy = FD->getType();
4264 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4265 SmallVector<ParmVarDecl*, 16> Params;
4266 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4267 AE = FT->arg_type_end(); AI != AE; ++AI) {
4268 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4269 Param->setScopeInfo(0, Params.size());
4270 Params.push_back(Param);
4271 }
David Blaikie9c70e042011-09-21 18:16:56 +00004272 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004273 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004274 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4275 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004276 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004277 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004278 VD->getStorageClass(),
4279 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004280 if (VD->getQualifier()) {
4281 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004282 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004283 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004284 }
4285 return NewD;
4286}
4287
James Dennett634962f2012-06-14 21:40:34 +00004288/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004289/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004290void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004291 if (W.getUsed()) return; // only do this once
4292 W.setUsed(true);
4293 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4294 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004295 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004296 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4297 NDId->getName()));
4298 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004299 WeakTopLevelDecl.push_back(NewD);
4300 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4301 // to insert Decl at TU scope, sorry.
4302 DeclContext *SavedContext = CurContext;
4303 CurContext = Context.getTranslationUnitDecl();
4304 PushOnScopeChains(NewD, S);
4305 CurContext = SavedContext;
4306 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004307 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004308 }
4309}
4310
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004311/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4312/// it, apply them to D. This is a bit tricky because PD can have attributes
4313/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004314void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4315 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004316 // It's valid to "forward-declare" #pragma weak, in which case we
4317 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004318 if (Inheritable) {
4319 LoadExternalWeakUndeclaredIdentifiers();
4320 if (!WeakUndeclaredIdentifiers.empty()) {
4321 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4322 if (IdentifierInfo *Id = ND->getIdentifier()) {
4323 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4324 = WeakUndeclaredIdentifiers.find(Id);
4325 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4326 WeakInfo W = I->second;
4327 DeclApplyPragmaWeak(S, ND, W);
4328 WeakUndeclaredIdentifiers[Id] = W;
4329 }
John McCall6fe02402010-10-27 00:59:00 +00004330 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004331 }
4332 }
4333 }
4334
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004335 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004336 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004337 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004338
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004339 // Walk the declarator structure, applying decl attributes that were in a type
4340 // position to the decl itself. This handles cases like:
4341 // int *__attr__(x)** D;
4342 // when X is a decl attribute.
4343 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4344 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004345 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004346
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004347 // Finally, apply any attributes on the decl itself.
4348 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004349 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004350}
John McCall28a6aea2009-11-04 02:18:39 +00004351
John McCall31168b02011-06-15 23:02:42 +00004352/// Is the given declaration allowed to use a forbidden type?
4353static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4354 // Private ivars are always okay. Unfortunately, people don't
4355 // always properly make their ivars private, even in system headers.
4356 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004357 // Function declarations in sys headers will be marked unavailable.
4358 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4359 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004360 return false;
4361
4362 // Require it to be declared in a system header.
4363 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4364}
4365
4366/// Handle a delayed forbidden-type diagnostic.
4367static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4368 Decl *decl) {
4369 if (decl && isForbiddenTypeAllowed(S, decl)) {
4370 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4371 "this system declaration uses an unsupported type"));
4372 return;
4373 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004374 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004375 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004376 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004377 // kind of forbidden type messages on unavailable functions.
4378 if (FD->hasAttr<UnavailableAttr>() &&
4379 diag.getForbiddenTypeDiagnostic() ==
4380 diag::err_arc_array_param_no_ownership) {
4381 diag.Triggered = true;
4382 return;
4383 }
4384 }
John McCall31168b02011-06-15 23:02:42 +00004385
4386 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4387 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4388 diag.Triggered = true;
4389}
4390
John McCall2ec85372012-05-07 06:16:41 +00004391void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4392 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004393 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004394 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004395
John McCall2ec85372012-05-07 06:16:41 +00004396 // When delaying diagnostics to run in the context of a parsed
4397 // declaration, we only want to actually emit anything if parsing
4398 // succeeds.
4399 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004400
John McCall2ec85372012-05-07 06:16:41 +00004401 // We emit all the active diagnostics in this pool or any of its
4402 // parents. In general, we'll get one pool for the decl spec
4403 // and a child pool for each declarator; in a decl group like:
4404 // deprecated_typedef foo, *bar, baz();
4405 // only the declarator pops will be passed decls. This is correct;
4406 // we really do need to consider delayed diagnostics from the decl spec
4407 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004408 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004409 do {
John McCall6347b682012-05-07 06:16:58 +00004410 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004411 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4412 // This const_cast is a bit lame. Really, Triggered should be mutable.
4413 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004414 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004415 continue;
4416
John McCallc1465822011-02-14 07:13:47 +00004417 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004418 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004419 // Don't bother giving deprecation diagnostics if the decl is invalid.
4420 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004421 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004422 break;
4423
4424 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004425 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004426 break;
John McCall31168b02011-06-15 23:02:42 +00004427
4428 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004429 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004430 break;
John McCall86121512010-01-27 03:50:35 +00004431 }
4432 }
John McCall2ec85372012-05-07 06:16:41 +00004433 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004434}
4435
John McCall6347b682012-05-07 06:16:58 +00004436/// Given a set of delayed diagnostics, re-emit them as if they had
4437/// been delayed in the current context instead of in the given pool.
4438/// Essentially, this just moves them to the current pool.
4439void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4440 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4441 assert(curPool && "re-emitting in undelayed context not supported");
4442 curPool->steal(pool);
4443}
4444
John McCall28a6aea2009-11-04 02:18:39 +00004445static bool isDeclDeprecated(Decl *D) {
4446 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004447 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004448 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004449 // A category implicitly has the availability of the interface.
4450 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4451 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004452 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4453 return false;
4454}
4455
John McCallb45a1e72010-08-26 02:13:20 +00004456void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004457 Decl *Ctx) {
4458 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004459 return;
4460
John McCall86121512010-01-27 03:50:35 +00004461 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004462 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004463 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004464 << DD.getDeprecationDecl()->getDeclName()
4465 << DD.getDeprecationMessage();
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004466 else if (DD.getUnknownObjCClass()) {
4467 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4468 << DD.getDeprecationDecl()->getDeclName();
4469 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4470 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00004471 else
4472 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004473 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00004474}
4475
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004476void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004477 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004478 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00004479 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004480 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004481 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4482 UnknownObjCClass,
4483 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004484 return;
4485 }
4486
4487 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004488 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004489 return;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004490 if (!Message.empty()) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00004491 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4492 << Message;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004493 Diag(D->getLocation(),
4494 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4495 : diag::note_previous_decl) << D->getDeclName();
4496 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004497 else {
Fariborz Jahanianf0218892012-05-27 16:59:48 +00004498 if (!UnknownObjCClass) {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004499 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanianf0218892012-05-27 16:59:48 +00004500 Diag(D->getLocation(),
4501 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4502 : diag::note_previous_decl) << D->getDeclName();
4503 }
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004504 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004505 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004506 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4507 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004508 }
John McCall28a6aea2009-11-04 02:18:39 +00004509}