blob: 987a969f1f30cdca076d9ce4728f26752495ae55 [file] [log] [blame]
Chris Lattner6b6b5372008-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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000028using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030
John McCall883cc2c2011-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 Sadowskib51e0312011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000046 ExpectedStruct,
47 ExpectedTLSVar
John McCall883cc2c2011-03-02 12:29:23 +000048};
49
Chris Lattnere5c5ee12008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruth87c44602011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000065
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070
John McCall183700f2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000072}
73
Daniel Dunbar35682492008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopesd20254f2009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000087 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000088}
89
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000102}
103
John McCall711c52b2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000110}
111
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000118 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000133}
134
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148}
149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000156 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000175
John McCall506b57e2010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000211
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221 return true;
222}
223
224///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000225/// \brief Check if passed in Decl is a field or potentially shared global var
226/// \return true if the Decl is a field or potentially shared global variable
227///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000228static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000229 if (isa<FieldDecl>(D))
230 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000231 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000232 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
233
234 return false;
235}
236
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000237/// \brief Check if the passed-in expression is of type int or bool.
238static bool isIntOrBool(Expr *Exp) {
239 QualType QT = Exp->getType();
240 return QT->isBooleanType() || QT->isIntegerType();
241}
242
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000243
244// Check to see if the type is a smart pointer of some kind. We assume
245// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000246static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
247 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
248 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
249 if (Res1.first == Res1.second)
250 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000251
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000252 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
253 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
254 if (Res2.first == Res2.second)
255 return false;
256
257 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000258}
259
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000260/// \brief Check if passed in Decl is a pointer type.
261/// Note that this function may produce an error message.
262/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000263static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
264 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000265 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000266 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000267 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000268 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000269
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000270 if (const RecordType *RT = QT->getAs<RecordType>()) {
271 // If it's an incomplete type, it could be a smart pointer; skip it.
272 // (We don't want to force template instantiation if we can avoid it,
273 // since that would alter the order in which templates are instantiated.)
274 if (RT->isIncompleteType())
275 return true;
276
277 if (threadSafetyCheckIsSmartPointer(S, RT))
278 return true;
279 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000280
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000281 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000282 << Attr.getName()->getName() << QT;
283 } else {
284 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
285 << Attr.getName();
286 }
287 return false;
288}
289
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000290/// \brief Checks that the passed in QualType either is of RecordType or points
291/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000292static const RecordType *getRecordType(QualType QT) {
293 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000294 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000295
296 // Now check if we point to record type.
297 if (const PointerType *PT = QT->getAs<PointerType>())
298 return PT->getPointeeType()->getAs<RecordType>();
299
300 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000301}
302
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000303
Jordy Rosefad5de92012-05-08 03:27:22 +0000304static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
305 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000306 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
307 if (RT->getDecl()->getAttr<LockableAttr>())
308 return true;
309 return false;
310}
311
312
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000313/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000314/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000315static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
316 QualType Ty) {
317 const RecordType *RT = getRecordType(Ty);
318
319 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000320 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000321 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000322 << Attr.getName() << Ty.getAsString();
323 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000324 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000325
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000326 // Don't check for lockable if the class hasn't been defined yet.
327 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000328 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000329
330 // Allow smart pointers to be used as lockable objects.
331 // FIXME -- Check the type that the smart pointer points to.
332 if (threadSafetyCheckIsSmartPointer(S, RT))
333 return;
334
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000335 // Check if the type is lockable.
336 RecordDecl *RD = RT->getDecl();
337 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000338 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000339
340 // Else check if any base classes are lockable.
341 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
342 CXXBasePaths BPaths(false, false);
343 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
344 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000345 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000346
347 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
348 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000349}
350
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000351/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000352/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000353/// \param Sidx The attribute argument index to start checking with.
354/// \param ParamIdxOk Whether an argument can be indexing into a function
355/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000356static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000357 const AttributeList &Attr,
358 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000359 int Sidx = 0,
360 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000361 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000362 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000363
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000364 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000365 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000366 Args.push_back(ArgExp);
367 continue;
368 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000369
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000370 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
371 // Ignore empty strings without warnings
372 if (StrLit->getLength() == 0)
373 continue;
374
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000375 // We allow constant strings to be used as a placeholder for expressions
376 // that are not valid C++ syntax, but warn that they are ignored.
377 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
378 Attr.getName();
379 continue;
380 }
381
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000382 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000383
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000384 // A pointer to member expression of the form &MyClass::mu is treated
385 // specially -- we need to look at the type of the member.
386 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
387 if (UOp->getOpcode() == UO_AddrOf)
388 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
389 if (DRE->getDecl()->isCXXInstanceMember())
390 ArgTy = DRE->getDecl()->getType();
391
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000392 // First see if we can just cast to record type, or point to record type.
393 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000394
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000395 // Now check if we index into a record type function param.
396 if(!RT && ParamIdxOk) {
397 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000398 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
399 if(FD && IL) {
400 unsigned int NumParams = FD->getNumParams();
401 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000402 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
403 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
404 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
406 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000407 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000408 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000409 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000410 }
411 }
412
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000413 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000414
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000415 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000416 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000417}
418
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000419//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000420// Attribute Implementations
421//===----------------------------------------------------------------------===//
422
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000423// FIXME: All this manual attribute parsing code is gross. At the
424// least add some helper functions to check most argument patterns (#
425// and types of args).
426
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000427enum ThreadAttributeDeclKind {
428 ThreadExpectedFieldOrGlobalVar,
429 ThreadExpectedFunctionOrMethod,
430 ThreadExpectedClassOrStruct
431};
432
Michael Handc691572012-07-23 18:48:41 +0000433static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
434 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000435 assert(!Attr.isInvalid());
436
437 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000438 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000439
440 // D must be either a member field or global (potentially shared) variable.
441 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000442 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
443 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000444 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000445 }
446
Michael Handc691572012-07-23 18:48:41 +0000447 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000448}
449
Michael Handc691572012-07-23 18:48:41 +0000450static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
451 if (!checkGuardedVarAttrCommon(S, D, Attr))
452 return;
453
454 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
455}
456
457static void handlePtGuardedVarAttr(Sema &S, Decl *D,
458 const AttributeList &Attr) {
459 if (!checkGuardedVarAttrCommon(S, D, Attr))
460 return;
461
462 if (!threadSafetyCheckIsPointer(S, D, Attr))
463 return;
464
465 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
466}
467
468static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
469 const AttributeList &Attr,
470 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000471 assert(!Attr.isInvalid());
472
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000473 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000474 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000475
476 // D must be either a member field or global (potentially shared) variable.
477 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000478 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
479 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000480 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000481 }
482
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000483 SmallVector<Expr*, 1> Args;
484 // check that all arguments are lockable objects
485 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
486 unsigned Size = Args.size();
487 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000488 return false;
489
490 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000491
Michael Handc691572012-07-23 18:48:41 +0000492 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000493}
494
Michael Handc691572012-07-23 18:48:41 +0000495static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
496 Expr *Arg = 0;
497 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
498 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000499
Michael Handc691572012-07-23 18:48:41 +0000500 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
501}
502
503static void handlePtGuardedByAttr(Sema &S, Decl *D,
504 const AttributeList &Attr) {
505 Expr *Arg = 0;
506 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
507 return;
508
509 if (!threadSafetyCheckIsPointer(S, D, Attr))
510 return;
511
512 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
513 S.Context, Arg));
514}
515
516static bool checkLockableAttrCommon(Sema &S, Decl *D,
517 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000518 assert(!Attr.isInvalid());
519
520 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000521 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000522
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000523 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000524 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000525 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
526 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000527 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000528 }
529
Michael Handc691572012-07-23 18:48:41 +0000530 return true;
531}
532
533static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
534 if (!checkLockableAttrCommon(S, D, Attr))
535 return;
536
537 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
538}
539
540static void handleScopedLockableAttr(Sema &S, Decl *D,
541 const AttributeList &Attr) {
542 if (!checkLockableAttrCommon(S, D, Attr))
543 return;
544
545 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000546}
547
548static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
549 const AttributeList &Attr) {
550 assert(!Attr.isInvalid());
551
552 if (!checkAttributeNumArgs(S, Attr, 0))
553 return;
554
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000555 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000556 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
557 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000558 return;
559 }
560
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000561 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000562 S.Context));
563}
564
Kostya Serebryany71efba02012-01-24 19:25:38 +0000565static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000566 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000567 assert(!Attr.isInvalid());
568
569 if (!checkAttributeNumArgs(S, Attr, 0))
570 return;
571
572 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
573 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
574 << Attr.getName() << ExpectedFunctionOrMethod;
575 return;
576 }
577
578 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000579 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000580}
581
Michael Handc691572012-07-23 18:48:41 +0000582static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
583 const AttributeList &Attr,
584 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000585 assert(!Attr.isInvalid());
586
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000587 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000588 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000589
590 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000591 ValueDecl *VD = dyn_cast<ValueDecl>(D);
592 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000593 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
594 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000595 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000596 }
597
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000598 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000599 QualType QT = VD->getType();
600 if (!QT->isDependentType()) {
601 const RecordType *RT = getRecordType(QT);
602 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000603 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000604 << Attr.getName();
605 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000606 }
607 }
608
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000609 // Check that all arguments are lockable objects.
610 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000611 if (Args.size() == 0)
612 return false;
613
614 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000615}
616
Michael Handc691572012-07-23 18:48:41 +0000617static void handleAcquiredAfterAttr(Sema &S, Decl *D,
618 const AttributeList &Attr) {
619 SmallVector<Expr*, 1> Args;
620 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
621 return;
622
623 Expr **StartArg = &Args[0];
624 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000625 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000626}
627
628static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
629 const AttributeList &Attr) {
630 SmallVector<Expr*, 1> Args;
631 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
632 return;
633
634 Expr **StartArg = &Args[0];
635 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000636 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000637}
638
639static bool checkLockFunAttrCommon(Sema &S, Decl *D,
640 const AttributeList &Attr,
641 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000642 assert(!Attr.isInvalid());
643
644 // zero or more arguments ok
645
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000646 // check that the attribute is applied to a function
647 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000648 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
649 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000650 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000651 }
652
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000653 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000654 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000655
Michael Handc691572012-07-23 18:48:41 +0000656 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000657}
658
Michael Handc691572012-07-23 18:48:41 +0000659static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
660 const AttributeList &Attr) {
661 SmallVector<Expr*, 1> Args;
662 if (!checkLockFunAttrCommon(S, D, Attr, Args))
663 return;
664
665 unsigned Size = Args.size();
666 Expr **StartArg = Size == 0 ? 0 : &Args[0];
667 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
668 S.Context,
669 StartArg, Size));
670}
671
672static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
673 const AttributeList &Attr) {
674 SmallVector<Expr*, 1> Args;
675 if (!checkLockFunAttrCommon(S, D, Attr, Args))
676 return;
677
678 unsigned Size = Args.size();
679 Expr **StartArg = Size == 0 ? 0 : &Args[0];
680 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
681 S.Context,
682 StartArg, Size));
683}
684
685static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
686 const AttributeList &Attr,
687 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000688 assert(!Attr.isInvalid());
689
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000690 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000691 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000692
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000693 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000694 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
695 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000696 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000697 }
698
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000699 if (!isIntOrBool(Attr.getArg(0))) {
700 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000701 << Attr.getName();
702 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000703 }
704
705 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000706 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000707
Michael Handc691572012-07-23 18:48:41 +0000708 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000709}
710
Michael Handc691572012-07-23 18:48:41 +0000711static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
712 const AttributeList &Attr) {
713 SmallVector<Expr*, 2> Args;
714 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
715 return;
716
717 unsigned Size = Args.size();
718 Expr **StartArg = Size == 0 ? 0 : &Args[0];
719 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
720 S.Context,
721 Attr.getArg(0),
722 StartArg, Size));
723}
724
725static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
726 const AttributeList &Attr) {
727 SmallVector<Expr*, 2> Args;
728 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
729 return;
730
731 unsigned Size = Args.size();
732 Expr **StartArg = Size == 0 ? 0 : &Args[0];
733 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
734 S.Context,
735 Attr.getArg(0),
736 StartArg, Size));
737}
738
739static bool checkLocksRequiredCommon(Sema &S, Decl *D,
740 const AttributeList &Attr,
741 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000742 assert(!Attr.isInvalid());
743
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000744 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000745 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000746
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000747 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000748 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
749 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000750 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000751 }
752
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000753 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000754 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000755 if (Args.size() == 0)
756 return false;
757
758 return true;
759}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000760
Michael Handc691572012-07-23 18:48:41 +0000761static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
762 const AttributeList &Attr) {
763 SmallVector<Expr*, 1> Args;
764 if (!checkLocksRequiredCommon(S, D, Attr, Args))
765 return;
766
767 Expr **StartArg = &Args[0];
768 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000769 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000770 StartArg,
771 Args.size()));
772}
773
774static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
775 const AttributeList &Attr) {
776 SmallVector<Expr*, 1> Args;
777 if (!checkLocksRequiredCommon(S, D, Attr, Args))
778 return;
779
780 Expr **StartArg = &Args[0];
781 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000782 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000783 StartArg,
784 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000785}
786
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000787static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000788 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000789 assert(!Attr.isInvalid());
790
791 // zero or more arguments ok
792
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000793 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000794 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
795 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000796 return;
797 }
798
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000799 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000800 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000801 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000802 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000803 Expr **StartArg = Size == 0 ? 0 : &Args[0];
804
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000805 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000806 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000807}
808
809static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000810 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000811 assert(!Attr.isInvalid());
812
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000813 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000814 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000815 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000816
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000817 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000818 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
819 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000820 return;
821 }
822
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000823 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000824 return;
825
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000826 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000827 SmallVector<Expr*, 1> Args;
828 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
829 unsigned Size = Args.size();
830 if (Size == 0)
831 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000832
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000833 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
834 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000835}
836
837static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839 assert(!Attr.isInvalid());
840
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000841 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000842 return;
843
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000845 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
846 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000847 return;
848 }
849
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000850 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000851 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000852 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000853 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000854 if (Size == 0)
855 return;
856 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000857
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000858 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000859 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000860}
861
862
Chandler Carruth1b03c872011-07-02 00:01:44 +0000863static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
864 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000865 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000866 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000867 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000868 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000869 }
Mike Stumpbf916502009-07-24 19:02:52 +0000870
Chris Lattner6b6b5372008-06-26 18:38:35 +0000871 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000872
873 Expr *sizeExpr;
874
875 // Special case where the argument is a template id.
876 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000877 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000878 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000879 UnqualifiedId id;
880 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000881
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000882 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
883 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000884 if (Size.isInvalid())
885 return;
886
887 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000888 } else {
889 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000890 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000891 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000892
Peter Collingbourne7a730022010-11-23 20:45:58 +0000893 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000894 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000895
896 // Instantiate/Install the vector type, and let Sema build the type for us.
897 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000898 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000899 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000900 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000901 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000902
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000903 // Remember this typedef decl, we will need it later for diagnostics.
904 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000905 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000906}
907
Chandler Carruth1b03c872011-07-02 00:01:44 +0000908static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000909 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000910 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000911 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000912
Chandler Carruth87c44602011-07-01 23:49:12 +0000913 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000914 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000915 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000916 // If the alignment is less than or equal to 8 bits, the packed attribute
917 // has no effect.
918 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000919 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000920 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000921 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000922 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000923 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000924 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000925 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000926}
927
Chandler Carruth1b03c872011-07-02 00:01:44 +0000928static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000929 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000930 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000931 else
932 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
933}
934
Chandler Carruth1b03c872011-07-02 00:01:44 +0000935static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000936 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000937 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000938 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000939
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000940 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000941 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000942 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000943 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000944 return;
945 }
946
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000947 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000948}
949
Ted Kremenek2f041d02011-09-29 07:02:25 +0000950static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
951 // The IBOutlet/IBOutletCollection attributes only apply to instance
952 // variables or properties of Objective-C classes. The outlet must also
953 // have an object reference type.
954 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
955 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000956 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000957 << Attr.getName() << VD->getType() << 0;
958 return false;
959 }
960 }
961 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
962 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000963 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000964 << Attr.getName() << PD->getType() << 1;
965 return false;
966 }
967 }
968 else {
969 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
970 return false;
971 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000972
Ted Kremenek2f041d02011-09-29 07:02:25 +0000973 return true;
974}
975
Chandler Carruth1b03c872011-07-02 00:01:44 +0000976static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000977 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000978 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000979 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000980
981 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000982 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000983
Ted Kremenek2f041d02011-09-29 07:02:25 +0000984 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000985}
986
Chandler Carruth1b03c872011-07-02 00:01:44 +0000987static void handleIBOutletCollection(Sema &S, Decl *D,
988 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000989
990 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000991 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
993 return;
994 }
995
Ted Kremenek2f041d02011-09-29 07:02:25 +0000996 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000997 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000998
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000999 IdentifierInfo *II = Attr.getParameterName();
1000 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001001 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001002
John McCallb3d87482010-08-24 05:47:05 +00001003 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001004 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001005 if (!TypeRep) {
1006 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1007 return;
1008 }
John McCallb3d87482010-08-24 05:47:05 +00001009 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001010 // Diagnose use of non-object type in iboutletcollection attribute.
1011 // FIXME. Gnu attribute extension ignores use of builtin types in
1012 // attributes. So, __attribute__((iboutletcollection(char))) will be
1013 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001014 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001015 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1016 return;
1017 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001018 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1019 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001020}
1021
Chandler Carruthd309c812011-07-01 23:49:16 +00001022static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001023 if (const RecordType *UT = T->getAsUnionType())
1024 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1025 RecordDecl *UD = UT->getDecl();
1026 for (RecordDecl::field_iterator it = UD->field_begin(),
1027 itend = UD->field_end(); it != itend; ++it) {
1028 QualType QT = it->getType();
1029 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1030 T = QT;
1031 return;
1032 }
1033 }
1034 }
1035}
1036
Nuno Lopes587de5b2012-05-24 00:22:00 +00001037static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001038 if (!isFunctionOrMethod(D)) {
1039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1040 << "alloc_size" << ExpectedFunctionOrMethod;
1041 return;
1042 }
1043
Nuno Lopes587de5b2012-05-24 00:22:00 +00001044 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1045 return;
1046
1047 // In C++ the implicit 'this' function parameter also counts, and they are
1048 // counted from one.
1049 bool HasImplicitThisParam = isInstanceMethod(D);
1050 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1051
1052 SmallVector<unsigned, 8> SizeArgs;
1053
1054 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1055 E = Attr.arg_end(); I!=E; ++I) {
1056 // The argument must be an integer constant expression.
1057 Expr *Ex = *I;
1058 llvm::APSInt ArgNum;
1059 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1060 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1061 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1062 << "alloc_size" << Ex->getSourceRange();
1063 return;
1064 }
1065
1066 uint64_t x = ArgNum.getZExtValue();
1067
1068 if (x < 1 || x > NumArgs) {
1069 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1070 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1071 return;
1072 }
1073
1074 --x;
1075 if (HasImplicitThisParam) {
1076 if (x == 0) {
1077 S.Diag(Attr.getLoc(),
1078 diag::err_attribute_invalid_implicit_this_argument)
1079 << "alloc_size" << Ex->getSourceRange();
1080 return;
1081 }
1082 --x;
1083 }
1084
1085 // check if the function argument is of an integer type
1086 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1087 if (!T->isIntegerType()) {
1088 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1089 << "alloc_size" << Ex->getSourceRange();
1090 return;
1091 }
1092
Nuno Lopes587de5b2012-05-24 00:22:00 +00001093 SizeArgs.push_back(x);
1094 }
1095
1096 // check if the function returns a pointer
1097 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1098 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1099 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1100 }
1101
Nuno Lopes96c67d12012-06-18 16:27:56 +00001102 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1103 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001104}
1105
Chandler Carruth1b03c872011-07-02 00:01:44 +00001106static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001107 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1108 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001109 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001110 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001111 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001112 return;
1113 }
Mike Stumpbf916502009-07-24 19:02:52 +00001114
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001115 // In C++ the implicit 'this' function parameter also counts, and they are
1116 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001117 bool HasImplicitThisParam = isInstanceMethod(D);
1118 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001119
1120 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001121 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001122
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001123 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1124 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001125
1126
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001127 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001128 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001129 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001130 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1131 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001132 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1133 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001134 return;
1135 }
Mike Stumpbf916502009-07-24 19:02:52 +00001136
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001137 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001138
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001139 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001141 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001142 return;
1143 }
Mike Stumpbf916502009-07-24 19:02:52 +00001144
Ted Kremenek465172f2008-07-21 22:09:15 +00001145 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001146 if (HasImplicitThisParam) {
1147 if (x == 0) {
1148 S.Diag(Attr.getLoc(),
1149 diag::err_attribute_invalid_implicit_this_argument)
1150 << "nonnull" << Ex->getSourceRange();
1151 return;
1152 }
1153 --x;
1154 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001155
1156 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001157 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001158 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001159
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001160 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001161 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001162 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001163 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001164 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001165 }
Mike Stumpbf916502009-07-24 19:02:52 +00001166
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001167 NonNullArgs.push_back(x);
1168 }
Mike Stumpbf916502009-07-24 19:02:52 +00001169
1170 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1171 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001172 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001173 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1174 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001175 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001176 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001177 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001178 }
Mike Stumpbf916502009-07-24 19:02:52 +00001179
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001180 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001181 if (NonNullArgs.empty()) {
1182 // Warn the trivial case only if attribute is not coming from a
1183 // macro instantiation.
1184 if (Attr.getLoc().isFileID())
1185 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001186 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001187 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001188 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001189
1190 unsigned* start = &NonNullArgs[0];
1191 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001192 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001193 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001194 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001195}
1196
Chandler Carruth1b03c872011-07-02 00:01:44 +00001197static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001198 // This attribute must be applied to a function declaration.
1199 // The first argument to the attribute must be a string,
1200 // the name of the resource, for example "malloc".
1201 // The following arguments must be argument indexes, the arguments must be
1202 // of integer type for Returns, otherwise of pointer type.
1203 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001204 // after being held. free() should be __attribute((ownership_takes)), whereas
1205 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001206
1207 if (!AL.getParameterName()) {
1208 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1209 << AL.getName()->getName() << 1;
1210 return;
1211 }
1212 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001213 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001214 switch (AL.getKind()) {
1215 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001216 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001217 if (AL.getNumArgs() < 1) {
1218 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1219 return;
1220 }
Jordy Rose2a479922010-08-12 08:54:03 +00001221 break;
1222 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001223 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001224 if (AL.getNumArgs() < 1) {
1225 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1226 return;
1227 }
Jordy Rose2a479922010-08-12 08:54:03 +00001228 break;
1229 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001230 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001231 if (AL.getNumArgs() > 1) {
1232 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1233 << AL.getNumArgs() + 1;
1234 return;
1235 }
Jordy Rose2a479922010-08-12 08:54:03 +00001236 break;
1237 default:
1238 // This should never happen given how we are called.
1239 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001240 }
1241
Chandler Carruth87c44602011-07-01 23:49:12 +00001242 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001243 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1244 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001245 return;
1246 }
1247
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001248 // In C++ the implicit 'this' function parameter also counts, and they are
1249 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001250 bool HasImplicitThisParam = isInstanceMethod(D);
1251 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001252
Chris Lattner5f9e2722011-07-23 10:55:15 +00001253 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001254
1255 // Normalize the argument, __foo__ becomes foo.
1256 if (Module.startswith("__") && Module.endswith("__"))
1257 Module = Module.substr(2, Module.size() - 4);
1258
Chris Lattner5f9e2722011-07-23 10:55:15 +00001259 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001260
Jordy Rose2a479922010-08-12 08:54:03 +00001261 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1262 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001263
Peter Collingbourne7a730022010-11-23 20:45:58 +00001264 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001265 llvm::APSInt ArgNum(32);
1266 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1267 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1268 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1269 << AL.getName()->getName() << IdxExpr->getSourceRange();
1270 continue;
1271 }
1272
1273 unsigned x = (unsigned) ArgNum.getZExtValue();
1274
1275 if (x > NumArgs || x < 1) {
1276 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1277 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1278 continue;
1279 }
1280 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001281 if (HasImplicitThisParam) {
1282 if (x == 0) {
1283 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1284 << "ownership" << IdxExpr->getSourceRange();
1285 return;
1286 }
1287 --x;
1288 }
1289
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001290 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001291 case OwnershipAttr::Takes:
1292 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001293 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001294 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001295 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1296 // FIXME: Should also highlight argument in decl.
1297 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001298 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001299 << "pointer"
1300 << IdxExpr->getSourceRange();
1301 continue;
1302 }
1303 break;
1304 }
Sean Huntcf807c42010-08-18 23:23:40 +00001305 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001306 if (AL.getNumArgs() > 1) {
1307 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001308 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001309 llvm::APSInt ArgNum(32);
1310 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1311 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1312 S.Diag(AL.getLoc(), diag::err_ownership_type)
1313 << "ownership_returns" << "integer"
1314 << IdxExpr->getSourceRange();
1315 return;
1316 }
1317 }
1318 break;
1319 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001320 } // switch
1321
1322 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001323 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001324 i = D->specific_attr_begin<OwnershipAttr>(),
1325 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001326 i != e; ++i) {
1327 if ((*i)->getOwnKind() != K) {
1328 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1329 I!=E; ++I) {
1330 if (x == *I) {
1331 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1332 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001333 }
1334 }
1335 }
1336 }
1337 OwnershipArgs.push_back(x);
1338 }
1339
1340 unsigned* start = OwnershipArgs.data();
1341 unsigned size = OwnershipArgs.size();
1342 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001343
1344 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1345 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1346 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001347 }
Sean Huntcf807c42010-08-18 23:23:40 +00001348
Chandler Carruth87c44602011-07-01 23:49:12 +00001349 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001350 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001351}
1352
John McCall332bb2a2011-02-08 22:35:49 +00001353/// Whether this declaration has internal linkage for the purposes of
1354/// things that want to complain about things not have internal linkage.
1355static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1356 switch (D->getLinkage()) {
1357 case NoLinkage:
1358 case InternalLinkage:
1359 return true;
1360
1361 // Template instantiations that go from external to unique-external
1362 // shouldn't get diagnosed.
1363 case UniqueExternalLinkage:
1364 return true;
1365
1366 case ExternalLinkage:
1367 return false;
1368 }
1369 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001370}
1371
Chandler Carruth1b03c872011-07-02 00:01:44 +00001372static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001373 // Check the attribute arguments.
1374 if (Attr.getNumArgs() > 1) {
1375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1376 return;
1377 }
1378
Chandler Carruth87c44602011-07-01 23:49:12 +00001379 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001381 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001382 return;
1383 }
1384
Chandler Carruth87c44602011-07-01 23:49:12 +00001385 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001386
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001387 // gcc rejects
1388 // class c {
1389 // static int a __attribute__((weakref ("v2")));
1390 // static int b() __attribute__((weakref ("f3")));
1391 // };
1392 // and ignores the attributes of
1393 // void f(void) {
1394 // static int a __attribute__((weakref ("v2")));
1395 // }
1396 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001397 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001398 if (!Ctx->isFileContext()) {
1399 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001400 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001401 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001402 }
1403
1404 // The GCC manual says
1405 //
1406 // At present, a declaration to which `weakref' is attached can only
1407 // be `static'.
1408 //
1409 // It also says
1410 //
1411 // Without a TARGET,
1412 // given as an argument to `weakref' or to `alias', `weakref' is
1413 // equivalent to `weak'.
1414 //
1415 // gcc 4.4.1 will accept
1416 // int a7 __attribute__((weakref));
1417 // as
1418 // int a7 __attribute__((weak));
1419 // This looks like a bug in gcc. We reject that for now. We should revisit
1420 // it if this behaviour is actually used.
1421
John McCall332bb2a2011-02-08 22:35:49 +00001422 if (!hasEffectivelyInternalLinkage(nd)) {
1423 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001424 return;
1425 }
1426
1427 // GCC rejects
1428 // static ((alias ("y"), weakref)).
1429 // Should we? How to check that weakref is before or after alias?
1430
1431 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001432 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001433 Arg = Arg->IgnoreParenCasts();
1434 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1435
Douglas Gregor5cee1192011-07-27 05:40:30 +00001436 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001437 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1438 << "weakref" << 1;
1439 return;
1440 }
1441 // GCC will accept anything as the argument of weakref. Should we
1442 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001443 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001444 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001445 }
1446
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001447 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001448}
1449
Chandler Carruth1b03c872011-07-02 00:01:44 +00001450static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001451 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001452 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001454 return;
1455 }
Mike Stumpbf916502009-07-24 19:02:52 +00001456
Peter Collingbourne7a730022010-11-23 20:45:58 +00001457 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001458 Arg = Arg->IgnoreParenCasts();
1459 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001460
Douglas Gregor5cee1192011-07-27 05:40:30 +00001461 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001462 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001463 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001464 return;
1465 }
Mike Stumpbf916502009-07-24 19:02:52 +00001466
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001467 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001468 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1469 return;
1470 }
1471
Chris Lattner6b6b5372008-06-26 18:38:35 +00001472 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001473
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001474 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001475 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476}
1477
Benjamin Krameree409a92012-05-12 21:10:52 +00001478static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1479 // Check the attribute arguments.
1480 if (!checkAttributeNumArgs(S, Attr, 0))
1481 return;
1482
1483 if (!isa<FunctionDecl>(D)) {
1484 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1485 << Attr.getName() << ExpectedFunction;
1486 return;
1487 }
1488
1489 if (D->hasAttr<HotAttr>()) {
1490 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1491 << Attr.getName() << "hot";
1492 return;
1493 }
1494
1495 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1496}
1497
1498static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1499 // Check the attribute arguments.
1500 if (!checkAttributeNumArgs(S, Attr, 0))
1501 return;
1502
1503 if (!isa<FunctionDecl>(D)) {
1504 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1505 << Attr.getName() << ExpectedFunction;
1506 return;
1507 }
1508
1509 if (D->hasAttr<ColdAttr>()) {
1510 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1511 << Attr.getName() << "cold";
1512 return;
1513 }
1514
1515 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1516}
1517
Chandler Carruth1b03c872011-07-02 00:01:44 +00001518static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001519 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001520 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001521 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001522
Chandler Carruth87c44602011-07-01 23:49:12 +00001523 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001525 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001526 return;
1527 }
1528
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001529 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001530}
1531
Chandler Carruth1b03c872011-07-02 00:01:44 +00001532static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1533 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001534 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001535 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1537 return;
1538 }
1539
Chandler Carruth87c44602011-07-01 23:49:12 +00001540 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001541 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001542 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001543 return;
1544 }
Mike Stumpbf916502009-07-24 19:02:52 +00001545
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001546 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001547}
1548
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001549static void handleTLSModelAttr(Sema &S, Decl *D,
1550 const AttributeList &Attr) {
1551 // Check the attribute arguments.
1552 if (Attr.getNumArgs() != 1) {
1553 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1554 return;
1555 }
1556
1557 Expr *Arg = Attr.getArg(0);
1558 Arg = Arg->IgnoreParenCasts();
1559 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1560
1561 // Check that it is a string.
1562 if (!Str) {
1563 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1564 return;
1565 }
1566
1567 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1569 << Attr.getName() << ExpectedTLSVar;
1570 return;
1571 }
1572
1573 // Check that the value.
1574 StringRef Model = Str->getString();
1575 if (Model != "global-dynamic" && Model != "local-dynamic"
1576 && Model != "initial-exec" && Model != "local-exec") {
1577 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1578 return;
1579 }
1580
1581 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1582 Model));
1583}
1584
Chandler Carruth1b03c872011-07-02 00:01:44 +00001585static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001586 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001587 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001588 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1589 return;
1590 }
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Chandler Carruth87c44602011-07-01 23:49:12 +00001592 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001593 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001594 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001595 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001596 return;
1597 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001598 }
1599
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001600 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001601}
1602
Chandler Carruth1b03c872011-07-02 00:01:44 +00001603static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001604 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001605 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001606 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001607
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001608 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001609}
1610
Chandler Carruth1b03c872011-07-02 00:01:44 +00001611static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001612 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001613 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001614 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001615 else
1616 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001617 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001618}
1619
Chandler Carruth1b03c872011-07-02 00:01:44 +00001620static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001621 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001622 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001623 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001624 else
1625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001626 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001627}
1628
Chandler Carruth1b03c872011-07-02 00:01:44 +00001629static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001630 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001631
1632 if (S.CheckNoReturnAttr(attr)) return;
1633
Chandler Carruth87c44602011-07-01 23:49:12 +00001634 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001635 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001636 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001637 return;
1638 }
1639
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001640 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001641}
1642
1643bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001644 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001645 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1646 attr.setInvalid();
1647 return true;
1648 }
1649
1650 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001651}
1652
Chandler Carruth1b03c872011-07-02 00:01:44 +00001653static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1654 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001655
1656 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1657 // because 'analyzer_noreturn' does not impact the type.
1658
Chandler Carruth1731e202011-07-11 23:30:35 +00001659 if(!checkAttributeNumArgs(S, Attr, 0))
1660 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001661
Chandler Carruth87c44602011-07-01 23:49:12 +00001662 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1663 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001664 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1665 && !VD->getType()->isFunctionPointerType())) {
1666 S.Diag(Attr.getLoc(),
1667 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1668 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001669 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001670 return;
1671 }
1672 }
1673
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001674 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001675}
1676
John Thompson35cc9622010-08-09 21:53:52 +00001677// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001678static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001679/*
1680 Returning a Vector Class in Registers
1681
Eric Christopherf48f3672010-12-01 22:13:54 +00001682 According to the PPU ABI specifications, a class with a single member of
1683 vector type is returned in memory when used as the return value of a function.
1684 This results in inefficient code when implementing vector classes. To return
1685 the value in a single vector register, add the vecreturn attribute to the
1686 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001687
1688 Example:
1689
1690 struct Vector
1691 {
1692 __vector float xyzw;
1693 } __attribute__((vecreturn));
1694
1695 Vector Add(Vector lhs, Vector rhs)
1696 {
1697 Vector result;
1698 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1699 return result; // This will be returned in a register
1700 }
1701*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001702 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001704 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001705 return;
1706 }
1707
Chandler Carruth87c44602011-07-01 23:49:12 +00001708 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001709 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1710 return;
1711 }
1712
Chandler Carruth87c44602011-07-01 23:49:12 +00001713 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001714 int count = 0;
1715
1716 if (!isa<CXXRecordDecl>(record)) {
1717 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1718 return;
1719 }
1720
1721 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1722 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1723 return;
1724 }
1725
Eric Christopherf48f3672010-12-01 22:13:54 +00001726 for (RecordDecl::field_iterator iter = record->field_begin();
1727 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001728 if ((count == 1) || !iter->getType()->isVectorType()) {
1729 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1730 return;
1731 }
1732 count++;
1733 }
1734
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001735 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001736}
1737
Chandler Carruth1b03c872011-07-02 00:01:44 +00001738static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001739 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001741 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001742 return;
1743 }
1744 // FIXME: Actually store the attribute on the declaration
1745}
1746
Chandler Carruth1b03c872011-07-02 00:01:44 +00001747static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001748 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001749 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001751 return;
1752 }
Mike Stumpbf916502009-07-24 19:02:52 +00001753
Chandler Carruth87c44602011-07-01 23:49:12 +00001754 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001755 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001757 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001758 return;
1759 }
Mike Stumpbf916502009-07-24 19:02:52 +00001760
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001761 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001762}
1763
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001764static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1765 const AttributeList &Attr) {
1766 // check the attribute arguments.
1767 if (Attr.hasParameterOrArguments()) {
1768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1769 return;
1770 }
1771
1772 if (!isa<FunctionDecl>(D)) {
1773 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1774 << Attr.getName() << ExpectedFunction;
1775 return;
1776 }
1777
1778 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1779}
1780
Chandler Carruth1b03c872011-07-02 00:01:44 +00001781static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001782 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001783 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001784 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1785 return;
1786 }
Mike Stumpbf916502009-07-24 19:02:52 +00001787
Chandler Carruth87c44602011-07-01 23:49:12 +00001788 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001789 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001790 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1791 return;
1792 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001793 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001795 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001796 return;
1797 }
Mike Stumpbf916502009-07-24 19:02:52 +00001798
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001799 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001800}
1801
Chandler Carruth1b03c872011-07-02 00:01:44 +00001802static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001803 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001804 if (Attr.getNumArgs() > 1) {
1805 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001806 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001807 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001808
1809 int priority = 65535; // FIXME: Do not hardcode such constants.
1810 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001811 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001812 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001813 if (E->isTypeDependent() || E->isValueDependent() ||
1814 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001816 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001817 return;
1818 }
1819 priority = Idx.getZExtValue();
1820 }
Mike Stumpbf916502009-07-24 19:02:52 +00001821
Chandler Carruth87c44602011-07-01 23:49:12 +00001822 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001824 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001825 return;
1826 }
1827
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001828 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001829 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001830}
1831
Chandler Carruth1b03c872011-07-02 00:01:44 +00001832static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001833 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001834 if (Attr.getNumArgs() > 1) {
1835 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001836 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001837 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001838
1839 int priority = 65535; // FIXME: Do not hardcode such constants.
1840 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001841 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001842 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001843 if (E->isTypeDependent() || E->isValueDependent() ||
1844 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001846 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001847 return;
1848 }
1849 priority = Idx.getZExtValue();
1850 }
Mike Stumpbf916502009-07-24 19:02:52 +00001851
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001854 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001855 return;
1856 }
1857
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001858 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001859 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001860}
1861
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001862template <typename AttrTy>
1863static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1864 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001865 unsigned NumArgs = Attr.getNumArgs();
1866 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001867 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001868 return;
1869 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001870
1871 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001872 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001873 if (NumArgs == 1) {
1874 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001875 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001876 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001877 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001878 return;
1879 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001880 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001881 }
Mike Stumpbf916502009-07-24 19:02:52 +00001882
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001883 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001884}
1885
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001886static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1887 const AttributeList &Attr) {
1888 unsigned NumArgs = Attr.getNumArgs();
1889 if (NumArgs > 0) {
1890 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1891 return;
1892 }
1893
1894 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001895 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001896}
1897
Patrick Beardb2f68202012-04-06 18:12:22 +00001898static void handleObjCRootClassAttr(Sema &S, Decl *D,
1899 const AttributeList &Attr) {
1900 if (!isa<ObjCInterfaceDecl>(D)) {
1901 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1902 return;
1903 }
1904
1905 unsigned NumArgs = Attr.getNumArgs();
1906 if (NumArgs > 0) {
1907 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1908 return;
1909 }
1910
1911 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1912}
1913
Ted Kremenek71207fc2012-01-05 22:47:47 +00001914static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001915 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001916 if (!isa<ObjCInterfaceDecl>(D)) {
1917 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1918 return;
1919 }
1920
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001921 unsigned NumArgs = Attr.getNumArgs();
1922 if (NumArgs > 0) {
1923 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1924 return;
1925 }
1926
Ted Kremenek71207fc2012-01-05 22:47:47 +00001927 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001928 Attr.getRange(), S.Context));
1929}
1930
Jordy Rosefad5de92012-05-08 03:27:22 +00001931static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1932 IdentifierInfo *Platform,
1933 VersionTuple Introduced,
1934 VersionTuple Deprecated,
1935 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001936 StringRef PlatformName
1937 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1938 if (PlatformName.empty())
1939 PlatformName = Platform->getName();
1940
1941 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1942 // of these steps are needed).
1943 if (!Introduced.empty() && !Deprecated.empty() &&
1944 !(Introduced <= Deprecated)) {
1945 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1946 << 1 << PlatformName << Deprecated.getAsString()
1947 << 0 << Introduced.getAsString();
1948 return true;
1949 }
1950
1951 if (!Introduced.empty() && !Obsoleted.empty() &&
1952 !(Introduced <= Obsoleted)) {
1953 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1954 << 2 << PlatformName << Obsoleted.getAsString()
1955 << 0 << Introduced.getAsString();
1956 return true;
1957 }
1958
1959 if (!Deprecated.empty() && !Obsoleted.empty() &&
1960 !(Deprecated <= Obsoleted)) {
1961 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1962 << 2 << PlatformName << Obsoleted.getAsString()
1963 << 1 << Deprecated.getAsString();
1964 return true;
1965 }
1966
1967 return false;
1968}
1969
Rafael Espindola599f1b72012-05-13 03:25:18 +00001970AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1971 IdentifierInfo *Platform,
1972 VersionTuple Introduced,
1973 VersionTuple Deprecated,
1974 VersionTuple Obsoleted,
1975 bool IsUnavailable,
1976 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00001977 VersionTuple MergedIntroduced = Introduced;
1978 VersionTuple MergedDeprecated = Deprecated;
1979 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00001980 bool FoundAny = false;
1981
Rafael Espindola98ae8342012-05-10 02:50:16 +00001982 if (D->hasAttrs()) {
1983 AttrVec &Attrs = D->getAttrs();
1984 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1985 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1986 if (!OldAA) {
1987 ++i;
1988 continue;
1989 }
Rafael Espindola3b294362012-05-06 19:56:25 +00001990
Rafael Espindola98ae8342012-05-10 02:50:16 +00001991 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1992 if (OldPlatform != Platform) {
1993 ++i;
1994 continue;
1995 }
1996
1997 FoundAny = true;
1998 VersionTuple OldIntroduced = OldAA->getIntroduced();
1999 VersionTuple OldDeprecated = OldAA->getDeprecated();
2000 VersionTuple OldObsoleted = OldAA->getObsoleted();
2001 bool OldIsUnavailable = OldAA->getUnavailable();
2002 StringRef OldMessage = OldAA->getMessage();
2003
2004 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2005 OldIntroduced != Introduced) ||
2006 (!OldDeprecated.empty() && !Deprecated.empty() &&
2007 OldDeprecated != Deprecated) ||
2008 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2009 OldObsoleted != Obsoleted) ||
2010 (OldIsUnavailable != IsUnavailable) ||
2011 (OldMessage != Message)) {
2012 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2013 Diag(Range.getBegin(), diag::note_previous_attribute);
2014 Attrs.erase(Attrs.begin() + i);
2015 --e;
2016 continue;
2017 }
2018
2019 VersionTuple MergedIntroduced2 = MergedIntroduced;
2020 VersionTuple MergedDeprecated2 = MergedDeprecated;
2021 VersionTuple MergedObsoleted2 = MergedObsoleted;
2022
2023 if (MergedIntroduced2.empty())
2024 MergedIntroduced2 = OldIntroduced;
2025 if (MergedDeprecated2.empty())
2026 MergedDeprecated2 = OldDeprecated;
2027 if (MergedObsoleted2.empty())
2028 MergedObsoleted2 = OldObsoleted;
2029
2030 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2031 MergedIntroduced2, MergedDeprecated2,
2032 MergedObsoleted2)) {
2033 Attrs.erase(Attrs.begin() + i);
2034 --e;
2035 continue;
2036 }
2037
2038 MergedIntroduced = MergedIntroduced2;
2039 MergedDeprecated = MergedDeprecated2;
2040 MergedObsoleted = MergedObsoleted2;
2041 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002042 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002043 }
2044
2045 if (FoundAny &&
2046 MergedIntroduced == Introduced &&
2047 MergedDeprecated == Deprecated &&
2048 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002049 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002050
Rafael Espindola98ae8342012-05-10 02:50:16 +00002051 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002052 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002053 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2054 Introduced, Deprecated,
2055 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002056 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002057 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002058}
2059
Chandler Carruth1b03c872011-07-02 00:01:44 +00002060static void handleAvailabilityAttr(Sema &S, Decl *D,
2061 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002062 IdentifierInfo *Platform = Attr.getParameterName();
2063 SourceLocation PlatformLoc = Attr.getParameterLoc();
2064
Rafael Espindola3b294362012-05-06 19:56:25 +00002065 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002066 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2067 << Platform;
2068
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002069 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2070 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2071 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002072 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002073 StringRef Str;
2074 const StringLiteral *SE =
2075 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2076 if (SE)
2077 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002078
Rafael Espindola599f1b72012-05-13 03:25:18 +00002079 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2080 Platform,
2081 Introduced.Version,
2082 Deprecated.Version,
2083 Obsoleted.Version,
2084 IsUnavailable, Str);
2085 if (NewAttr)
2086 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002087}
2088
Rafael Espindola599f1b72012-05-13 03:25:18 +00002089VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2090 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002091 if (isa<TypedefNameDecl>(D)) {
2092 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002093 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002094 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002095 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2096 if (ExistingAttr) {
2097 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2098 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002099 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002100 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2101 Diag(Range.getBegin(), diag::note_previous_attribute);
2102 D->dropAttr<VisibilityAttr>();
2103 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002104 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002105}
2106
Chandler Carruth1b03c872011-07-02 00:01:44 +00002107static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002108 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002109 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002110 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002111
Peter Collingbourne7a730022010-11-23 20:45:58 +00002112 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002113 Arg = Arg->IgnoreParenCasts();
2114 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002115
Douglas Gregor5cee1192011-07-27 05:40:30 +00002116 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002118 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002119 return;
2120 }
Mike Stumpbf916502009-07-24 19:02:52 +00002121
Chris Lattner5f9e2722011-07-23 10:55:15 +00002122 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002123 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002124
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002125 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002126 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002127 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002128 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002129 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002130 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002131 else if (TypeStr == "protected") {
2132 // Complain about attempts to use protected visibility on targets
2133 // (like Darwin) that don't support it.
2134 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2135 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2136 type = VisibilityAttr::Default;
2137 } else {
2138 type = VisibilityAttr::Protected;
2139 }
2140 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002141 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002142 return;
2143 }
Mike Stumpbf916502009-07-24 19:02:52 +00002144
Rafael Espindola599f1b72012-05-13 03:25:18 +00002145 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2146 if (NewAttr)
2147 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002148}
2149
Chandler Carruth1b03c872011-07-02 00:01:44 +00002150static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2151 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002152 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2153 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002154 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002155 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002156 return;
2157 }
2158
Chandler Carruth87c44602011-07-01 23:49:12 +00002159 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2160 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002162 << "objc_method_family" << 1;
2163 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002164 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002165 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002166 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002167 return;
2168 }
2169
Chris Lattner5f9e2722011-07-23 10:55:15 +00002170 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002171 ObjCMethodFamilyAttr::FamilyKind family;
2172 if (param == "none")
2173 family = ObjCMethodFamilyAttr::OMF_None;
2174 else if (param == "alloc")
2175 family = ObjCMethodFamilyAttr::OMF_alloc;
2176 else if (param == "copy")
2177 family = ObjCMethodFamilyAttr::OMF_copy;
2178 else if (param == "init")
2179 family = ObjCMethodFamilyAttr::OMF_init;
2180 else if (param == "mutableCopy")
2181 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2182 else if (param == "new")
2183 family = ObjCMethodFamilyAttr::OMF_new;
2184 else {
2185 // Just warn and ignore it. This is future-proof against new
2186 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002187 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002188 return;
2189 }
2190
John McCallf85e1932011-06-15 23:02:42 +00002191 if (family == ObjCMethodFamilyAttr::OMF_init &&
2192 !method->getResultType()->isObjCObjectPointerType()) {
2193 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2194 << method->getResultType();
2195 // Ignore the attribute.
2196 return;
2197 }
2198
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002199 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002200 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002201}
2202
Chandler Carruth1b03c872011-07-02 00:01:44 +00002203static void handleObjCExceptionAttr(Sema &S, Decl *D,
2204 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002205 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002206 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002207
Chris Lattner0db29ec2009-02-14 08:09:34 +00002208 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2209 if (OCI == 0) {
2210 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2211 return;
2212 }
Mike Stumpbf916502009-07-24 19:02:52 +00002213
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002214 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002215}
2216
Chandler Carruth1b03c872011-07-02 00:01:44 +00002217static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002218 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002219 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002220 return;
2221 }
Richard Smith162e1c12011-04-15 14:24:37 +00002222 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002223 QualType T = TD->getUnderlyingType();
2224 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002225 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002226 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2227 return;
2228 }
2229 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002230 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2231 QualType T = PD->getType();
2232 if (!T->isPointerType() ||
2233 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
2234 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2235 return;
2236 }
2237 }
2238 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002239 // It is okay to include this attribute on properties, e.g.:
2240 //
2241 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2242 //
2243 // In this case it follows tradition and suppresses an error in the above
2244 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002245 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002246 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002247 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002248}
2249
Mike Stumpbf916502009-07-24 19:02:52 +00002250static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002251handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002252 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002254 return;
2255 }
2256
2257 if (!isa<FunctionDecl>(D)) {
2258 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2259 return;
2260 }
2261
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002262 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002263}
2264
Chandler Carruth1b03c872011-07-02 00:01:44 +00002265static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002266 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002267 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002268 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002269 return;
2270 }
Mike Stumpbf916502009-07-24 19:02:52 +00002271
Steve Naroff9eae5762008-09-18 16:44:58 +00002272 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002274 return;
2275 }
Mike Stumpbf916502009-07-24 19:02:52 +00002276
Sean Huntcf807c42010-08-18 23:23:40 +00002277 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002278 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002279 type = BlocksAttr::ByRef;
2280 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002281 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002282 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002283 return;
2284 }
Mike Stumpbf916502009-07-24 19:02:52 +00002285
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002286 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002287}
2288
Chandler Carruth1b03c872011-07-02 00:01:44 +00002289static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002290 // check the attribute arguments.
2291 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002292 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002293 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002294 }
2295
John McCall3323fad2011-09-09 07:56:05 +00002296 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002297 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002298 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002299 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002300 if (E->isTypeDependent() || E->isValueDependent() ||
2301 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002302 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002303 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002304 return;
2305 }
Mike Stumpbf916502009-07-24 19:02:52 +00002306
John McCall3323fad2011-09-09 07:56:05 +00002307 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002308 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2309 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002310 return;
2311 }
John McCall3323fad2011-09-09 07:56:05 +00002312
2313 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002314 }
2315
John McCall3323fad2011-09-09 07:56:05 +00002316 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002317 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002318 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002319 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002320 if (E->isTypeDependent() || E->isValueDependent() ||
2321 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002323 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002324 return;
2325 }
2326 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002327
John McCall3323fad2011-09-09 07:56:05 +00002328 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002329 // FIXME: This error message could be improved, it would be nice
2330 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002331 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2332 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002333 return;
2334 }
2335 }
2336
Chandler Carruth87c44602011-07-01 23:49:12 +00002337 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002338 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002339 if (isa<FunctionNoProtoType>(FT)) {
2340 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2341 return;
2342 }
Mike Stumpbf916502009-07-24 19:02:52 +00002343
Chris Lattner897cd902009-03-17 23:03:47 +00002344 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002345 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002346 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002347 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002348 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002349 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002350 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002351 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002352 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002353 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2354 if (!BD->isVariadic()) {
2355 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2356 return;
2357 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002358 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002359 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002360 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002361 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002362 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002363 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002364 int m = Ty->isFunctionPointerType() ? 0 : 1;
2365 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002366 return;
2367 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002368 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002370 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002371 return;
2372 }
Anders Carlsson77091822008-10-05 18:05:59 +00002373 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002374 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002375 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002376 return;
2377 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002378 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002379 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002380}
2381
Chandler Carruth1b03c872011-07-02 00:01:44 +00002382static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002383 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002384 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002385 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002386
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002387 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002388 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002389 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002390 return;
2391 }
Mike Stumpbf916502009-07-24 19:02:52 +00002392
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002393 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2394 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2395 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002396 return;
2397 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002398 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2399 if (MD->getResultType()->isVoidType()) {
2400 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2401 << Attr.getName() << 1;
2402 return;
2403 }
2404
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002405 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002406}
2407
Chandler Carruth1b03c872011-07-02 00:01:44 +00002408static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002409 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002410 if (Attr.hasParameterOrArguments()) {
2411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002412 return;
2413 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002414
Chandler Carruth87c44602011-07-01 23:49:12 +00002415 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002416 if (isa<CXXRecordDecl>(D)) {
2417 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2418 return;
2419 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2421 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002422 return;
2423 }
2424
Chandler Carruth87c44602011-07-01 23:49:12 +00002425 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002426
2427 // 'weak' only applies to declarations with external linkage.
2428 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002429 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002430 return;
2431 }
Mike Stumpbf916502009-07-24 19:02:52 +00002432
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002433 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002434}
2435
Chandler Carruth1b03c872011-07-02 00:01:44 +00002436static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002437 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002438 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002439 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002440
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002441
2442 // weak_import only applies to variable & function declarations.
2443 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002444 if (!D->canBeWeakImported(isDef)) {
2445 if (isDef)
2446 S.Diag(Attr.getLoc(),
2447 diag::warn_attribute_weak_import_invalid_on_definition)
2448 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002449 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002450 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002451 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002452 // Nothing to warn about here.
2453 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002454 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002455 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002456
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002457 return;
2458 }
2459
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002460 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002461}
2462
Tanya Lattner0df579e2012-07-09 22:06:01 +00002463// Handles reqd_work_group_size and work_group_size_hint.
2464static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002465 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002466 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2467 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2468
Nate Begeman6f3d8382009-06-26 06:32:41 +00002469 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002470 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002471
2472 unsigned WGSize[3];
2473 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002474 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002475 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002476 if (E->isTypeDependent() || E->isValueDependent() ||
2477 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002478 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002479 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002480 return;
2481 }
2482 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2483 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002484
2485 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2486 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2487 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2488 if (!(A->getXDim() == WGSize[0] &&
2489 A->getYDim() == WGSize[1] &&
2490 A->getZDim() == WGSize[2])) {
2491 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2492 Attr.getName();
2493 }
2494 }
2495
2496 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2497 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2498 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2499 if (!(A->getXDim() == WGSize[0] &&
2500 A->getYDim() == WGSize[1] &&
2501 A->getZDim() == WGSize[2])) {
2502 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2503 Attr.getName();
2504 }
2505 }
2506
2507 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2508 D->addAttr(::new (S.Context)
2509 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2510 WGSize[0], WGSize[1], WGSize[2]));
2511 else
2512 D->addAttr(::new (S.Context)
2513 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2514 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002515}
2516
Rafael Espindola599f1b72012-05-13 03:25:18 +00002517SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2518 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002519 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2520 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002521 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002522 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2523 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002524 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002525 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002526 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002527}
2528
Chandler Carruth1b03c872011-07-02 00:01:44 +00002529static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002530 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002531 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002532 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002533
2534 // Make sure that there is a string literal as the sections's single
2535 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002536 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002537 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002538 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002539 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002540 return;
2541 }
Mike Stump1eb44332009-09-09 15:08:12 +00002542
Chris Lattner797c3c42009-08-10 19:03:04 +00002543 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002544 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002545 if (!Error.empty()) {
2546 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2547 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002548 return;
2549 }
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002551 // This attribute cannot be applied to local variables.
2552 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2553 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2554 return;
2555 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002556 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2557 SE->getString());
2558 if (NewAttr)
2559 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002560}
2561
Chris Lattner6b6b5372008-06-26 18:38:35 +00002562
Chandler Carruth1b03c872011-07-02 00:01:44 +00002563static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002564 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002565 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002566 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002567 return;
2568 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002569
Chandler Carruth87c44602011-07-01 23:49:12 +00002570 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002571 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002572 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002573 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002574 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002575 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002576}
2577
Chandler Carruth1b03c872011-07-02 00:01:44 +00002578static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002579 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002580 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002582 return;
2583 }
Mike Stumpbf916502009-07-24 19:02:52 +00002584
Chandler Carruth87c44602011-07-01 23:49:12 +00002585 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002586 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002587 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002588 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002589 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002590 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002591}
2592
Chandler Carruth1b03c872011-07-02 00:01:44 +00002593static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002594 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002595 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002596 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002597
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002598 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002599}
2600
Chandler Carruth1b03c872011-07-02 00:01:44 +00002601static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002602 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2604 return;
2605 }
Mike Stumpbf916502009-07-24 19:02:52 +00002606
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002607 if (Attr.getNumArgs() != 0) {
2608 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2609 return;
2610 }
Mike Stumpbf916502009-07-24 19:02:52 +00002611
Chandler Carruth87c44602011-07-01 23:49:12 +00002612 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002613
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002614 if (!VD || !VD->hasLocalStorage()) {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2616 return;
2617 }
Mike Stumpbf916502009-07-24 19:02:52 +00002618
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002619 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002620 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002621 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002622 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2623 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002624 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002625 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002626 Attr.getParameterName();
2627 return;
2628 }
Mike Stumpbf916502009-07-24 19:02:52 +00002629
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002630 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2631 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002632 S.Diag(Attr.getParameterLoc(),
2633 diag::err_attribute_cleanup_arg_not_function)
2634 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002635 return;
2636 }
2637
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002638 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002639 S.Diag(Attr.getParameterLoc(),
2640 diag::err_attribute_cleanup_func_must_take_one_arg)
2641 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002642 return;
2643 }
Mike Stumpbf916502009-07-24 19:02:52 +00002644
Anders Carlsson89941c12009-02-07 23:16:50 +00002645 // We're currently more strict than GCC about what function types we accept.
2646 // If this ever proves to be a problem it should be easy to fix.
2647 QualType Ty = S.Context.getPointerType(VD->getType());
2648 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002649 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2650 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002651 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002652 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2653 Attr.getParameterName() << ParamTy << Ty;
2654 return;
2655 }
Mike Stumpbf916502009-07-24 19:02:52 +00002656
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002657 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002658 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002659}
2660
Mike Stumpbf916502009-07-24 19:02:52 +00002661/// Handle __attribute__((format_arg((idx)))) attribute based on
2662/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002663static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002664 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002665 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002666
Chandler Carruth87c44602011-07-01 23:49:12 +00002667 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002669 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002670 return;
2671 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002672
2673 // In C++ the implicit 'this' function parameter also counts, and they are
2674 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002675 bool HasImplicitThisParam = isInstanceMethod(D);
2676 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002677 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002678
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002679 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002680 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002681 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002682 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2683 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2685 << "format" << 2 << IdxExpr->getSourceRange();
2686 return;
2687 }
Mike Stumpbf916502009-07-24 19:02:52 +00002688
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002689 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2690 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2691 << "format" << 2 << IdxExpr->getSourceRange();
2692 return;
2693 }
Mike Stumpbf916502009-07-24 19:02:52 +00002694
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002695 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002696
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002697 if (HasImplicitThisParam) {
2698 if (ArgIdx == 0) {
2699 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2700 << "format_arg" << IdxExpr->getSourceRange();
2701 return;
2702 }
2703 ArgIdx--;
2704 }
2705
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002706 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002707 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002708
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002709 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2710 if (not_nsstring_type &&
2711 !isCFStringType(Ty, S.Context) &&
2712 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002713 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002714 // FIXME: Should highlight the actual expression that has the wrong type.
2715 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002716 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002717 << IdxExpr->getSourceRange();
2718 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002719 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002720 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002721 if (!isNSStringType(Ty, S.Context) &&
2722 !isCFStringType(Ty, S.Context) &&
2723 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002724 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002725 // FIXME: Should highlight the actual expression that has the wrong type.
2726 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002727 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002728 << IdxExpr->getSourceRange();
2729 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002730 }
2731
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002732 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002733 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002734}
2735
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002736enum FormatAttrKind {
2737 CFStringFormat,
2738 NSStringFormat,
2739 StrftimeFormat,
2740 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002741 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002742 InvalidFormat
2743};
2744
2745/// getFormatAttrKind - Map from format attribute names to supported format
2746/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002747static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002748 return llvm::StringSwitch<FormatAttrKind>(Format)
2749 // Check for formats that get handled specially.
2750 .Case("NSString", NSStringFormat)
2751 .Case("CFString", CFStringFormat)
2752 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002753
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002754 // Otherwise, check for supported formats.
2755 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2756 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2757 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002758
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002759 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2760 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002761}
2762
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002763/// Handle __attribute__((init_priority(priority))) attributes based on
2764/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002765static void handleInitPriorityAttr(Sema &S, Decl *D,
2766 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002767 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002768 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2769 return;
2770 }
2771
Chandler Carruth87c44602011-07-01 23:49:12 +00002772 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002773 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2774 Attr.setInvalid();
2775 return;
2776 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002777 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002778 if (S.Context.getAsArrayType(T))
2779 T = S.Context.getBaseElementType(T);
2780 if (!T->getAs<RecordType>()) {
2781 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2782 Attr.setInvalid();
2783 return;
2784 }
2785
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002786 if (Attr.getNumArgs() != 1) {
2787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2788 Attr.setInvalid();
2789 return;
2790 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002791 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002792
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002793 llvm::APSInt priority(32);
2794 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2795 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2796 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2797 << "init_priority" << priorityExpr->getSourceRange();
2798 Attr.setInvalid();
2799 return;
2800 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002801 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002802 if (prioritynum < 101 || prioritynum > 65535) {
2803 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2804 << priorityExpr->getSourceRange();
2805 Attr.setInvalid();
2806 return;
2807 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002808 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002809 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002810}
2811
Rafael Espindola599f1b72012-05-13 03:25:18 +00002812FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2813 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002814 // Check whether we already have an equivalent format attribute.
2815 for (specific_attr_iterator<FormatAttr>
2816 i = D->specific_attr_begin<FormatAttr>(),
2817 e = D->specific_attr_end<FormatAttr>();
2818 i != e ; ++i) {
2819 FormatAttr *f = *i;
2820 if (f->getType() == Format &&
2821 f->getFormatIdx() == FormatIdx &&
2822 f->getFirstArg() == FirstArg) {
2823 // If we don't have a valid location for this attribute, adopt the
2824 // location.
2825 if (f->getLocation().isInvalid())
2826 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002827 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002828 }
2829 }
2830
Rafael Espindola599f1b72012-05-13 03:25:18 +00002831 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2832 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002833}
2834
Mike Stumpbf916502009-07-24 19:02:52 +00002835/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2836/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002837static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002838
Chris Lattner545dd342008-06-28 23:36:30 +00002839 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002840 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002841 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002842 return;
2843 }
2844
Chris Lattner545dd342008-06-28 23:36:30 +00002845 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002847 return;
2848 }
2849
Chandler Carruth87c44602011-07-01 23:49:12 +00002850 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002851 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002852 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002853 return;
2854 }
2855
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002856 // In C++ the implicit 'this' function parameter also counts, and they are
2857 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002858 bool HasImplicitThisParam = isInstanceMethod(D);
2859 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002860 unsigned FirstIdx = 1;
2861
Chris Lattner5f9e2722011-07-23 10:55:15 +00002862 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002863
2864 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002865 if (Format.startswith("__") && Format.endswith("__"))
2866 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002867
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002868 // Check for supported formats.
2869 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002870
2871 if (Kind == IgnoredFormat)
2872 return;
2873
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002874 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002875 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002876 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002877 return;
2878 }
2879
2880 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002881 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002882 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002883 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2884 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002885 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002886 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002887 return;
2888 }
2889
2890 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002891 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002892 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002893 return;
2894 }
2895
2896 // FIXME: Do we need to bounds check?
2897 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002898
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002899 if (HasImplicitThisParam) {
2900 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002901 S.Diag(Attr.getLoc(),
2902 diag::err_format_attribute_implicit_this_format_string)
2903 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002904 return;
2905 }
2906 ArgIdx--;
2907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Chris Lattner6b6b5372008-06-26 18:38:35 +00002909 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002910 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002911
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002912 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002913 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002914 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2915 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002916 return;
2917 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002918 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002919 // FIXME: do we need to check if the type is NSString*? What are the
2920 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002921 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002922 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002923 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2924 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002925 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002926 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002927 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002928 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002929 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002930 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2931 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002932 return;
2933 }
2934
2935 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002936 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002937 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002938 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2939 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002940 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002941 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002942 return;
2943 }
2944
2945 // check if the function is variadic if the 3rd argument non-zero
2946 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002947 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002948 ++NumArgs; // +1 for ...
2949 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002950 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002951 return;
2952 }
2953 }
2954
Chris Lattner3c73c412008-11-19 08:23:25 +00002955 // strftime requires FirstArg to be 0 because it doesn't read from any
2956 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002957 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002958 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002959 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2960 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002961 return;
2962 }
2963 // if 0 it disables parameter checking (to use with e.g. va_list)
2964 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002965 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002966 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002967 return;
2968 }
2969
Rafael Espindola599f1b72012-05-13 03:25:18 +00002970 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
2971 Idx.getZExtValue(),
2972 FirstArg.getZExtValue());
2973 if (NewAttr)
2974 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002975}
2976
Chandler Carruth1b03c872011-07-02 00:01:44 +00002977static void handleTransparentUnionAttr(Sema &S, Decl *D,
2978 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002979 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002980 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002981 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002982
Chris Lattner6b6b5372008-06-26 18:38:35 +00002983
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002984 // Try to find the underlying union declaration.
2985 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002986 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002987 if (TD && TD->getUnderlyingType()->isUnionType())
2988 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2989 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002990 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002991
2992 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002993 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002994 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002995 return;
2996 }
2997
John McCall5e1cdac2011-10-07 06:10:15 +00002998 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002999 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003000 diag::warn_transparent_union_attribute_not_definition);
3001 return;
3002 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003003
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003004 RecordDecl::field_iterator Field = RD->field_begin(),
3005 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003006 if (Field == FieldEnd) {
3007 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3008 return;
3009 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003010
David Blaikie581deb32012-06-06 20:45:41 +00003011 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003012 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003013 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003014 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003015 diag::warn_transparent_union_attribute_floating)
3016 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003017 return;
3018 }
3019
3020 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3021 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3022 for (; Field != FieldEnd; ++Field) {
3023 QualType FieldType = Field->getType();
3024 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3025 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3026 // Warn if we drop the attribute.
3027 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003028 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003029 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003030 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003031 diag::warn_transparent_union_attribute_field_size_align)
3032 << isSize << Field->getDeclName() << FieldBits;
3033 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003034 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003035 diag::note_transparent_union_first_field_size_align)
3036 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003037 return;
3038 }
3039 }
3040
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003041 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003042}
3043
Chandler Carruth1b03c872011-07-02 00:01:44 +00003044static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003045 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003046 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003047 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003048
Peter Collingbourne7a730022010-11-23 20:45:58 +00003049 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003050 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003051
Chris Lattner6b6b5372008-06-26 18:38:35 +00003052 // Make sure that there is a string literal as the annotation's single
3053 // argument.
3054 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003055 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003056 return;
3057 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003058
3059 // Don't duplicate annotations that are already set.
3060 for (specific_attr_iterator<AnnotateAttr>
3061 i = D->specific_attr_begin<AnnotateAttr>(),
3062 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3063 if ((*i)->getAnnotation() == SE->getString())
3064 return;
3065 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003066 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003067 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003068}
3069
Chandler Carruth1b03c872011-07-02 00:01:44 +00003070static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003071 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003072 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003073 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003074 return;
3075 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003076
Sean Huntbbd37c62009-11-21 08:43:09 +00003077 //FIXME: The C++0x version of this attribute has more limited applicabilty
3078 // than GNU's, and should error out when it is used to specify a
3079 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003080
Chris Lattner545dd342008-06-28 23:36:30 +00003081 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003082 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3083 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003084 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003085 }
Mike Stumpbf916502009-07-24 19:02:52 +00003086
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003087 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3088 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003089}
3090
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003091void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3092 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003093 // FIXME: Handle pack-expansions here.
3094 if (DiagnoseUnexpandedParameterPack(E))
3095 return;
3096
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003097 if (E->isTypeDependent() || E->isValueDependent()) {
3098 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003099 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3100 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003101 return;
3102 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003103
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003104 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003105 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003106 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003107 ExprResult ICE
3108 = VerifyIntegerConstantExpression(E, &Alignment,
3109 diag::err_aligned_attribute_argument_not_int,
3110 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003111 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003112 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003113 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003114 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3115 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003116 return;
3117 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003118 if (isDeclSpec) {
3119 // We've already verified it's a power of 2, now let's make sure it's
3120 // 8192 or less.
3121 if (Alignment.getZExtValue() > 8192) {
3122 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3123 << E->getSourceRange();
3124 return;
3125 }
3126 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003127
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003128 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3129 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003130}
3131
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003132void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3133 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003134 // FIXME: Cache the number on the Attr object if non-dependent?
3135 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003136 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3137 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003138 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003139}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003140
Chandler Carruthd309c812011-07-01 23:49:16 +00003141/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003142/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003143///
Mike Stumpbf916502009-07-24 19:02:52 +00003144/// Despite what would be logical, the mode attribute is a decl attribute, not a
3145/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3146/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003147static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003148 // This attribute isn't documented, but glibc uses it. It changes
3149 // the width of an int or unsigned int to the specified size.
3150
3151 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003152 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003153 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003154
Chris Lattnerfbf13472008-06-27 22:18:37 +00003155
3156 IdentifierInfo *Name = Attr.getParameterName();
3157 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003158 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003159 return;
3160 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003161
Chris Lattner5f9e2722011-07-23 10:55:15 +00003162 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003163
3164 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003165 if (Str.startswith("__") && Str.endswith("__"))
3166 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003167
3168 unsigned DestWidth = 0;
3169 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003170 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003171 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003172 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003173 switch (Str[0]) {
3174 case 'Q': DestWidth = 8; break;
3175 case 'H': DestWidth = 16; break;
3176 case 'S': DestWidth = 32; break;
3177 case 'D': DestWidth = 64; break;
3178 case 'X': DestWidth = 96; break;
3179 case 'T': DestWidth = 128; break;
3180 }
3181 if (Str[1] == 'F') {
3182 IntegerMode = false;
3183 } else if (Str[1] == 'C') {
3184 IntegerMode = false;
3185 ComplexMode = true;
3186 } else if (Str[1] != 'I') {
3187 DestWidth = 0;
3188 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003189 break;
3190 case 4:
3191 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3192 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003193 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003194 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003195 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003196 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003197 break;
3198 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003199 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003200 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003201 break;
3202 }
3203
3204 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003205 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003206 OldTy = TD->getUnderlyingType();
3207 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3208 OldTy = VD->getType();
3209 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003210 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003211 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003212 return;
3213 }
Eli Friedman73397492009-03-03 06:41:03 +00003214
John McCall183700f2009-09-21 23:43:11 +00003215 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003216 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3217 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003218 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003219 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3220 } else if (ComplexMode) {
3221 if (!OldTy->isComplexType())
3222 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3223 } else {
3224 if (!OldTy->isFloatingType())
3225 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3226 }
3227
Mike Stump390b4cc2009-05-16 07:39:55 +00003228 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3229 // and friends, at least with glibc.
3230 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3231 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003232 // FIXME: Make sure floating-point mappings are accurate
3233 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003234 QualType NewTy;
3235 switch (DestWidth) {
3236 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003237 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003238 return;
3239 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003240 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003241 return;
3242 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003243 if (!IntegerMode) {
3244 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3245 return;
3246 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003247 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003248 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003249 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003250 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003251 break;
3252 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003253 if (!IntegerMode) {
3254 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3255 return;
3256 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003257 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003258 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003259 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003260 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003261 break;
3262 case 32:
3263 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003264 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003265 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003266 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003267 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003268 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003269 break;
3270 case 64:
3271 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003272 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003273 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003274 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003275 NewTy = S.Context.LongTy;
3276 else
3277 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003278 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003279 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003280 NewTy = S.Context.UnsignedLongTy;
3281 else
3282 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003283 break;
Eli Friedman73397492009-03-03 06:41:03 +00003284 case 96:
3285 NewTy = S.Context.LongDoubleTy;
3286 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003287 case 128:
3288 if (!IntegerMode) {
3289 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3290 return;
3291 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003292 if (OldTy->isSignedIntegerType())
3293 NewTy = S.Context.Int128Ty;
3294 else
3295 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003296 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003297 }
3298
Eli Friedman73397492009-03-03 06:41:03 +00003299 if (ComplexMode) {
3300 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003301 }
3302
3303 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003304 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003305 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003306 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003307 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003308 cast<ValueDecl>(D)->setType(NewTy);
3309}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003310
Chandler Carruth1b03c872011-07-02 00:01:44 +00003311static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003312 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003313 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003314 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003315
Nick Lewycky78d1a102012-07-24 01:40:49 +00003316 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3317 if (!VD->hasGlobalStorage())
3318 S.Diag(Attr.getLoc(),
3319 diag::warn_attribute_requires_functions_or_static_globals)
3320 << Attr.getName();
3321 } else if (!isFunctionOrMethod(D)) {
3322 S.Diag(Attr.getLoc(),
3323 diag::warn_attribute_requires_functions_or_static_globals)
3324 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003325 return;
3326 }
Mike Stumpbf916502009-07-24 19:02:52 +00003327
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003328 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003329}
3330
Chandler Carruth1b03c872011-07-02 00:01:44 +00003331static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003332 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003333 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003334 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003335
Mike Stumpbf916502009-07-24 19:02:52 +00003336
Chandler Carruth87c44602011-07-01 23:49:12 +00003337 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003338 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003339 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003340 return;
3341 }
Mike Stumpbf916502009-07-24 19:02:52 +00003342
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003343 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003344}
3345
Chandler Carruth1b03c872011-07-02 00:01:44 +00003346static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3347 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003348 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003349 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003350 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003351
Chris Lattner7255a2d2010-06-22 00:03:40 +00003352
Chandler Carruth87c44602011-07-01 23:49:12 +00003353 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003355 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003356 return;
3357 }
3358
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003359 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003360 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003361}
3362
Chandler Carruth1b03c872011-07-02 00:01:44 +00003363static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003364 if (S.LangOpts.CUDA) {
3365 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003366 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003367 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3368 return;
3369 }
3370
Chandler Carruth87c44602011-07-01 23:49:12 +00003371 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003372 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003373 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003374 return;
3375 }
3376
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003377 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003378 } else {
3379 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3380 }
3381}
3382
Chandler Carruth1b03c872011-07-02 00:01:44 +00003383static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003384 if (S.LangOpts.CUDA) {
3385 // check the attribute arguments.
3386 if (Attr.getNumArgs() != 0) {
3387 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3388 return;
3389 }
3390
Chandler Carruth87c44602011-07-01 23:49:12 +00003391 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003392 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003393 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003394 return;
3395 }
3396
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003397 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003398 } else {
3399 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3400 }
3401}
3402
Chandler Carruth1b03c872011-07-02 00:01:44 +00003403static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003404 if (S.LangOpts.CUDA) {
3405 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003406 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003407 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003408
Chandler Carruth87c44602011-07-01 23:49:12 +00003409 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003411 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003412 return;
3413 }
3414
Chandler Carruth87c44602011-07-01 23:49:12 +00003415 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003416 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003417 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003418 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3419 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3420 << FD->getType()
3421 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3422 "void");
3423 } else {
3424 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3425 << FD->getType();
3426 }
3427 return;
3428 }
3429
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003430 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003431 } else {
3432 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3433 }
3434}
3435
Chandler Carruth1b03c872011-07-02 00:01:44 +00003436static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003437 if (S.LangOpts.CUDA) {
3438 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003439 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003440 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003441
Peter Collingbourneced76712010-12-01 03:15:31 +00003442
Chandler Carruth87c44602011-07-01 23:49:12 +00003443 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003445 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003446 return;
3447 }
3448
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003449 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003450 } else {
3451 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3452 }
3453}
3454
Chandler Carruth1b03c872011-07-02 00:01:44 +00003455static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003456 if (S.LangOpts.CUDA) {
3457 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003458 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003459 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003460
Peter Collingbourneced76712010-12-01 03:15:31 +00003461
Chandler Carruth87c44602011-07-01 23:49:12 +00003462 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003464 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003465 return;
3466 }
3467
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003468 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003469 } else {
3470 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3471 }
3472}
3473
Chandler Carruth1b03c872011-07-02 00:01:44 +00003474static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003475 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003476 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003477 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003478
Chandler Carruth87c44602011-07-01 23:49:12 +00003479 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003480 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003481 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003482 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003483 return;
3484 }
Mike Stumpbf916502009-07-24 19:02:52 +00003485
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003486 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003487 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003488 return;
3489 }
Mike Stumpbf916502009-07-24 19:02:52 +00003490
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003491 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003492}
3493
Chandler Carruth1b03c872011-07-02 00:01:44 +00003494static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003495 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003496
Chandler Carruth87c44602011-07-01 23:49:12 +00003497 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003498 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3499 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003500 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003501 return;
3502
Chandler Carruth87c44602011-07-01 23:49:12 +00003503 if (!isa<ObjCMethodDecl>(D)) {
3504 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3505 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003506 return;
3507 }
3508
Chandler Carruth87c44602011-07-01 23:49:12 +00003509 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003510 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003511 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003512 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003513 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003514 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003515 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003516 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003517 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003518 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003519 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003520 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003521 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003522 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003523 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003524 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003525 case AttributeList::AT_Pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003526 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003527 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003528 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003529 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003530 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003531 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003532 return;
3533 }
3534
Chris Lattner5f9e2722011-07-23 10:55:15 +00003535 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003536 PcsAttr::PCSType PCS;
3537 if (StrRef == "aapcs")
3538 PCS = PcsAttr::AAPCS;
3539 else if (StrRef == "aapcs-vfp")
3540 PCS = PcsAttr::AAPCS_VFP;
3541 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003542 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3543 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003544 return;
3545 }
3546
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003547 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003548 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003549 default:
3550 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003551 }
3552}
3553
Chandler Carruth1b03c872011-07-02 00:01:44 +00003554static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003555 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003556 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003557}
3558
John McCall711c52b2011-01-05 12:14:39 +00003559bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3560 if (attr.isInvalid())
3561 return true;
3562
Ted Kremenek831efae2011-04-15 05:49:29 +00003563 if ((attr.getNumArgs() != 0 &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003564 !(attr.getKind() == AttributeList::AT_Pcs && attr.getNumArgs() == 1)) ||
Ted Kremenek831efae2011-04-15 05:49:29 +00003565 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003566 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3567 attr.setInvalid();
3568 return true;
3569 }
3570
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003571 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3572 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003573 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003574 case AttributeList::AT_CDecl: CC = CC_C; break;
3575 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3576 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3577 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3578 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3579 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003580 Expr *Arg = attr.getArg(0);
3581 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003582 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003583 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3584 << "pcs" << 1;
3585 attr.setInvalid();
3586 return true;
3587 }
3588
Chris Lattner5f9e2722011-07-23 10:55:15 +00003589 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003590 if (StrRef == "aapcs") {
3591 CC = CC_AAPCS;
3592 break;
3593 } else if (StrRef == "aapcs-vfp") {
3594 CC = CC_AAPCS_VFP;
3595 break;
3596 }
3597 // FALLS THROUGH
3598 }
David Blaikie7530c032012-01-17 06:56:22 +00003599 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003600 }
3601
3602 return false;
3603}
3604
Chandler Carruth1b03c872011-07-02 00:01:44 +00003605static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003606 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003607
3608 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003609 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003610 return;
3611
Chandler Carruth87c44602011-07-01 23:49:12 +00003612 if (!isa<ObjCMethodDecl>(D)) {
3613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3614 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003615 return;
3616 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003617
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003618 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003619}
3620
3621/// Checks a regparm attribute, returning true if it is ill-formed and
3622/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003623bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3624 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003625 return true;
3626
Chandler Carruth87c44602011-07-01 23:49:12 +00003627 if (Attr.getNumArgs() != 1) {
3628 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3629 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003630 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003631 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003632
Chandler Carruth87c44602011-07-01 23:49:12 +00003633 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003634 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003635 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003636 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003637 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003638 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003639 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003640 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003641 }
3642
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003643 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003644 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003645 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003646 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003647 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003648 }
3649
John McCall711c52b2011-01-05 12:14:39 +00003650 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003651 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003652 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003653 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003654 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003655 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003656 }
3657
John McCall711c52b2011-01-05 12:14:39 +00003658 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003659}
3660
Chandler Carruth1b03c872011-07-02 00:01:44 +00003661static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003662 if (S.LangOpts.CUDA) {
3663 // check the attribute arguments.
3664 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003665 // FIXME: 0 is not okay.
3666 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003667 return;
3668 }
3669
Chandler Carruth87c44602011-07-01 23:49:12 +00003670 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003671 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003672 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003673 return;
3674 }
3675
3676 Expr *MaxThreadsExpr = Attr.getArg(0);
3677 llvm::APSInt MaxThreads(32);
3678 if (MaxThreadsExpr->isTypeDependent() ||
3679 MaxThreadsExpr->isValueDependent() ||
3680 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3681 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3682 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3683 return;
3684 }
3685
3686 llvm::APSInt MinBlocks(32);
3687 if (Attr.getNumArgs() > 1) {
3688 Expr *MinBlocksExpr = Attr.getArg(1);
3689 if (MinBlocksExpr->isTypeDependent() ||
3690 MinBlocksExpr->isValueDependent() ||
3691 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3692 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3693 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3694 return;
3695 }
3696 }
3697
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003698 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003699 MaxThreads.getZExtValue(),
3700 MinBlocks.getZExtValue()));
3701 } else {
3702 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3703 }
3704}
3705
Chris Lattner0744e5f2008-06-29 00:23:49 +00003706//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003707// Checker-specific attribute handlers.
3708//===----------------------------------------------------------------------===//
3709
John McCallc7ad3812011-01-25 03:31:58 +00003710static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003711 return type->isDependentType() ||
3712 type->isObjCObjectPointerType() ||
3713 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003714}
3715static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003716 return type->isDependentType() ||
3717 type->isPointerType() ||
3718 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003719}
3720
Chandler Carruth1b03c872011-07-02 00:01:44 +00003721static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003722 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003723 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003724 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003725 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003726 return;
3727 }
3728
3729 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003730 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003731 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3732 cf = false;
3733 } else {
3734 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3735 cf = true;
3736 }
3737
3738 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003739 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003740 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003741 return;
3742 }
3743
3744 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003745 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003746 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003747 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003748}
3749
Chandler Carruth1b03c872011-07-02 00:01:44 +00003750static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3751 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003752 if (!isa<ObjCMethodDecl>(D)) {
3753 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003754 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003755 return;
3756 }
3757
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003758 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003759}
3760
Chandler Carruth1b03c872011-07-02 00:01:44 +00003761static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3762 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003763
John McCallc7ad3812011-01-25 03:31:58 +00003764 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003765
Chandler Carruth87c44602011-07-01 23:49:12 +00003766 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003767 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003768 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003769 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003770 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003771 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003772 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003773 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003774 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003775 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003776 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003777 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003778 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003779 return;
3780 }
Mike Stumpbf916502009-07-24 19:02:52 +00003781
John McCallc7ad3812011-01-25 03:31:58 +00003782 bool typeOK;
3783 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003784 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003785 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003786 case AttributeList::AT_NSReturnsAutoreleased:
3787 case AttributeList::AT_NSReturnsRetained:
3788 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003789 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3790 cf = false;
3791 break;
3792
Sean Hunt8e083e72012-06-19 23:57:03 +00003793 case AttributeList::AT_CFReturnsRetained:
3794 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003795 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3796 cf = true;
3797 break;
3798 }
3799
3800 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003801 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003802 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003803 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003804 }
Mike Stumpbf916502009-07-24 19:02:52 +00003805
Chandler Carruth87c44602011-07-01 23:49:12 +00003806 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003807 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003808 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003809 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003810 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003811 S.Context));
3812 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003813 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003814 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003815 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003816 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003817 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003818 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003819 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003820 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003821 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003822 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003823 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003824 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003825 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003826 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003827 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003828 return;
3829 };
3830}
3831
John McCalldc7c5ad2011-07-22 08:53:00 +00003832static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3833 const AttributeList &attr) {
3834 SourceLocation loc = attr.getLoc();
3835
3836 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3837
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003838 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003839 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003840 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003841 return;
3842 }
3843
3844 // Check that the method returns a normal pointer.
3845 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003846
3847 if (!resultType->isReferenceType() &&
3848 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003849 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3850 << SourceRange(loc)
3851 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3852
3853 // Drop the attribute.
3854 return;
3855 }
3856
3857 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003858 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003859}
3860
John McCall8dfac0b2011-09-30 05:12:12 +00003861/// Handle cf_audited_transfer and cf_unknown_transfer.
3862static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3863 if (!isa<FunctionDecl>(D)) {
3864 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003865 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003866 return;
3867 }
3868
Sean Hunt8e083e72012-06-19 23:57:03 +00003869 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00003870
3871 // Check whether there's a conflicting attribute already present.
3872 Attr *Existing;
3873 if (IsAudited) {
3874 Existing = D->getAttr<CFUnknownTransferAttr>();
3875 } else {
3876 Existing = D->getAttr<CFAuditedTransferAttr>();
3877 }
3878 if (Existing) {
3879 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3880 << A.getName()
3881 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3882 << A.getRange() << Existing->getRange();
3883 return;
3884 }
3885
3886 // All clear; add the attribute.
3887 if (IsAudited) {
3888 D->addAttr(
3889 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3890 } else {
3891 D->addAttr(
3892 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3893 }
3894}
3895
John McCallfe98da02011-09-29 07:17:38 +00003896static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3897 const AttributeList &Attr) {
3898 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3899 if (!RD || RD->isUnion()) {
3900 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003901 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003902 }
3903
3904 IdentifierInfo *ParmName = Attr.getParameterName();
3905
3906 // In Objective-C, verify that the type names an Objective-C type.
3907 // We don't want to check this outside of ObjC because people sometimes
3908 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003909 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003910 // Check for an existing type with this name.
3911 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3912 Sema::LookupOrdinaryName);
3913 if (S.LookupName(R, Sc)) {
3914 NamedDecl *Target = R.getFoundDecl();
3915 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3916 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3917 S.Diag(Target->getLocStart(), diag::note_declared_at);
3918 }
3919 }
3920 }
3921
3922 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3923 ParmName));
3924}
3925
Chandler Carruth1b03c872011-07-02 00:01:44 +00003926static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3927 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003928 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003929
Chandler Carruth87c44602011-07-01 23:49:12 +00003930 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003931 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003932}
3933
Chandler Carruth1b03c872011-07-02 00:01:44 +00003934static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3935 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003936 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003937 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003938 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003939 return;
3940 }
3941
Chandler Carruth87c44602011-07-01 23:49:12 +00003942 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003943 QualType type = vd->getType();
3944
3945 if (!type->isDependentType() &&
3946 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003947 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003948 << type;
3949 return;
3950 }
3951
3952 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3953
3954 // If we have no lifetime yet, check the lifetime we're presumably
3955 // going to infer.
3956 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3957 lifetime = type->getObjCARCImplicitLifetime();
3958
3959 switch (lifetime) {
3960 case Qualifiers::OCL_None:
3961 assert(type->isDependentType() &&
3962 "didn't infer lifetime for non-dependent type?");
3963 break;
3964
3965 case Qualifiers::OCL_Weak: // meaningful
3966 case Qualifiers::OCL_Strong: // meaningful
3967 break;
3968
3969 case Qualifiers::OCL_ExplicitNone:
3970 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003971 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003972 << (lifetime == Qualifiers::OCL_Autoreleasing);
3973 break;
3974 }
3975
Chandler Carruth87c44602011-07-01 23:49:12 +00003976 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003977 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003978}
3979
Francois Pichet11542142010-12-19 06:50:37 +00003980//===----------------------------------------------------------------------===//
3981// Microsoft specific attribute handlers.
3982//===----------------------------------------------------------------------===//
3983
Chandler Carruth1b03c872011-07-02 00:01:44 +00003984static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003985 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003986 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003987 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003988 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003989
Francois Pichet11542142010-12-19 06:50:37 +00003990 Expr *Arg = Attr.getArg(0);
3991 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003992 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3994 << "uuid" << 1;
3995 return;
3996 }
3997
Chris Lattner5f9e2722011-07-23 10:55:15 +00003998 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003999
4000 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4001 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004002
Francois Pichetd3d3be92010-12-20 01:41:49 +00004003 // Validate GUID length.
4004 if (IsCurly && StrRef.size() != 38) {
4005 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4006 return;
4007 }
4008 if (!IsCurly && StrRef.size() != 36) {
4009 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4010 return;
4011 }
4012
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004013 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004014 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004015 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004016 if (IsCurly) // Skip the optional '{'
4017 ++I;
4018
4019 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004020 if (i == 8 || i == 13 || i == 18 || i == 23) {
4021 if (*I != '-') {
4022 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4023 return;
4024 }
4025 } else if (!isxdigit(*I)) {
4026 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4027 return;
4028 }
4029 I++;
4030 }
Francois Pichet11542142010-12-19 06:50:37 +00004031
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004032 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004033 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004034 } else
Francois Pichet11542142010-12-19 06:50:37 +00004035 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004036}
4037
John McCallc052dbb2012-05-22 21:28:12 +00004038static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4039 if (S.LangOpts.MicrosoftExt) {
4040 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004041 if (Kind == AttributeList::AT_SingleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004042 D->addAttr(
4043 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004044 else if (Kind == AttributeList::AT_MultipleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004045 D->addAttr(
4046 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004047 else if (Kind == AttributeList::AT_VirtualInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004048 D->addAttr(
4049 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4050 } else
4051 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4052}
4053
4054static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4055 if (S.LangOpts.MicrosoftExt) {
4056 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004057 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004058 D->addAttr(
4059 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004060 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004061 D->addAttr(
4062 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004063 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004064 D->addAttr(
4065 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4066 } else
4067 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4068}
4069
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004070static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4071 if (S.LangOpts.MicrosoftExt)
4072 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4073 else
4074 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4075}
4076
Ted Kremenekb71368d2009-05-09 02:44:38 +00004077//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004078// Top Level Sema Entry Points
4079//===----------------------------------------------------------------------===//
4080
Chandler Carruth1b03c872011-07-02 00:01:44 +00004081static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4082 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004083 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004084 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4085 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4086 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004087 default:
4088 break;
4089 }
4090}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004091
Chandler Carruth1b03c872011-07-02 00:01:44 +00004092static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4093 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004094 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004095 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4096 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4097 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004098 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004099 case AttributeList::AT_AddressSpace:
4100 case AttributeList::AT_OpenCLImageAccess:
4101 case AttributeList::AT_ObjCGC:
4102 case AttributeList::AT_VectorSize:
4103 case AttributeList::AT_NeonVectorType:
4104 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004105 // Ignore these, these are type attributes, handled by
4106 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004107 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004108 case AttributeList::AT_CUDADevice:
4109 case AttributeList::AT_CUDAHost:
4110 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004111 // Ignore, this is a non-inheritable attribute, handled
4112 // by ProcessNonInheritableDeclAttr.
4113 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004114 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4115 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4116 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4117 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004118 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004119 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004120 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004121 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004122 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4123 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4124 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004125 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004126 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4127 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4128 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4129 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004130 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4131 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004132 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4133 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004134 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004135 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004136 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4137 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4138 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4139 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4140 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004141 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004142 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004143 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4144 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4145 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4146 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4147 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004148 case AttributeList::AT_ownership_returns:
4149 case AttributeList::AT_ownership_takes:
4150 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004151 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004152 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4153 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4154 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4155 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4156 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4157 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4158 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004159
Sean Hunt8e083e72012-06-19 23:57:03 +00004160 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004161 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004162 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004163 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004164
Sean Hunt8e083e72012-06-19 23:57:03 +00004165 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004166 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4167
Sean Hunt8e083e72012-06-19 23:57:03 +00004168 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004169 handleNSBridgedAttr(S, scope, D, Attr); break;
4170
Sean Hunt8e083e72012-06-19 23:57:03 +00004171 case AttributeList::AT_CFAuditedTransfer:
4172 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004173 handleCFTransferAttr(S, D, Attr); break;
4174
Ted Kremenekb71368d2009-05-09 02:44:38 +00004175 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004176 case AttributeList::AT_CFConsumed:
4177 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4178 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004179 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004180
Sean Hunt8e083e72012-06-19 23:57:03 +00004181 case AttributeList::AT_NSReturnsAutoreleased:
4182 case AttributeList::AT_NSReturnsNotRetained:
4183 case AttributeList::AT_CFReturnsNotRetained:
4184 case AttributeList::AT_NSReturnsRetained:
4185 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004186 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004187
Tanya Lattner0df579e2012-07-09 22:06:01 +00004188 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004189 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004190 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004191
Sean Hunt8e083e72012-06-19 23:57:03 +00004192 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004193 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004194
Sean Hunt8e083e72012-06-19 23:57:03 +00004195 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4196 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4197 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004198 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4199 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004200 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004201 handleArcWeakrefUnavailableAttr (S, D, Attr);
4202 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004203 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004204 handleObjCRootClassAttr(S, D, Attr);
4205 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004206 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004207 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004208 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004209 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4210 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004211 handleReturnsTwiceAttr(S, D, Attr);
4212 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004213 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4214 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4215 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004216 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004217 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4218 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4219 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4220 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004221 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004222 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004223 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004224 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004225 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004226 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004227 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004228 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004229 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4230 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4231 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4232 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4233 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4234 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4235 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4236 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4237 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004238 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004239 // Just ignore
4240 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004241 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004242 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004243 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004244 case AttributeList::AT_StdCall:
4245 case AttributeList::AT_CDecl:
4246 case AttributeList::AT_FastCall:
4247 case AttributeList::AT_ThisCall:
4248 case AttributeList::AT_Pascal:
4249 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004250 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004251 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004252 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004253 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004254 break;
John McCallc052dbb2012-05-22 21:28:12 +00004255
4256 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004257 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004258 handleMsStructAttr(S, D, Attr);
4259 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004260 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004261 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004262 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004263 case AttributeList::AT_SingleInheritance:
4264 case AttributeList::AT_MultipleInheritance:
4265 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004266 handleInheritanceAttr(S, D, Attr);
4267 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004268 case AttributeList::AT_Win64:
4269 case AttributeList::AT_Ptr32:
4270 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004271 handlePortabilityAttr(S, D, Attr);
4272 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004273 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004274 handleForceInlineAttr(S, D, Attr);
4275 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004276
4277 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004278 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004279 handleGuardedVarAttr(S, D, Attr);
4280 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004281 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004282 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004283 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004284 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004285 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004286 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004287 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004288 handleNoAddressSafetyAttr(S, D, Attr);
4289 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004290 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004291 handleNoThreadSafetyAttr(S, D, Attr);
4292 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004293 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004294 handleLockableAttr(S, D, Attr);
4295 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004296 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004297 handleGuardedByAttr(S, D, Attr);
4298 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004299 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004300 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004301 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004302 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004303 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004304 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004305 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004306 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004307 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004308 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004309 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004310 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004311 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004312 handleLockReturnedAttr(S, D, Attr);
4313 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004314 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004315 handleLocksExcludedAttr(S, D, Attr);
4316 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004317 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004318 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004319 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004320 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004321 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004322 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004323 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004324 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004325 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004326 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004327 handleUnlockFunAttr(S, D, Attr);
4328 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004329 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004330 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004331 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004332 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004333 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004334 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004335
Chris Lattner803d0802008-06-29 00:43:07 +00004336 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004337 // Ask target about the attribute.
4338 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4339 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004340 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4341 diag::warn_unhandled_ms_attribute_ignored :
4342 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004343 break;
4344 }
4345}
4346
Peter Collingbourne60700392011-01-21 02:08:45 +00004347/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4348/// the attribute applies to decls. If the attribute is a type attribute, just
4349/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4350/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004351static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4352 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004353 bool NonInheritable, bool Inheritable) {
4354 if (Attr.isInvalid())
4355 return;
4356
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004357 // Type attributes are still treated as declaration attributes by
4358 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4359 // want to process them, however, because we will simply warn about ignoring
4360 // them. So instead, we will bail out early.
4361 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004362 return;
4363
4364 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004365 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004366
4367 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004368 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004369}
4370
Chris Lattner803d0802008-06-29 00:43:07 +00004371/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4372/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004373void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004374 const AttributeList *AttrList,
4375 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004376 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004377 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004378 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004379
4380 // GCC accepts
4381 // static int a9 __attribute__((weakref));
4382 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004383 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004384 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004385 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004386 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004387 }
4388}
4389
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004390// Annotation attributes are the only attributes allowed after an access
4391// specifier.
4392bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4393 const AttributeList *AttrList) {
4394 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004395 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004396 handleAnnotateAttr(*this, ASDecl, *l);
4397 } else {
4398 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4399 return true;
4400 }
4401 }
4402
4403 return false;
4404}
4405
John McCalle82247a2011-10-01 05:17:03 +00004406/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4407/// contains any decl attributes that we should warn about.
4408static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4409 for ( ; A; A = A->getNext()) {
4410 // Only warn if the attribute is an unignored, non-type attribute.
4411 if (A->isUsedAsTypeAttr()) continue;
4412 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4413
4414 if (A->getKind() == AttributeList::UnknownAttribute) {
4415 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4416 << A->getName() << A->getRange();
4417 } else {
4418 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4419 << A->getName() << A->getRange();
4420 }
4421 }
4422}
4423
4424/// checkUnusedDeclAttributes - Given a declarator which is not being
4425/// used to build a declaration, complain about any decl attributes
4426/// which might be lying around on it.
4427void Sema::checkUnusedDeclAttributes(Declarator &D) {
4428 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4429 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4430 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4431 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4432}
4433
Ryan Flynne25ff832009-07-30 03:15:39 +00004434/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004435/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004436NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4437 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004438 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004439 NamedDecl *NewD = 0;
4440 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004441 FunctionDecl *NewFD;
4442 // FIXME: Missing call to CheckFunctionDeclaration().
4443 // FIXME: Mangling?
4444 // FIXME: Is the qualifier info correct?
4445 // FIXME: Is the DeclContext correct?
4446 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4447 Loc, Loc, DeclarationName(II),
4448 FD->getType(), FD->getTypeSourceInfo(),
4449 SC_None, SC_None,
4450 false/*isInlineSpecified*/,
4451 FD->hasPrototype(),
4452 false/*isConstexprSpecified*/);
4453 NewD = NewFD;
4454
4455 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004456 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004457
4458 // Fake up parameter variables; they are declared as if this were
4459 // a typedef.
4460 QualType FDTy = FD->getType();
4461 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4462 SmallVector<ParmVarDecl*, 16> Params;
4463 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4464 AE = FT->arg_type_end(); AI != AE; ++AI) {
4465 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4466 Param->setScopeInfo(0, Params.size());
4467 Params.push_back(Param);
4468 }
David Blaikie4278c652011-09-21 18:16:56 +00004469 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004470 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004471 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4472 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004473 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004474 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004475 VD->getStorageClass(),
4476 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004477 if (VD->getQualifier()) {
4478 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004479 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004480 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004481 }
4482 return NewD;
4483}
4484
James Dennett1dfbd922012-06-14 21:40:34 +00004485/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004486/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004487void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004488 if (W.getUsed()) return; // only do this once
4489 W.setUsed(true);
4490 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4491 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004492 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004493 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4494 NDId->getName()));
4495 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004496 WeakTopLevelDecl.push_back(NewD);
4497 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4498 // to insert Decl at TU scope, sorry.
4499 DeclContext *SavedContext = CurContext;
4500 CurContext = Context.getTranslationUnitDecl();
4501 PushOnScopeChains(NewD, S);
4502 CurContext = SavedContext;
4503 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004504 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004505 }
4506}
4507
Chris Lattner0744e5f2008-06-29 00:23:49 +00004508/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4509/// it, apply them to D. This is a bit tricky because PD can have attributes
4510/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004511void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4512 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004513 // It's valid to "forward-declare" #pragma weak, in which case we
4514 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004515 if (Inheritable) {
4516 LoadExternalWeakUndeclaredIdentifiers();
4517 if (!WeakUndeclaredIdentifiers.empty()) {
4518 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4519 if (IdentifierInfo *Id = ND->getIdentifier()) {
4520 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4521 = WeakUndeclaredIdentifiers.find(Id);
4522 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4523 WeakInfo W = I->second;
4524 DeclApplyPragmaWeak(S, ND, W);
4525 WeakUndeclaredIdentifiers[Id] = W;
4526 }
John McCalld4aff0e2010-10-27 00:59:00 +00004527 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004528 }
4529 }
4530 }
4531
Chris Lattner0744e5f2008-06-29 00:23:49 +00004532 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004533 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004534 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004535
Chris Lattner0744e5f2008-06-29 00:23:49 +00004536 // Walk the declarator structure, applying decl attributes that were in a type
4537 // position to the decl itself. This handles cases like:
4538 // int *__attr__(x)** D;
4539 // when X is a decl attribute.
4540 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4541 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004542 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004543
Chris Lattner0744e5f2008-06-29 00:23:49 +00004544 // Finally, apply any attributes on the decl itself.
4545 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004546 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004547}
John McCall54abf7d2009-11-04 02:18:39 +00004548
John McCallf85e1932011-06-15 23:02:42 +00004549/// Is the given declaration allowed to use a forbidden type?
4550static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4551 // Private ivars are always okay. Unfortunately, people don't
4552 // always properly make their ivars private, even in system headers.
4553 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004554 // Function declarations in sys headers will be marked unavailable.
4555 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4556 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004557 return false;
4558
4559 // Require it to be declared in a system header.
4560 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4561}
4562
4563/// Handle a delayed forbidden-type diagnostic.
4564static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4565 Decl *decl) {
4566 if (decl && isForbiddenTypeAllowed(S, decl)) {
4567 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4568 "this system declaration uses an unsupported type"));
4569 return;
4570 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004571 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004572 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004573 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004574 // kind of forbidden type messages on unavailable functions.
4575 if (FD->hasAttr<UnavailableAttr>() &&
4576 diag.getForbiddenTypeDiagnostic() ==
4577 diag::err_arc_array_param_no_ownership) {
4578 diag.Triggered = true;
4579 return;
4580 }
4581 }
John McCallf85e1932011-06-15 23:02:42 +00004582
4583 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4584 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4585 diag.Triggered = true;
4586}
4587
John McCall92576642012-05-07 06:16:41 +00004588void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4589 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004590 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004591 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004592
John McCall92576642012-05-07 06:16:41 +00004593 // When delaying diagnostics to run in the context of a parsed
4594 // declaration, we only want to actually emit anything if parsing
4595 // succeeds.
4596 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004597
John McCall92576642012-05-07 06:16:41 +00004598 // We emit all the active diagnostics in this pool or any of its
4599 // parents. In general, we'll get one pool for the decl spec
4600 // and a child pool for each declarator; in a decl group like:
4601 // deprecated_typedef foo, *bar, baz();
4602 // only the declarator pops will be passed decls. This is correct;
4603 // we really do need to consider delayed diagnostics from the decl spec
4604 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004605 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004606 do {
John McCall13489672012-05-07 06:16:58 +00004607 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004608 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4609 // This const_cast is a bit lame. Really, Triggered should be mutable.
4610 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004611 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004612 continue;
4613
John McCalleee1d542011-02-14 07:13:47 +00004614 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004615 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004616 // Don't bother giving deprecation diagnostics if the decl is invalid.
4617 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004618 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004619 break;
4620
4621 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004622 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004623 break;
John McCallf85e1932011-06-15 23:02:42 +00004624
4625 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004626 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004627 break;
John McCall2f514482010-01-27 03:50:35 +00004628 }
4629 }
John McCall92576642012-05-07 06:16:41 +00004630 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004631}
4632
John McCall13489672012-05-07 06:16:58 +00004633/// Given a set of delayed diagnostics, re-emit them as if they had
4634/// been delayed in the current context instead of in the given pool.
4635/// Essentially, this just moves them to the current pool.
4636void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4637 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4638 assert(curPool && "re-emitting in undelayed context not supported");
4639 curPool->steal(pool);
4640}
4641
John McCall54abf7d2009-11-04 02:18:39 +00004642static bool isDeclDeprecated(Decl *D) {
4643 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004644 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004645 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004646 // A category implicitly has the availability of the interface.
4647 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4648 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004649 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4650 return false;
4651}
4652
John McCall9c3087b2010-08-26 02:13:20 +00004653void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004654 Decl *Ctx) {
4655 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004656 return;
4657
John McCall2f514482010-01-27 03:50:35 +00004658 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004659 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004660 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004661 << DD.getDeprecationDecl()->getDeclName()
4662 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004663 else if (DD.getUnknownObjCClass()) {
4664 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4665 << DD.getDeprecationDecl()->getDeclName();
4666 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4667 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004668 else
4669 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004670 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004671}
4672
Chris Lattner5f9e2722011-07-23 10:55:15 +00004673void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004674 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004675 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004676 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004677 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004678 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4679 UnknownObjCClass,
4680 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004681 return;
4682 }
4683
4684 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004685 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004686 return;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004687 if (!Message.empty()) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004688 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4689 << Message;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004690 Diag(D->getLocation(),
4691 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4692 : diag::note_previous_decl) << D->getDeclName();
4693 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004694 else {
Fariborz Jahanian350e9562012-05-27 16:59:48 +00004695 if (!UnknownObjCClass) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004696 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian350e9562012-05-27 16:59:48 +00004697 Diag(D->getLocation(),
4698 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4699 : diag::note_previous_decl) << D->getDeclName();
4700 }
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004701 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004702 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004703 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4704 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004705 }
John McCall54abf7d2009-11-04 02:18:39 +00004706}