blob: 744d18bfd9fbda8fdc569e26b1852d3307b321b0 [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) {
87 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
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000433static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
434 bool pointer = false) {
435 assert(!Attr.isInvalid());
436
437 if (!checkAttributeNumArgs(S, Attr, 0))
438 return;
439
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;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000444 return;
445 }
446
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000447 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000448 return;
449
450 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000451 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000452 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000453 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000454}
455
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000456static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000458 assert(!Attr.isInvalid());
459
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000461 return;
462
463 // D must be either a member field or global (potentially shared) variable.
464 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000465 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
466 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000467 return;
468 }
469
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000470 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000471 return;
472
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000473 SmallVector<Expr*, 1> Args;
474 // check that all arguments are lockable objects
475 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
476 unsigned Size = Args.size();
477 if (Size != 1)
478 return;
479 Expr *Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000480
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000481 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000482 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000483 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000484 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000485 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000486}
487
488
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000489static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
490 bool scoped = false) {
491 assert(!Attr.isInvalid());
492
493 if (!checkAttributeNumArgs(S, Attr, 0))
494 return;
495
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000496 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000497 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000498 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
499 << Attr.getName() << ThreadExpectedClassOrStruct;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500 return;
501 }
502
503 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000504 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000505 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000506 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000507}
508
509static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
510 const AttributeList &Attr) {
511 assert(!Attr.isInvalid());
512
513 if (!checkAttributeNumArgs(S, Attr, 0))
514 return;
515
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000516 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000517 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
518 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000519 return;
520 }
521
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000522 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000523 S.Context));
524}
525
Kostya Serebryany71efba02012-01-24 19:25:38 +0000526static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000527 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000528 assert(!Attr.isInvalid());
529
530 if (!checkAttributeNumArgs(S, Attr, 0))
531 return;
532
533 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
534 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
535 << Attr.getName() << ExpectedFunctionOrMethod;
536 return;
537 }
538
539 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
540 S.Context));
541}
542
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000543static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
544 bool before) {
545 assert(!Attr.isInvalid());
546
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000547 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000548 return;
549
550 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000551 ValueDecl *VD = dyn_cast<ValueDecl>(D);
552 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000553 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
554 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000555 return;
556 }
557
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000558 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000559 QualType QT = VD->getType();
560 if (!QT->isDependentType()) {
561 const RecordType *RT = getRecordType(QT);
562 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000563 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000564 << Attr.getName();
565 return;
566 }
567 }
568
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000569 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000570 // Check that all arguments are lockable objects.
571 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000572 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000573 if (Size == 0)
574 return;
575 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000576
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000577 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000578 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000579 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000580 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000581 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000582 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000583}
584
585static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000586 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000587 assert(!Attr.isInvalid());
588
589 // zero or more arguments ok
590
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000591 // check that the attribute is applied to a function
592 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000593 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
594 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000595 return;
596 }
597
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000598 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000599 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000600 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000601 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000602 Expr **StartArg = Size == 0 ? 0 : &Args[0];
603
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000604 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000605 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000606 S.Context, StartArg,
607 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000608 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000609 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000610 S.Context, StartArg,
611 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000612}
613
614static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000615 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000616 assert(!Attr.isInvalid());
617
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000618 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000619 return;
620
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000621 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000622 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
623 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000624 return;
625 }
626
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000627 if (!isIntOrBool(Attr.getArg(0))) {
628 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
629 << Attr.getName();
630 return;
631 }
632
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000633 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000634 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000635 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000636 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000637 Expr **StartArg = Size == 0 ? 0 : &Args[0];
638
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000639 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000640 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000641 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000642 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000643 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000644 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000645 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000646 S.Context,
647 Attr.getArg(0),
648 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000649}
650
651static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000652 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000653 assert(!Attr.isInvalid());
654
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000655 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000656 return;
657
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000658 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000659 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
660 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000661 return;
662 }
663
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000664 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000665 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000666 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000667 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000668 if (Size == 0)
669 return;
670 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000671
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000672 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000673 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000674 S.Context, StartArg,
675 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000676 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000677 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000678 S.Context, StartArg,
679 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000680}
681
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000682static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000683 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000684 assert(!Attr.isInvalid());
685
686 // zero or more arguments ok
687
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000688 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000689 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
690 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000691 return;
692 }
693
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000694 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000695 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000696 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000697 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000698 Expr **StartArg = Size == 0 ? 0 : &Args[0];
699
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000700 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000701 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000702}
703
704static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000706 assert(!Attr.isInvalid());
707
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000708 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000709 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000710 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000711
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000712 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000713 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
714 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000715 return;
716 }
717
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000718 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000719 return;
720
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000721 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000722 SmallVector<Expr*, 1> Args;
723 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
724 unsigned Size = Args.size();
725 if (Size == 0)
726 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000727
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000728 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
729 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000730}
731
732static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000733 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000734 assert(!Attr.isInvalid());
735
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000736 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000737 return;
738
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000739 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000740 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
741 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000742 return;
743 }
744
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000745 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000746 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000747 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000748 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000749 if (Size == 0)
750 return;
751 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000752
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000753 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000754 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000755}
756
757
Chandler Carruth1b03c872011-07-02 00:01:44 +0000758static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
759 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000760 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000761 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000762 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000763 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000764 }
Mike Stumpbf916502009-07-24 19:02:52 +0000765
Chris Lattner6b6b5372008-06-26 18:38:35 +0000766 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000767
768 Expr *sizeExpr;
769
770 // Special case where the argument is a template id.
771 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000772 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000773 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000774 UnqualifiedId id;
775 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000776
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000777 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
778 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000779 if (Size.isInvalid())
780 return;
781
782 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000783 } else {
784 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000785 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000786 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000787
Peter Collingbourne7a730022010-11-23 20:45:58 +0000788 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000789 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000790
791 // Instantiate/Install the vector type, and let Sema build the type for us.
792 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000793 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000794 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000795 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000796 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000797
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000798 // Remember this typedef decl, we will need it later for diagnostics.
799 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000800 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000801}
802
Chandler Carruth1b03c872011-07-02 00:01:44 +0000803static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000804 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000805 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000806 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000807
Chandler Carruth87c44602011-07-01 23:49:12 +0000808 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000809 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000810 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000811 // If the alignment is less than or equal to 8 bits, the packed attribute
812 // has no effect.
813 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000814 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000815 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000816 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000817 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000818 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000819 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000820 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000821}
822
Chandler Carruth1b03c872011-07-02 00:01:44 +0000823static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000824 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000825 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000826 else
827 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
828}
829
Chandler Carruth1b03c872011-07-02 00:01:44 +0000830static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000831 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000832 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000833 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000834
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000835 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000836 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000837 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000838 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000839 return;
840 }
841
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000842 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000843}
844
Ted Kremenek2f041d02011-09-29 07:02:25 +0000845static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
846 // The IBOutlet/IBOutletCollection attributes only apply to instance
847 // variables or properties of Objective-C classes. The outlet must also
848 // have an object reference type.
849 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
850 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000851 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000852 << Attr.getName() << VD->getType() << 0;
853 return false;
854 }
855 }
856 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
857 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000858 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000859 << Attr.getName() << PD->getType() << 1;
860 return false;
861 }
862 }
863 else {
864 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
865 return false;
866 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000867
Ted Kremenek2f041d02011-09-29 07:02:25 +0000868 return true;
869}
870
Chandler Carruth1b03c872011-07-02 00:01:44 +0000871static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000872 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000873 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000874 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000875
876 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000877 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000878
Ted Kremenek2f041d02011-09-29 07:02:25 +0000879 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000880}
881
Chandler Carruth1b03c872011-07-02 00:01:44 +0000882static void handleIBOutletCollection(Sema &S, Decl *D,
883 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000884
885 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000886 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
888 return;
889 }
890
Ted Kremenek2f041d02011-09-29 07:02:25 +0000891 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000892 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000893
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000894 IdentifierInfo *II = Attr.getParameterName();
895 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000896 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000897
John McCallb3d87482010-08-24 05:47:05 +0000898 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000899 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000900 if (!TypeRep) {
901 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
902 return;
903 }
John McCallb3d87482010-08-24 05:47:05 +0000904 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000905 // Diagnose use of non-object type in iboutletcollection attribute.
906 // FIXME. Gnu attribute extension ignores use of builtin types in
907 // attributes. So, __attribute__((iboutletcollection(char))) will be
908 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000909 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000910 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
911 return;
912 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000913 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
914 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000915}
916
Chandler Carruthd309c812011-07-01 23:49:16 +0000917static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000918 if (const RecordType *UT = T->getAsUnionType())
919 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
920 RecordDecl *UD = UT->getDecl();
921 for (RecordDecl::field_iterator it = UD->field_begin(),
922 itend = UD->field_end(); it != itend; ++it) {
923 QualType QT = it->getType();
924 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
925 T = QT;
926 return;
927 }
928 }
929 }
930}
931
Nuno Lopes587de5b2012-05-24 00:22:00 +0000932static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +0000933 if (!isFunctionOrMethod(D)) {
934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
935 << "alloc_size" << ExpectedFunctionOrMethod;
936 return;
937 }
938
Nuno Lopes587de5b2012-05-24 00:22:00 +0000939 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
940 return;
941
942 // In C++ the implicit 'this' function parameter also counts, and they are
943 // counted from one.
944 bool HasImplicitThisParam = isInstanceMethod(D);
945 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
946
947 SmallVector<unsigned, 8> SizeArgs;
948
949 for (AttributeList::arg_iterator I = Attr.arg_begin(),
950 E = Attr.arg_end(); I!=E; ++I) {
951 // The argument must be an integer constant expression.
952 Expr *Ex = *I;
953 llvm::APSInt ArgNum;
954 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
955 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
956 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
957 << "alloc_size" << Ex->getSourceRange();
958 return;
959 }
960
961 uint64_t x = ArgNum.getZExtValue();
962
963 if (x < 1 || x > NumArgs) {
964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
965 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
966 return;
967 }
968
969 --x;
970 if (HasImplicitThisParam) {
971 if (x == 0) {
972 S.Diag(Attr.getLoc(),
973 diag::err_attribute_invalid_implicit_this_argument)
974 << "alloc_size" << Ex->getSourceRange();
975 return;
976 }
977 --x;
978 }
979
980 // check if the function argument is of an integer type
981 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
982 if (!T->isIntegerType()) {
983 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
984 << "alloc_size" << Ex->getSourceRange();
985 return;
986 }
987
Nuno Lopes587de5b2012-05-24 00:22:00 +0000988 SizeArgs.push_back(x);
989 }
990
991 // check if the function returns a pointer
992 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
993 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
994 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
995 }
996
Nuno Lopes96c67d12012-06-18 16:27:56 +0000997 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
998 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +0000999}
1000
Chandler Carruth1b03c872011-07-02 00:01:44 +00001001static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001002 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1003 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001004 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001006 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001007 return;
1008 }
Mike Stumpbf916502009-07-24 19:02:52 +00001009
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001010 // In C++ the implicit 'this' function parameter also counts, and they are
1011 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001012 bool HasImplicitThisParam = isInstanceMethod(D);
1013 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001014
1015 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001016 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001017
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001018 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1019 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001020
1021
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001022 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001023 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001024 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001025 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1026 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001027 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1028 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001029 return;
1030 }
Mike Stumpbf916502009-07-24 19:02:52 +00001031
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001032 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001033
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001034 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001036 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001037 return;
1038 }
Mike Stumpbf916502009-07-24 19:02:52 +00001039
Ted Kremenek465172f2008-07-21 22:09:15 +00001040 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001041 if (HasImplicitThisParam) {
1042 if (x == 0) {
1043 S.Diag(Attr.getLoc(),
1044 diag::err_attribute_invalid_implicit_this_argument)
1045 << "nonnull" << Ex->getSourceRange();
1046 return;
1047 }
1048 --x;
1049 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001050
1051 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001052 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001053 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001054
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001055 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001056 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001057 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001058 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001059 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001060 }
Mike Stumpbf916502009-07-24 19:02:52 +00001061
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001062 NonNullArgs.push_back(x);
1063 }
Mike Stumpbf916502009-07-24 19:02:52 +00001064
1065 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1066 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001067 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001068 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1069 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001070 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001071 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001072 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001073 }
Mike Stumpbf916502009-07-24 19:02:52 +00001074
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001075 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001076 if (NonNullArgs.empty()) {
1077 // Warn the trivial case only if attribute is not coming from a
1078 // macro instantiation.
1079 if (Attr.getLoc().isFileID())
1080 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001081 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001082 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001083 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001084
1085 unsigned* start = &NonNullArgs[0];
1086 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001087 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001088 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001089 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001090}
1091
Chandler Carruth1b03c872011-07-02 00:01:44 +00001092static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001093 // This attribute must be applied to a function declaration.
1094 // The first argument to the attribute must be a string,
1095 // the name of the resource, for example "malloc".
1096 // The following arguments must be argument indexes, the arguments must be
1097 // of integer type for Returns, otherwise of pointer type.
1098 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001099 // after being held. free() should be __attribute((ownership_takes)), whereas
1100 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001101
1102 if (!AL.getParameterName()) {
1103 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1104 << AL.getName()->getName() << 1;
1105 return;
1106 }
1107 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001108 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001109 switch (AL.getKind()) {
1110 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001111 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001112 if (AL.getNumArgs() < 1) {
1113 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1114 return;
1115 }
Jordy Rose2a479922010-08-12 08:54:03 +00001116 break;
1117 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001118 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001119 if (AL.getNumArgs() < 1) {
1120 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1121 return;
1122 }
Jordy Rose2a479922010-08-12 08:54:03 +00001123 break;
1124 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001125 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001126 if (AL.getNumArgs() > 1) {
1127 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1128 << AL.getNumArgs() + 1;
1129 return;
1130 }
Jordy Rose2a479922010-08-12 08:54:03 +00001131 break;
1132 default:
1133 // This should never happen given how we are called.
1134 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001135 }
1136
Chandler Carruth87c44602011-07-01 23:49:12 +00001137 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001138 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1139 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001140 return;
1141 }
1142
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001143 // In C++ the implicit 'this' function parameter also counts, and they are
1144 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001145 bool HasImplicitThisParam = isInstanceMethod(D);
1146 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001147
Chris Lattner5f9e2722011-07-23 10:55:15 +00001148 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001149
1150 // Normalize the argument, __foo__ becomes foo.
1151 if (Module.startswith("__") && Module.endswith("__"))
1152 Module = Module.substr(2, Module.size() - 4);
1153
Chris Lattner5f9e2722011-07-23 10:55:15 +00001154 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001155
Jordy Rose2a479922010-08-12 08:54:03 +00001156 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1157 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001158
Peter Collingbourne7a730022010-11-23 20:45:58 +00001159 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001160 llvm::APSInt ArgNum(32);
1161 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1162 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1163 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1164 << AL.getName()->getName() << IdxExpr->getSourceRange();
1165 continue;
1166 }
1167
1168 unsigned x = (unsigned) ArgNum.getZExtValue();
1169
1170 if (x > NumArgs || x < 1) {
1171 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1172 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1173 continue;
1174 }
1175 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001176 if (HasImplicitThisParam) {
1177 if (x == 0) {
1178 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1179 << "ownership" << IdxExpr->getSourceRange();
1180 return;
1181 }
1182 --x;
1183 }
1184
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001185 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001186 case OwnershipAttr::Takes:
1187 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001188 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001189 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001190 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1191 // FIXME: Should also highlight argument in decl.
1192 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001193 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001194 << "pointer"
1195 << IdxExpr->getSourceRange();
1196 continue;
1197 }
1198 break;
1199 }
Sean Huntcf807c42010-08-18 23:23:40 +00001200 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001201 if (AL.getNumArgs() > 1) {
1202 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001203 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001204 llvm::APSInt ArgNum(32);
1205 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1206 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1207 S.Diag(AL.getLoc(), diag::err_ownership_type)
1208 << "ownership_returns" << "integer"
1209 << IdxExpr->getSourceRange();
1210 return;
1211 }
1212 }
1213 break;
1214 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001215 } // switch
1216
1217 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001218 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001219 i = D->specific_attr_begin<OwnershipAttr>(),
1220 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001221 i != e; ++i) {
1222 if ((*i)->getOwnKind() != K) {
1223 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1224 I!=E; ++I) {
1225 if (x == *I) {
1226 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1227 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001228 }
1229 }
1230 }
1231 }
1232 OwnershipArgs.push_back(x);
1233 }
1234
1235 unsigned* start = OwnershipArgs.data();
1236 unsigned size = OwnershipArgs.size();
1237 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001238
1239 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1240 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1241 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001242 }
Sean Huntcf807c42010-08-18 23:23:40 +00001243
Chandler Carruth87c44602011-07-01 23:49:12 +00001244 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001245 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001246}
1247
John McCall332bb2a2011-02-08 22:35:49 +00001248/// Whether this declaration has internal linkage for the purposes of
1249/// things that want to complain about things not have internal linkage.
1250static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1251 switch (D->getLinkage()) {
1252 case NoLinkage:
1253 case InternalLinkage:
1254 return true;
1255
1256 // Template instantiations that go from external to unique-external
1257 // shouldn't get diagnosed.
1258 case UniqueExternalLinkage:
1259 return true;
1260
1261 case ExternalLinkage:
1262 return false;
1263 }
1264 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001265}
1266
Chandler Carruth1b03c872011-07-02 00:01:44 +00001267static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001268 // Check the attribute arguments.
1269 if (Attr.getNumArgs() > 1) {
1270 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1271 return;
1272 }
1273
Chandler Carruth87c44602011-07-01 23:49:12 +00001274 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001275 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001276 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001277 return;
1278 }
1279
Chandler Carruth87c44602011-07-01 23:49:12 +00001280 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001281
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001282 // gcc rejects
1283 // class c {
1284 // static int a __attribute__((weakref ("v2")));
1285 // static int b() __attribute__((weakref ("f3")));
1286 // };
1287 // and ignores the attributes of
1288 // void f(void) {
1289 // static int a __attribute__((weakref ("v2")));
1290 // }
1291 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001292 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001293 if (!Ctx->isFileContext()) {
1294 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001295 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001296 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001297 }
1298
1299 // The GCC manual says
1300 //
1301 // At present, a declaration to which `weakref' is attached can only
1302 // be `static'.
1303 //
1304 // It also says
1305 //
1306 // Without a TARGET,
1307 // given as an argument to `weakref' or to `alias', `weakref' is
1308 // equivalent to `weak'.
1309 //
1310 // gcc 4.4.1 will accept
1311 // int a7 __attribute__((weakref));
1312 // as
1313 // int a7 __attribute__((weak));
1314 // This looks like a bug in gcc. We reject that for now. We should revisit
1315 // it if this behaviour is actually used.
1316
John McCall332bb2a2011-02-08 22:35:49 +00001317 if (!hasEffectivelyInternalLinkage(nd)) {
1318 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001319 return;
1320 }
1321
1322 // GCC rejects
1323 // static ((alias ("y"), weakref)).
1324 // Should we? How to check that weakref is before or after alias?
1325
1326 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001327 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001328 Arg = Arg->IgnoreParenCasts();
1329 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1330
Douglas Gregor5cee1192011-07-27 05:40:30 +00001331 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1333 << "weakref" << 1;
1334 return;
1335 }
1336 // GCC will accept anything as the argument of weakref. Should we
1337 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001338 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001339 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001340 }
1341
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001342 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001343}
1344
Chandler Carruth1b03c872011-07-02 00:01:44 +00001345static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001346 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001347 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001348 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001349 return;
1350 }
Mike Stumpbf916502009-07-24 19:02:52 +00001351
Peter Collingbourne7a730022010-11-23 20:45:58 +00001352 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001353 Arg = Arg->IgnoreParenCasts();
1354 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001355
Douglas Gregor5cee1192011-07-27 05:40:30 +00001356 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001358 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001359 return;
1360 }
Mike Stumpbf916502009-07-24 19:02:52 +00001361
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001362 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001363 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1364 return;
1365 }
1366
Chris Lattner6b6b5372008-06-26 18:38:35 +00001367 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001368
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001369 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001370 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001371}
1372
Benjamin Krameree409a92012-05-12 21:10:52 +00001373static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1374 // Check the attribute arguments.
1375 if (!checkAttributeNumArgs(S, Attr, 0))
1376 return;
1377
1378 if (!isa<FunctionDecl>(D)) {
1379 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1380 << Attr.getName() << ExpectedFunction;
1381 return;
1382 }
1383
1384 if (D->hasAttr<HotAttr>()) {
1385 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1386 << Attr.getName() << "hot";
1387 return;
1388 }
1389
1390 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1391}
1392
1393static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1394 // Check the attribute arguments.
1395 if (!checkAttributeNumArgs(S, Attr, 0))
1396 return;
1397
1398 if (!isa<FunctionDecl>(D)) {
1399 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1400 << Attr.getName() << ExpectedFunction;
1401 return;
1402 }
1403
1404 if (D->hasAttr<ColdAttr>()) {
1405 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1406 << Attr.getName() << "cold";
1407 return;
1408 }
1409
1410 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1411}
1412
Chandler Carruth1b03c872011-07-02 00:01:44 +00001413static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001414 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001415 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001416 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001417
Chandler Carruth87c44602011-07-01 23:49:12 +00001418 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001420 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001421 return;
1422 }
1423
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001424 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001425}
1426
Chandler Carruth1b03c872011-07-02 00:01:44 +00001427static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1428 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001429 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001430 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1432 return;
1433 }
1434
Chandler Carruth87c44602011-07-01 23:49:12 +00001435 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001437 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001438 return;
1439 }
Mike Stumpbf916502009-07-24 19:02:52 +00001440
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001441 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001442}
1443
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001444static void handleTLSModelAttr(Sema &S, Decl *D,
1445 const AttributeList &Attr) {
1446 // Check the attribute arguments.
1447 if (Attr.getNumArgs() != 1) {
1448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1449 return;
1450 }
1451
1452 Expr *Arg = Attr.getArg(0);
1453 Arg = Arg->IgnoreParenCasts();
1454 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1455
1456 // Check that it is a string.
1457 if (!Str) {
1458 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1459 return;
1460 }
1461
1462 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1463 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1464 << Attr.getName() << ExpectedTLSVar;
1465 return;
1466 }
1467
1468 // Check that the value.
1469 StringRef Model = Str->getString();
1470 if (Model != "global-dynamic" && Model != "local-dynamic"
1471 && Model != "initial-exec" && Model != "local-exec") {
1472 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1473 return;
1474 }
1475
1476 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1477 Model));
1478}
1479
Chandler Carruth1b03c872011-07-02 00:01:44 +00001480static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001481 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001482 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1484 return;
1485 }
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Chandler Carruth87c44602011-07-01 23:49:12 +00001487 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001488 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001489 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001490 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001491 return;
1492 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001493 }
1494
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001495 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001496}
1497
Chandler Carruth1b03c872011-07-02 00:01:44 +00001498static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001499 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001500 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001501 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001502
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001503 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001504}
1505
Chandler Carruth1b03c872011-07-02 00:01:44 +00001506static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001507 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001508 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001509 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001510 else
1511 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001512 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001513}
1514
Chandler Carruth1b03c872011-07-02 00:01:44 +00001515static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001516 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001517 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001518 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001519 else
1520 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001521 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001522}
1523
Chandler Carruth1b03c872011-07-02 00:01:44 +00001524static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001525 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001526
1527 if (S.CheckNoReturnAttr(attr)) return;
1528
Chandler Carruth87c44602011-07-01 23:49:12 +00001529 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001530 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001531 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001532 return;
1533 }
1534
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001535 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001536}
1537
1538bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001539 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001540 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1541 attr.setInvalid();
1542 return true;
1543 }
1544
1545 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001546}
1547
Chandler Carruth1b03c872011-07-02 00:01:44 +00001548static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1549 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001550
1551 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1552 // because 'analyzer_noreturn' does not impact the type.
1553
Chandler Carruth1731e202011-07-11 23:30:35 +00001554 if(!checkAttributeNumArgs(S, Attr, 0))
1555 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001556
Chandler Carruth87c44602011-07-01 23:49:12 +00001557 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1558 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001559 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1560 && !VD->getType()->isFunctionPointerType())) {
1561 S.Diag(Attr.getLoc(),
1562 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1563 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001564 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001565 return;
1566 }
1567 }
1568
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001569 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001570}
1571
John Thompson35cc9622010-08-09 21:53:52 +00001572// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001573static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001574/*
1575 Returning a Vector Class in Registers
1576
Eric Christopherf48f3672010-12-01 22:13:54 +00001577 According to the PPU ABI specifications, a class with a single member of
1578 vector type is returned in memory when used as the return value of a function.
1579 This results in inefficient code when implementing vector classes. To return
1580 the value in a single vector register, add the vecreturn attribute to the
1581 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001582
1583 Example:
1584
1585 struct Vector
1586 {
1587 __vector float xyzw;
1588 } __attribute__((vecreturn));
1589
1590 Vector Add(Vector lhs, Vector rhs)
1591 {
1592 Vector result;
1593 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1594 return result; // This will be returned in a register
1595 }
1596*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001597 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001598 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001599 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001600 return;
1601 }
1602
Chandler Carruth87c44602011-07-01 23:49:12 +00001603 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001604 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1605 return;
1606 }
1607
Chandler Carruth87c44602011-07-01 23:49:12 +00001608 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001609 int count = 0;
1610
1611 if (!isa<CXXRecordDecl>(record)) {
1612 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1613 return;
1614 }
1615
1616 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1617 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1618 return;
1619 }
1620
Eric Christopherf48f3672010-12-01 22:13:54 +00001621 for (RecordDecl::field_iterator iter = record->field_begin();
1622 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001623 if ((count == 1) || !iter->getType()->isVectorType()) {
1624 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1625 return;
1626 }
1627 count++;
1628 }
1629
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001630 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001631}
1632
Chandler Carruth1b03c872011-07-02 00:01:44 +00001633static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001634 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001636 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001637 return;
1638 }
1639 // FIXME: Actually store the attribute on the declaration
1640}
1641
Chandler Carruth1b03c872011-07-02 00:01:44 +00001642static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001643 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001644 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001646 return;
1647 }
Mike Stumpbf916502009-07-24 19:02:52 +00001648
Chandler Carruth87c44602011-07-01 23:49:12 +00001649 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001650 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001652 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001653 return;
1654 }
Mike Stumpbf916502009-07-24 19:02:52 +00001655
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001656 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001657}
1658
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001659static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1660 const AttributeList &Attr) {
1661 // check the attribute arguments.
1662 if (Attr.hasParameterOrArguments()) {
1663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1664 return;
1665 }
1666
1667 if (!isa<FunctionDecl>(D)) {
1668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1669 << Attr.getName() << ExpectedFunction;
1670 return;
1671 }
1672
1673 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1674}
1675
Chandler Carruth1b03c872011-07-02 00:01:44 +00001676static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001677 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001678 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1680 return;
1681 }
Mike Stumpbf916502009-07-24 19:02:52 +00001682
Chandler Carruth87c44602011-07-01 23:49:12 +00001683 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001684 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001685 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1686 return;
1687 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001688 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001690 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001691 return;
1692 }
Mike Stumpbf916502009-07-24 19:02:52 +00001693
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001694 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001695}
1696
Chandler Carruth1b03c872011-07-02 00:01:44 +00001697static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001698 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001699 if (Attr.getNumArgs() > 1) {
1700 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001701 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001702 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001703
1704 int priority = 65535; // FIXME: Do not hardcode such constants.
1705 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001706 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001707 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001708 if (E->isTypeDependent() || E->isValueDependent() ||
1709 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001711 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001712 return;
1713 }
1714 priority = Idx.getZExtValue();
1715 }
Mike Stumpbf916502009-07-24 19:02:52 +00001716
Chandler Carruth87c44602011-07-01 23:49:12 +00001717 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001718 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001719 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001720 return;
1721 }
1722
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001723 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001724 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001725}
1726
Chandler Carruth1b03c872011-07-02 00:01:44 +00001727static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001728 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001729 if (Attr.getNumArgs() > 1) {
1730 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001731 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001732 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001733
1734 int priority = 65535; // FIXME: Do not hardcode such constants.
1735 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001736 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001737 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001738 if (E->isTypeDependent() || E->isValueDependent() ||
1739 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001741 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001742 return;
1743 }
1744 priority = Idx.getZExtValue();
1745 }
Mike Stumpbf916502009-07-24 19:02:52 +00001746
Chandler Carruth87c44602011-07-01 23:49:12 +00001747 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001748 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001749 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001750 return;
1751 }
1752
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001753 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001754 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001755}
1756
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001757template <typename AttrTy>
1758static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1759 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001760 unsigned NumArgs = Attr.getNumArgs();
1761 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001762 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001763 return;
1764 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001765
1766 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001767 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001768 if (NumArgs == 1) {
1769 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001770 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001771 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001772 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001773 return;
1774 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001775 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001776 }
Mike Stumpbf916502009-07-24 19:02:52 +00001777
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001778 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001779}
1780
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001781static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1782 const AttributeList &Attr) {
1783 unsigned NumArgs = Attr.getNumArgs();
1784 if (NumArgs > 0) {
1785 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1786 return;
1787 }
1788
1789 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001790 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001791}
1792
Patrick Beardb2f68202012-04-06 18:12:22 +00001793static void handleObjCRootClassAttr(Sema &S, Decl *D,
1794 const AttributeList &Attr) {
1795 if (!isa<ObjCInterfaceDecl>(D)) {
1796 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1797 return;
1798 }
1799
1800 unsigned NumArgs = Attr.getNumArgs();
1801 if (NumArgs > 0) {
1802 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1803 return;
1804 }
1805
1806 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1807}
1808
Ted Kremenek71207fc2012-01-05 22:47:47 +00001809static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001810 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001811 if (!isa<ObjCInterfaceDecl>(D)) {
1812 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1813 return;
1814 }
1815
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001816 unsigned NumArgs = Attr.getNumArgs();
1817 if (NumArgs > 0) {
1818 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1819 return;
1820 }
1821
Ted Kremenek71207fc2012-01-05 22:47:47 +00001822 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001823 Attr.getRange(), S.Context));
1824}
1825
Jordy Rosefad5de92012-05-08 03:27:22 +00001826static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1827 IdentifierInfo *Platform,
1828 VersionTuple Introduced,
1829 VersionTuple Deprecated,
1830 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001831 StringRef PlatformName
1832 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1833 if (PlatformName.empty())
1834 PlatformName = Platform->getName();
1835
1836 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1837 // of these steps are needed).
1838 if (!Introduced.empty() && !Deprecated.empty() &&
1839 !(Introduced <= Deprecated)) {
1840 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1841 << 1 << PlatformName << Deprecated.getAsString()
1842 << 0 << Introduced.getAsString();
1843 return true;
1844 }
1845
1846 if (!Introduced.empty() && !Obsoleted.empty() &&
1847 !(Introduced <= Obsoleted)) {
1848 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1849 << 2 << PlatformName << Obsoleted.getAsString()
1850 << 0 << Introduced.getAsString();
1851 return true;
1852 }
1853
1854 if (!Deprecated.empty() && !Obsoleted.empty() &&
1855 !(Deprecated <= Obsoleted)) {
1856 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1857 << 2 << PlatformName << Obsoleted.getAsString()
1858 << 1 << Deprecated.getAsString();
1859 return true;
1860 }
1861
1862 return false;
1863}
1864
Rafael Espindola599f1b72012-05-13 03:25:18 +00001865AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1866 IdentifierInfo *Platform,
1867 VersionTuple Introduced,
1868 VersionTuple Deprecated,
1869 VersionTuple Obsoleted,
1870 bool IsUnavailable,
1871 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00001872 VersionTuple MergedIntroduced = Introduced;
1873 VersionTuple MergedDeprecated = Deprecated;
1874 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00001875 bool FoundAny = false;
1876
Rafael Espindola98ae8342012-05-10 02:50:16 +00001877 if (D->hasAttrs()) {
1878 AttrVec &Attrs = D->getAttrs();
1879 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1880 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1881 if (!OldAA) {
1882 ++i;
1883 continue;
1884 }
Rafael Espindola3b294362012-05-06 19:56:25 +00001885
Rafael Espindola98ae8342012-05-10 02:50:16 +00001886 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1887 if (OldPlatform != Platform) {
1888 ++i;
1889 continue;
1890 }
1891
1892 FoundAny = true;
1893 VersionTuple OldIntroduced = OldAA->getIntroduced();
1894 VersionTuple OldDeprecated = OldAA->getDeprecated();
1895 VersionTuple OldObsoleted = OldAA->getObsoleted();
1896 bool OldIsUnavailable = OldAA->getUnavailable();
1897 StringRef OldMessage = OldAA->getMessage();
1898
1899 if ((!OldIntroduced.empty() && !Introduced.empty() &&
1900 OldIntroduced != Introduced) ||
1901 (!OldDeprecated.empty() && !Deprecated.empty() &&
1902 OldDeprecated != Deprecated) ||
1903 (!OldObsoleted.empty() && !Obsoleted.empty() &&
1904 OldObsoleted != Obsoleted) ||
1905 (OldIsUnavailable != IsUnavailable) ||
1906 (OldMessage != Message)) {
1907 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1908 Diag(Range.getBegin(), diag::note_previous_attribute);
1909 Attrs.erase(Attrs.begin() + i);
1910 --e;
1911 continue;
1912 }
1913
1914 VersionTuple MergedIntroduced2 = MergedIntroduced;
1915 VersionTuple MergedDeprecated2 = MergedDeprecated;
1916 VersionTuple MergedObsoleted2 = MergedObsoleted;
1917
1918 if (MergedIntroduced2.empty())
1919 MergedIntroduced2 = OldIntroduced;
1920 if (MergedDeprecated2.empty())
1921 MergedDeprecated2 = OldDeprecated;
1922 if (MergedObsoleted2.empty())
1923 MergedObsoleted2 = OldObsoleted;
1924
1925 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1926 MergedIntroduced2, MergedDeprecated2,
1927 MergedObsoleted2)) {
1928 Attrs.erase(Attrs.begin() + i);
1929 --e;
1930 continue;
1931 }
1932
1933 MergedIntroduced = MergedIntroduced2;
1934 MergedDeprecated = MergedDeprecated2;
1935 MergedObsoleted = MergedObsoleted2;
1936 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00001937 }
Rafael Espindola3b294362012-05-06 19:56:25 +00001938 }
1939
1940 if (FoundAny &&
1941 MergedIntroduced == Introduced &&
1942 MergedDeprecated == Deprecated &&
1943 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00001944 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00001945
Rafael Espindola98ae8342012-05-10 02:50:16 +00001946 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00001947 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00001948 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1949 Introduced, Deprecated,
1950 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00001951 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00001952 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00001953}
1954
Chandler Carruth1b03c872011-07-02 00:01:44 +00001955static void handleAvailabilityAttr(Sema &S, Decl *D,
1956 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001957 IdentifierInfo *Platform = Attr.getParameterName();
1958 SourceLocation PlatformLoc = Attr.getParameterLoc();
1959
Rafael Espindola3b294362012-05-06 19:56:25 +00001960 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001961 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1962 << Platform;
1963
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001964 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1965 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1966 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001967 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001968 StringRef Str;
1969 const StringLiteral *SE =
1970 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1971 if (SE)
1972 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00001973
Rafael Espindola599f1b72012-05-13 03:25:18 +00001974 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
1975 Platform,
1976 Introduced.Version,
1977 Deprecated.Version,
1978 Obsoleted.Version,
1979 IsUnavailable, Str);
1980 if (NewAttr)
1981 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00001982}
1983
Rafael Espindola599f1b72012-05-13 03:25:18 +00001984VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
1985 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00001986 if (isa<TypedefNameDecl>(D)) {
1987 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00001988 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00001989 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00001990 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
1991 if (ExistingAttr) {
1992 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
1993 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00001994 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00001995 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
1996 Diag(Range.getBegin(), diag::note_previous_attribute);
1997 D->dropAttr<VisibilityAttr>();
1998 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00001999 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002000}
2001
Chandler Carruth1b03c872011-07-02 00:01:44 +00002002static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002003 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002004 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002005 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002006
Peter Collingbourne7a730022010-11-23 20:45:58 +00002007 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002008 Arg = Arg->IgnoreParenCasts();
2009 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002010
Douglas Gregor5cee1192011-07-27 05:40:30 +00002011 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002012 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002013 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002014 return;
2015 }
Mike Stumpbf916502009-07-24 19:02:52 +00002016
Chris Lattner5f9e2722011-07-23 10:55:15 +00002017 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002018 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002019
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002020 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002021 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002022 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002023 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002024 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002025 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002026 else if (TypeStr == "protected") {
2027 // Complain about attempts to use protected visibility on targets
2028 // (like Darwin) that don't support it.
2029 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2030 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2031 type = VisibilityAttr::Default;
2032 } else {
2033 type = VisibilityAttr::Protected;
2034 }
2035 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002036 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002037 return;
2038 }
Mike Stumpbf916502009-07-24 19:02:52 +00002039
Rafael Espindola599f1b72012-05-13 03:25:18 +00002040 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2041 if (NewAttr)
2042 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002043}
2044
Chandler Carruth1b03c872011-07-02 00:01:44 +00002045static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2046 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002047 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2048 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002049 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002050 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002051 return;
2052 }
2053
Chandler Carruth87c44602011-07-01 23:49:12 +00002054 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2055 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2056 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002057 << "objc_method_family" << 1;
2058 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002060 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002061 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002062 return;
2063 }
2064
Chris Lattner5f9e2722011-07-23 10:55:15 +00002065 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002066 ObjCMethodFamilyAttr::FamilyKind family;
2067 if (param == "none")
2068 family = ObjCMethodFamilyAttr::OMF_None;
2069 else if (param == "alloc")
2070 family = ObjCMethodFamilyAttr::OMF_alloc;
2071 else if (param == "copy")
2072 family = ObjCMethodFamilyAttr::OMF_copy;
2073 else if (param == "init")
2074 family = ObjCMethodFamilyAttr::OMF_init;
2075 else if (param == "mutableCopy")
2076 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2077 else if (param == "new")
2078 family = ObjCMethodFamilyAttr::OMF_new;
2079 else {
2080 // Just warn and ignore it. This is future-proof against new
2081 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002082 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002083 return;
2084 }
2085
John McCallf85e1932011-06-15 23:02:42 +00002086 if (family == ObjCMethodFamilyAttr::OMF_init &&
2087 !method->getResultType()->isObjCObjectPointerType()) {
2088 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2089 << method->getResultType();
2090 // Ignore the attribute.
2091 return;
2092 }
2093
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002094 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002095 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002096}
2097
Chandler Carruth1b03c872011-07-02 00:01:44 +00002098static void handleObjCExceptionAttr(Sema &S, Decl *D,
2099 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002100 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002101 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002102
Chris Lattner0db29ec2009-02-14 08:09:34 +00002103 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2104 if (OCI == 0) {
2105 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2106 return;
2107 }
Mike Stumpbf916502009-07-24 19:02:52 +00002108
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002109 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002110}
2111
Chandler Carruth1b03c872011-07-02 00:01:44 +00002112static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002113 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002114 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002115 return;
2116 }
Richard Smith162e1c12011-04-15 14:24:37 +00002117 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002118 QualType T = TD->getUnderlyingType();
2119 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002120 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002121 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2122 return;
2123 }
2124 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002125 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2126 QualType T = PD->getType();
2127 if (!T->isPointerType() ||
2128 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
2129 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2130 return;
2131 }
2132 }
2133 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002134 // It is okay to include this attribute on properties, e.g.:
2135 //
2136 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2137 //
2138 // In this case it follows tradition and suppresses an error in the above
2139 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002140 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002141 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002142 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002143}
2144
Mike Stumpbf916502009-07-24 19:02:52 +00002145static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002146handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002147 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002149 return;
2150 }
2151
2152 if (!isa<FunctionDecl>(D)) {
2153 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2154 return;
2155 }
2156
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002157 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002158}
2159
Chandler Carruth1b03c872011-07-02 00:01:44 +00002160static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002161 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002163 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002164 return;
2165 }
Mike Stumpbf916502009-07-24 19:02:52 +00002166
Steve Naroff9eae5762008-09-18 16:44:58 +00002167 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002169 return;
2170 }
Mike Stumpbf916502009-07-24 19:02:52 +00002171
Sean Huntcf807c42010-08-18 23:23:40 +00002172 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002173 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002174 type = BlocksAttr::ByRef;
2175 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002176 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002177 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002178 return;
2179 }
Mike Stumpbf916502009-07-24 19:02:52 +00002180
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002181 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002182}
2183
Chandler Carruth1b03c872011-07-02 00:01:44 +00002184static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002185 // check the attribute arguments.
2186 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002187 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002188 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002189 }
2190
John McCall3323fad2011-09-09 07:56:05 +00002191 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002192 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002193 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002194 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002195 if (E->isTypeDependent() || E->isValueDependent() ||
2196 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002198 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002199 return;
2200 }
Mike Stumpbf916502009-07-24 19:02:52 +00002201
John McCall3323fad2011-09-09 07:56:05 +00002202 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002203 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2204 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002205 return;
2206 }
John McCall3323fad2011-09-09 07:56:05 +00002207
2208 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002209 }
2210
John McCall3323fad2011-09-09 07:56:05 +00002211 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002212 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002213 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002214 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002215 if (E->isTypeDependent() || E->isValueDependent() ||
2216 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002217 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002218 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002219 return;
2220 }
2221 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002222
John McCall3323fad2011-09-09 07:56:05 +00002223 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002224 // FIXME: This error message could be improved, it would be nice
2225 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002226 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2227 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002228 return;
2229 }
2230 }
2231
Chandler Carruth87c44602011-07-01 23:49:12 +00002232 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002233 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002234 if (isa<FunctionNoProtoType>(FT)) {
2235 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2236 return;
2237 }
Mike Stumpbf916502009-07-24 19:02:52 +00002238
Chris Lattner897cd902009-03-17 23:03:47 +00002239 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002240 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002241 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002242 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002243 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002244 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002245 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002246 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002247 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002248 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2249 if (!BD->isVariadic()) {
2250 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2251 return;
2252 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002253 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002254 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002255 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002256 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002257 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002258 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002259 int m = Ty->isFunctionPointerType() ? 0 : 1;
2260 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002261 return;
2262 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002263 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002264 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002265 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002266 return;
2267 }
Anders Carlsson77091822008-10-05 18:05:59 +00002268 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002269 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002270 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002271 return;
2272 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002273 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002274 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002275}
2276
Chandler Carruth1b03c872011-07-02 00:01:44 +00002277static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002278 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002279 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002280 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002281
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002282 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002283 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002284 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002285 return;
2286 }
Mike Stumpbf916502009-07-24 19:02:52 +00002287
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002288 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2289 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2290 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002291 return;
2292 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002293 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2294 if (MD->getResultType()->isVoidType()) {
2295 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2296 << Attr.getName() << 1;
2297 return;
2298 }
2299
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002300 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002301}
2302
Chandler Carruth1b03c872011-07-02 00:01:44 +00002303static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002304 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002305 if (Attr.hasParameterOrArguments()) {
2306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002307 return;
2308 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002309
Chandler Carruth87c44602011-07-01 23:49:12 +00002310 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002311 if (isa<CXXRecordDecl>(D)) {
2312 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2313 return;
2314 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2316 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002317 return;
2318 }
2319
Chandler Carruth87c44602011-07-01 23:49:12 +00002320 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002321
2322 // 'weak' only applies to declarations with external linkage.
2323 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002324 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002325 return;
2326 }
Mike Stumpbf916502009-07-24 19:02:52 +00002327
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002328 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002329}
2330
Chandler Carruth1b03c872011-07-02 00:01:44 +00002331static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002332 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002333 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002334 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002335
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002336
2337 // weak_import only applies to variable & function declarations.
2338 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002339 if (!D->canBeWeakImported(isDef)) {
2340 if (isDef)
2341 S.Diag(Attr.getLoc(),
2342 diag::warn_attribute_weak_import_invalid_on_definition)
2343 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002344 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002345 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002346 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002347 // Nothing to warn about here.
2348 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002349 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002350 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002351
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002352 return;
2353 }
2354
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002355 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002356}
2357
Chandler Carruth1b03c872011-07-02 00:01:44 +00002358static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2359 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002360 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002361 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002362 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002363
2364 unsigned WGSize[3];
2365 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002366 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002367 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002368 if (E->isTypeDependent() || E->isValueDependent() ||
2369 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2371 << "reqd_work_group_size" << E->getSourceRange();
2372 return;
2373 }
2374 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2375 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002376 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002377 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002378 WGSize[2]));
2379}
2380
Rafael Espindola599f1b72012-05-13 03:25:18 +00002381SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2382 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002383 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2384 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002385 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002386 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2387 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002388 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002389 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002390 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002391}
2392
Chandler Carruth1b03c872011-07-02 00:01:44 +00002393static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002394 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002395 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002396 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002397
2398 // Make sure that there is a string literal as the sections's single
2399 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002400 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002401 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002402 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002403 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002404 return;
2405 }
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Chris Lattner797c3c42009-08-10 19:03:04 +00002407 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002408 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002409 if (!Error.empty()) {
2410 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2411 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002412 return;
2413 }
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002415 // This attribute cannot be applied to local variables.
2416 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2417 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2418 return;
2419 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002420 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2421 SE->getString());
2422 if (NewAttr)
2423 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002424}
2425
Chris Lattner6b6b5372008-06-26 18:38:35 +00002426
Chandler Carruth1b03c872011-07-02 00:01:44 +00002427static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002428 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002429 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002431 return;
2432 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002433
Chandler Carruth87c44602011-07-01 23:49:12 +00002434 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002435 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002436 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002437 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002438 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002439 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002440}
2441
Chandler Carruth1b03c872011-07-02 00:01:44 +00002442static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002443 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002444 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002445 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002446 return;
2447 }
Mike Stumpbf916502009-07-24 19:02:52 +00002448
Chandler Carruth87c44602011-07-01 23:49:12 +00002449 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002450 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002451 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002452 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002453 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002454 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002455}
2456
Chandler Carruth1b03c872011-07-02 00:01:44 +00002457static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002458 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002459 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002460 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002461
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002462 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002463}
2464
Chandler Carruth1b03c872011-07-02 00:01:44 +00002465static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002466 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2468 return;
2469 }
Mike Stumpbf916502009-07-24 19:02:52 +00002470
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002471 if (Attr.getNumArgs() != 0) {
2472 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2473 return;
2474 }
Mike Stumpbf916502009-07-24 19:02:52 +00002475
Chandler Carruth87c44602011-07-01 23:49:12 +00002476 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002477
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002478 if (!VD || !VD->hasLocalStorage()) {
2479 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2480 return;
2481 }
Mike Stumpbf916502009-07-24 19:02:52 +00002482
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002483 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002484 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002485 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002486 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2487 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002488 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002489 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002490 Attr.getParameterName();
2491 return;
2492 }
Mike Stumpbf916502009-07-24 19:02:52 +00002493
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002494 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2495 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002496 S.Diag(Attr.getParameterLoc(),
2497 diag::err_attribute_cleanup_arg_not_function)
2498 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002499 return;
2500 }
2501
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002502 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002503 S.Diag(Attr.getParameterLoc(),
2504 diag::err_attribute_cleanup_func_must_take_one_arg)
2505 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002506 return;
2507 }
Mike Stumpbf916502009-07-24 19:02:52 +00002508
Anders Carlsson89941c12009-02-07 23:16:50 +00002509 // We're currently more strict than GCC about what function types we accept.
2510 // If this ever proves to be a problem it should be easy to fix.
2511 QualType Ty = S.Context.getPointerType(VD->getType());
2512 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002513 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2514 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002515 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002516 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2517 Attr.getParameterName() << ParamTy << Ty;
2518 return;
2519 }
Mike Stumpbf916502009-07-24 19:02:52 +00002520
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002521 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002522 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002523}
2524
Mike Stumpbf916502009-07-24 19:02:52 +00002525/// Handle __attribute__((format_arg((idx)))) attribute based on
2526/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002527static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002528 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002529 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002530
Chandler Carruth87c44602011-07-01 23:49:12 +00002531 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002533 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002534 return;
2535 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002536
2537 // In C++ the implicit 'this' function parameter also counts, and they are
2538 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002539 bool HasImplicitThisParam = isInstanceMethod(D);
2540 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002541 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002542
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002543 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002544 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002545 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002546 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2547 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2549 << "format" << 2 << IdxExpr->getSourceRange();
2550 return;
2551 }
Mike Stumpbf916502009-07-24 19:02:52 +00002552
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002553 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2554 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2555 << "format" << 2 << IdxExpr->getSourceRange();
2556 return;
2557 }
Mike Stumpbf916502009-07-24 19:02:52 +00002558
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002559 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002560
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002561 if (HasImplicitThisParam) {
2562 if (ArgIdx == 0) {
2563 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2564 << "format_arg" << IdxExpr->getSourceRange();
2565 return;
2566 }
2567 ArgIdx--;
2568 }
2569
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002570 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002571 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002572
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002573 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2574 if (not_nsstring_type &&
2575 !isCFStringType(Ty, S.Context) &&
2576 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002577 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002578 // FIXME: Should highlight the actual expression that has the wrong type.
2579 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002580 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002581 << IdxExpr->getSourceRange();
2582 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002583 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002584 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002585 if (!isNSStringType(Ty, S.Context) &&
2586 !isCFStringType(Ty, S.Context) &&
2587 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002588 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002589 // FIXME: Should highlight the actual expression that has the wrong type.
2590 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002591 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002592 << IdxExpr->getSourceRange();
2593 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002594 }
2595
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002596 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002597 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002598}
2599
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002600enum FormatAttrKind {
2601 CFStringFormat,
2602 NSStringFormat,
2603 StrftimeFormat,
2604 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002605 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002606 InvalidFormat
2607};
2608
2609/// getFormatAttrKind - Map from format attribute names to supported format
2610/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002611static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002612 return llvm::StringSwitch<FormatAttrKind>(Format)
2613 // Check for formats that get handled specially.
2614 .Case("NSString", NSStringFormat)
2615 .Case("CFString", CFStringFormat)
2616 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002617
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002618 // Otherwise, check for supported formats.
2619 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2620 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2621 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002622
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002623 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2624 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002625}
2626
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002627/// Handle __attribute__((init_priority(priority))) attributes based on
2628/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002629static void handleInitPriorityAttr(Sema &S, Decl *D,
2630 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002631 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002632 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2633 return;
2634 }
2635
Chandler Carruth87c44602011-07-01 23:49:12 +00002636 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002637 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2638 Attr.setInvalid();
2639 return;
2640 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002641 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002642 if (S.Context.getAsArrayType(T))
2643 T = S.Context.getBaseElementType(T);
2644 if (!T->getAs<RecordType>()) {
2645 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2646 Attr.setInvalid();
2647 return;
2648 }
2649
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002650 if (Attr.getNumArgs() != 1) {
2651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2652 Attr.setInvalid();
2653 return;
2654 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002655 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002656
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002657 llvm::APSInt priority(32);
2658 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2659 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2660 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2661 << "init_priority" << priorityExpr->getSourceRange();
2662 Attr.setInvalid();
2663 return;
2664 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002665 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002666 if (prioritynum < 101 || prioritynum > 65535) {
2667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2668 << priorityExpr->getSourceRange();
2669 Attr.setInvalid();
2670 return;
2671 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002672 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002673 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002674}
2675
Rafael Espindola599f1b72012-05-13 03:25:18 +00002676FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2677 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002678 // Check whether we already have an equivalent format attribute.
2679 for (specific_attr_iterator<FormatAttr>
2680 i = D->specific_attr_begin<FormatAttr>(),
2681 e = D->specific_attr_end<FormatAttr>();
2682 i != e ; ++i) {
2683 FormatAttr *f = *i;
2684 if (f->getType() == Format &&
2685 f->getFormatIdx() == FormatIdx &&
2686 f->getFirstArg() == FirstArg) {
2687 // If we don't have a valid location for this attribute, adopt the
2688 // location.
2689 if (f->getLocation().isInvalid())
2690 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002691 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002692 }
2693 }
2694
Rafael Espindola599f1b72012-05-13 03:25:18 +00002695 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2696 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002697}
2698
Mike Stumpbf916502009-07-24 19:02:52 +00002699/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2700/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002701static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002702
Chris Lattner545dd342008-06-28 23:36:30 +00002703 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002704 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002705 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002706 return;
2707 }
2708
Chris Lattner545dd342008-06-28 23:36:30 +00002709 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002711 return;
2712 }
2713
Chandler Carruth87c44602011-07-01 23:49:12 +00002714 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002716 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002717 return;
2718 }
2719
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002720 // In C++ the implicit 'this' function parameter also counts, and they are
2721 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002722 bool HasImplicitThisParam = isInstanceMethod(D);
2723 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002724 unsigned FirstIdx = 1;
2725
Chris Lattner5f9e2722011-07-23 10:55:15 +00002726 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002727
2728 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002729 if (Format.startswith("__") && Format.endswith("__"))
2730 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002731
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002732 // Check for supported formats.
2733 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002734
2735 if (Kind == IgnoredFormat)
2736 return;
2737
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002738 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002739 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002740 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002741 return;
2742 }
2743
2744 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002745 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002746 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002747 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2748 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002749 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002750 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002751 return;
2752 }
2753
2754 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002755 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002756 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002757 return;
2758 }
2759
2760 // FIXME: Do we need to bounds check?
2761 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002762
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002763 if (HasImplicitThisParam) {
2764 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002765 S.Diag(Attr.getLoc(),
2766 diag::err_format_attribute_implicit_this_format_string)
2767 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002768 return;
2769 }
2770 ArgIdx--;
2771 }
Mike Stump1eb44332009-09-09 15:08:12 +00002772
Chris Lattner6b6b5372008-06-26 18:38:35 +00002773 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002774 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002775
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002776 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002777 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002778 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2779 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002780 return;
2781 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002782 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002783 // FIXME: do we need to check if the type is NSString*? What are the
2784 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002785 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002786 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002787 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2788 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002789 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002790 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002791 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002792 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002793 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002794 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2795 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002796 return;
2797 }
2798
2799 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002800 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002801 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002802 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2803 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002804 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002805 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002806 return;
2807 }
2808
2809 // check if the function is variadic if the 3rd argument non-zero
2810 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002811 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002812 ++NumArgs; // +1 for ...
2813 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002814 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002815 return;
2816 }
2817 }
2818
Chris Lattner3c73c412008-11-19 08:23:25 +00002819 // strftime requires FirstArg to be 0 because it doesn't read from any
2820 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002821 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002822 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002823 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2824 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002825 return;
2826 }
2827 // if 0 it disables parameter checking (to use with e.g. va_list)
2828 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002829 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002830 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002831 return;
2832 }
2833
Rafael Espindola599f1b72012-05-13 03:25:18 +00002834 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
2835 Idx.getZExtValue(),
2836 FirstArg.getZExtValue());
2837 if (NewAttr)
2838 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002839}
2840
Chandler Carruth1b03c872011-07-02 00:01:44 +00002841static void handleTransparentUnionAttr(Sema &S, Decl *D,
2842 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002843 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002844 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002845 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002846
Chris Lattner6b6b5372008-06-26 18:38:35 +00002847
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002848 // Try to find the underlying union declaration.
2849 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002850 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002851 if (TD && TD->getUnderlyingType()->isUnionType())
2852 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2853 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002854 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002855
2856 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002858 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002859 return;
2860 }
2861
John McCall5e1cdac2011-10-07 06:10:15 +00002862 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002863 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002864 diag::warn_transparent_union_attribute_not_definition);
2865 return;
2866 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002867
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002868 RecordDecl::field_iterator Field = RD->field_begin(),
2869 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002870 if (Field == FieldEnd) {
2871 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2872 return;
2873 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002874
David Blaikie581deb32012-06-06 20:45:41 +00002875 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002876 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002877 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002878 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002879 diag::warn_transparent_union_attribute_floating)
2880 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002881 return;
2882 }
2883
2884 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2885 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2886 for (; Field != FieldEnd; ++Field) {
2887 QualType FieldType = Field->getType();
2888 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2889 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2890 // Warn if we drop the attribute.
2891 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002892 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002893 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002894 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002895 diag::warn_transparent_union_attribute_field_size_align)
2896 << isSize << Field->getDeclName() << FieldBits;
2897 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002898 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002899 diag::note_transparent_union_first_field_size_align)
2900 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002901 return;
2902 }
2903 }
2904
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002905 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002906}
2907
Chandler Carruth1b03c872011-07-02 00:01:44 +00002908static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002909 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002910 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002911 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002912
Peter Collingbourne7a730022010-11-23 20:45:58 +00002913 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002914 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002915
Chris Lattner6b6b5372008-06-26 18:38:35 +00002916 // Make sure that there is a string literal as the annotation's single
2917 // argument.
2918 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002919 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002920 return;
2921 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002922
2923 // Don't duplicate annotations that are already set.
2924 for (specific_attr_iterator<AnnotateAttr>
2925 i = D->specific_attr_begin<AnnotateAttr>(),
2926 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2927 if ((*i)->getAnnotation() == SE->getString())
2928 return;
2929 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002930 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002931 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002932}
2933
Chandler Carruth1b03c872011-07-02 00:01:44 +00002934static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002935 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002936 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002938 return;
2939 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002940
Sean Huntbbd37c62009-11-21 08:43:09 +00002941 //FIXME: The C++0x version of this attribute has more limited applicabilty
2942 // than GNU's, and should error out when it is used to specify a
2943 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002944
Chris Lattner545dd342008-06-28 23:36:30 +00002945 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002946 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2947 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002948 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002949 }
Mike Stumpbf916502009-07-24 19:02:52 +00002950
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002951 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
2952 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002953}
2954
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002955void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
2956 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002957 // FIXME: Handle pack-expansions here.
2958 if (DiagnoseUnexpandedParameterPack(E))
2959 return;
2960
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002961 if (E->isTypeDependent() || E->isValueDependent()) {
2962 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002963 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
2964 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002965 return;
2966 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002967
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002968 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002969 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002970 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00002971 ExprResult ICE
2972 = VerifyIntegerConstantExpression(E, &Alignment,
2973 diag::err_aligned_attribute_argument_not_int,
2974 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00002975 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002976 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002977 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002978 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2979 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002980 return;
2981 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002982 if (isDeclSpec) {
2983 // We've already verified it's a power of 2, now let's make sure it's
2984 // 8192 or less.
2985 if (Alignment.getZExtValue() > 8192) {
2986 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
2987 << E->getSourceRange();
2988 return;
2989 }
2990 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002991
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002992 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
2993 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00002994}
2995
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00002996void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
2997 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00002998 // FIXME: Cache the number on the Attr object if non-dependent?
2999 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003000 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3001 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003002 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003003}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003004
Chandler Carruthd309c812011-07-01 23:49:16 +00003005/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003006/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003007///
Mike Stumpbf916502009-07-24 19:02:52 +00003008/// Despite what would be logical, the mode attribute is a decl attribute, not a
3009/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3010/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003011static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003012 // This attribute isn't documented, but glibc uses it. It changes
3013 // the width of an int or unsigned int to the specified size.
3014
3015 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003016 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003017 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003018
Chris Lattnerfbf13472008-06-27 22:18:37 +00003019
3020 IdentifierInfo *Name = Attr.getParameterName();
3021 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003022 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003023 return;
3024 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003025
Chris Lattner5f9e2722011-07-23 10:55:15 +00003026 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003027
3028 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003029 if (Str.startswith("__") && Str.endswith("__"))
3030 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003031
3032 unsigned DestWidth = 0;
3033 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003034 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003035 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003036 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003037 switch (Str[0]) {
3038 case 'Q': DestWidth = 8; break;
3039 case 'H': DestWidth = 16; break;
3040 case 'S': DestWidth = 32; break;
3041 case 'D': DestWidth = 64; break;
3042 case 'X': DestWidth = 96; break;
3043 case 'T': DestWidth = 128; break;
3044 }
3045 if (Str[1] == 'F') {
3046 IntegerMode = false;
3047 } else if (Str[1] == 'C') {
3048 IntegerMode = false;
3049 ComplexMode = true;
3050 } else if (Str[1] != 'I') {
3051 DestWidth = 0;
3052 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003053 break;
3054 case 4:
3055 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3056 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003057 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003058 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003059 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003060 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003061 break;
3062 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003063 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003064 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003065 break;
3066 }
3067
3068 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003069 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003070 OldTy = TD->getUnderlyingType();
3071 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3072 OldTy = VD->getType();
3073 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003074 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003075 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003076 return;
3077 }
Eli Friedman73397492009-03-03 06:41:03 +00003078
John McCall183700f2009-09-21 23:43:11 +00003079 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003080 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3081 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003082 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003083 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3084 } else if (ComplexMode) {
3085 if (!OldTy->isComplexType())
3086 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3087 } else {
3088 if (!OldTy->isFloatingType())
3089 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3090 }
3091
Mike Stump390b4cc2009-05-16 07:39:55 +00003092 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3093 // and friends, at least with glibc.
3094 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3095 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003096 // FIXME: Make sure floating-point mappings are accurate
3097 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003098 QualType NewTy;
3099 switch (DestWidth) {
3100 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003101 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003102 return;
3103 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003104 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003105 return;
3106 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003107 if (!IntegerMode) {
3108 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3109 return;
3110 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003111 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003112 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003113 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003114 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003115 break;
3116 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003117 if (!IntegerMode) {
3118 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3119 return;
3120 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003121 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003122 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003123 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003124 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003125 break;
3126 case 32:
3127 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003128 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003129 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003130 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003131 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003132 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003133 break;
3134 case 64:
3135 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003136 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003137 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003138 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003139 NewTy = S.Context.LongTy;
3140 else
3141 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003142 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003143 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003144 NewTy = S.Context.UnsignedLongTy;
3145 else
3146 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003147 break;
Eli Friedman73397492009-03-03 06:41:03 +00003148 case 96:
3149 NewTy = S.Context.LongDoubleTy;
3150 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003151 case 128:
3152 if (!IntegerMode) {
3153 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3154 return;
3155 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003156 if (OldTy->isSignedIntegerType())
3157 NewTy = S.Context.Int128Ty;
3158 else
3159 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003160 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003161 }
3162
Eli Friedman73397492009-03-03 06:41:03 +00003163 if (ComplexMode) {
3164 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003165 }
3166
3167 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003168 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003169 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003170 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003171 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003172 cast<ValueDecl>(D)->setType(NewTy);
3173}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003174
Chandler Carruth1b03c872011-07-02 00:01:44 +00003175static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003176 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003177 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003178 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003179
Chandler Carruth87c44602011-07-01 23:49:12 +00003180 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00003181 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003182 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00003183 return;
3184 }
Mike Stumpbf916502009-07-24 19:02:52 +00003185
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003186 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003187}
3188
Chandler Carruth1b03c872011-07-02 00:01:44 +00003189static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003190 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003191 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003192 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003193
Mike Stumpbf916502009-07-24 19:02:52 +00003194
Chandler Carruth87c44602011-07-01 23:49:12 +00003195 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003197 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003198 return;
3199 }
Mike Stumpbf916502009-07-24 19:02:52 +00003200
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003201 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003202}
3203
Chandler Carruth1b03c872011-07-02 00:01:44 +00003204static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3205 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003206 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003207 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003208 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003209
Chris Lattner7255a2d2010-06-22 00:03:40 +00003210
Chandler Carruth87c44602011-07-01 23:49:12 +00003211 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003212 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003213 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003214 return;
3215 }
3216
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003217 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003218 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003219}
3220
Chandler Carruth1b03c872011-07-02 00:01:44 +00003221static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003222 if (S.LangOpts.CUDA) {
3223 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003224 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3226 return;
3227 }
3228
Chandler Carruth87c44602011-07-01 23:49:12 +00003229 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003230 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003231 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003232 return;
3233 }
3234
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003235 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003236 } else {
3237 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3238 }
3239}
3240
Chandler Carruth1b03c872011-07-02 00:01:44 +00003241static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003242 if (S.LangOpts.CUDA) {
3243 // check the attribute arguments.
3244 if (Attr.getNumArgs() != 0) {
3245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3246 return;
3247 }
3248
Chandler Carruth87c44602011-07-01 23:49:12 +00003249 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003251 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003252 return;
3253 }
3254
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003255 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003256 } else {
3257 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3258 }
3259}
3260
Chandler Carruth1b03c872011-07-02 00:01:44 +00003261static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003262 if (S.LangOpts.CUDA) {
3263 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003264 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003265 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003266
Chandler Carruth87c44602011-07-01 23:49:12 +00003267 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003268 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003269 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003270 return;
3271 }
3272
Chandler Carruth87c44602011-07-01 23:49:12 +00003273 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003274 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003275 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003276 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3277 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3278 << FD->getType()
3279 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3280 "void");
3281 } else {
3282 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3283 << FD->getType();
3284 }
3285 return;
3286 }
3287
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003288 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003289 } else {
3290 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3291 }
3292}
3293
Chandler Carruth1b03c872011-07-02 00:01:44 +00003294static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003295 if (S.LangOpts.CUDA) {
3296 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003297 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003298 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003299
Peter Collingbourneced76712010-12-01 03:15:31 +00003300
Chandler Carruth87c44602011-07-01 23:49:12 +00003301 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003302 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003303 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003304 return;
3305 }
3306
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003307 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003308 } else {
3309 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3310 }
3311}
3312
Chandler Carruth1b03c872011-07-02 00:01:44 +00003313static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003314 if (S.LangOpts.CUDA) {
3315 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003316 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003317 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003318
Peter Collingbourneced76712010-12-01 03:15:31 +00003319
Chandler Carruth87c44602011-07-01 23:49:12 +00003320 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003322 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003323 return;
3324 }
3325
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003326 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003327 } else {
3328 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3329 }
3330}
3331
Chandler Carruth1b03c872011-07-02 00:01:44 +00003332static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003333 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003334 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003335 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003336
Chandler Carruth87c44602011-07-01 23:49:12 +00003337 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003338 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003339 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003340 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003341 return;
3342 }
Mike Stumpbf916502009-07-24 19:02:52 +00003343
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003344 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003345 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003346 return;
3347 }
Mike Stumpbf916502009-07-24 19:02:52 +00003348
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003349 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003350}
3351
Chandler Carruth1b03c872011-07-02 00:01:44 +00003352static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003353 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003354
Chandler Carruth87c44602011-07-01 23:49:12 +00003355 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003356 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3357 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003358 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003359 return;
3360
Chandler Carruth87c44602011-07-01 23:49:12 +00003361 if (!isa<ObjCMethodDecl>(D)) {
3362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3363 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003364 return;
3365 }
3366
Chandler Carruth87c44602011-07-01 23:49:12 +00003367 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003368 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003369 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003370 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003371 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003372 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003373 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003374 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003375 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003376 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003377 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003378 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003379 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003380 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003381 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003382 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003383 case AttributeList::AT_Pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003384 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003385 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003386 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003387 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003388 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003389 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003390 return;
3391 }
3392
Chris Lattner5f9e2722011-07-23 10:55:15 +00003393 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003394 PcsAttr::PCSType PCS;
3395 if (StrRef == "aapcs")
3396 PCS = PcsAttr::AAPCS;
3397 else if (StrRef == "aapcs-vfp")
3398 PCS = PcsAttr::AAPCS_VFP;
3399 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003400 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3401 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003402 return;
3403 }
3404
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003405 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003406 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003407 default:
3408 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003409 }
3410}
3411
Chandler Carruth1b03c872011-07-02 00:01:44 +00003412static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003413 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003414 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003415}
3416
John McCall711c52b2011-01-05 12:14:39 +00003417bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3418 if (attr.isInvalid())
3419 return true;
3420
Ted Kremenek831efae2011-04-15 05:49:29 +00003421 if ((attr.getNumArgs() != 0 &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003422 !(attr.getKind() == AttributeList::AT_Pcs && attr.getNumArgs() == 1)) ||
Ted Kremenek831efae2011-04-15 05:49:29 +00003423 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003424 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3425 attr.setInvalid();
3426 return true;
3427 }
3428
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003429 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3430 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003431 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003432 case AttributeList::AT_CDecl: CC = CC_C; break;
3433 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3434 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3435 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3436 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3437 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003438 Expr *Arg = attr.getArg(0);
3439 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003440 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003441 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3442 << "pcs" << 1;
3443 attr.setInvalid();
3444 return true;
3445 }
3446
Chris Lattner5f9e2722011-07-23 10:55:15 +00003447 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003448 if (StrRef == "aapcs") {
3449 CC = CC_AAPCS;
3450 break;
3451 } else if (StrRef == "aapcs-vfp") {
3452 CC = CC_AAPCS_VFP;
3453 break;
3454 }
3455 // FALLS THROUGH
3456 }
David Blaikie7530c032012-01-17 06:56:22 +00003457 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003458 }
3459
3460 return false;
3461}
3462
Chandler Carruth1b03c872011-07-02 00:01:44 +00003463static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003464 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003465
3466 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003467 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003468 return;
3469
Chandler Carruth87c44602011-07-01 23:49:12 +00003470 if (!isa<ObjCMethodDecl>(D)) {
3471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3472 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003473 return;
3474 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003475
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003476 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003477}
3478
3479/// Checks a regparm attribute, returning true if it is ill-formed and
3480/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003481bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3482 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003483 return true;
3484
Chandler Carruth87c44602011-07-01 23:49:12 +00003485 if (Attr.getNumArgs() != 1) {
3486 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3487 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003488 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003489 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003490
Chandler Carruth87c44602011-07-01 23:49:12 +00003491 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003492 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003493 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003494 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003495 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003496 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003497 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003498 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003499 }
3500
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003501 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003502 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003503 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003504 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003505 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003506 }
3507
John McCall711c52b2011-01-05 12:14:39 +00003508 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003509 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003510 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003511 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003512 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003513 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003514 }
3515
John McCall711c52b2011-01-05 12:14:39 +00003516 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003517}
3518
Chandler Carruth1b03c872011-07-02 00:01:44 +00003519static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003520 if (S.LangOpts.CUDA) {
3521 // check the attribute arguments.
3522 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003523 // FIXME: 0 is not okay.
3524 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003525 return;
3526 }
3527
Chandler Carruth87c44602011-07-01 23:49:12 +00003528 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003529 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003530 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003531 return;
3532 }
3533
3534 Expr *MaxThreadsExpr = Attr.getArg(0);
3535 llvm::APSInt MaxThreads(32);
3536 if (MaxThreadsExpr->isTypeDependent() ||
3537 MaxThreadsExpr->isValueDependent() ||
3538 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3540 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3541 return;
3542 }
3543
3544 llvm::APSInt MinBlocks(32);
3545 if (Attr.getNumArgs() > 1) {
3546 Expr *MinBlocksExpr = Attr.getArg(1);
3547 if (MinBlocksExpr->isTypeDependent() ||
3548 MinBlocksExpr->isValueDependent() ||
3549 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3550 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3551 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3552 return;
3553 }
3554 }
3555
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003556 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003557 MaxThreads.getZExtValue(),
3558 MinBlocks.getZExtValue()));
3559 } else {
3560 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3561 }
3562}
3563
Chris Lattner0744e5f2008-06-29 00:23:49 +00003564//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003565// Checker-specific attribute handlers.
3566//===----------------------------------------------------------------------===//
3567
John McCallc7ad3812011-01-25 03:31:58 +00003568static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003569 return type->isDependentType() ||
3570 type->isObjCObjectPointerType() ||
3571 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003572}
3573static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003574 return type->isDependentType() ||
3575 type->isPointerType() ||
3576 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003577}
3578
Chandler Carruth1b03c872011-07-02 00:01:44 +00003579static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003580 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003581 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003582 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003583 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003584 return;
3585 }
3586
3587 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003588 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003589 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3590 cf = false;
3591 } else {
3592 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3593 cf = true;
3594 }
3595
3596 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003597 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003598 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003599 return;
3600 }
3601
3602 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003603 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003604 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003605 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003606}
3607
Chandler Carruth1b03c872011-07-02 00:01:44 +00003608static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3609 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003610 if (!isa<ObjCMethodDecl>(D)) {
3611 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003612 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003613 return;
3614 }
3615
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003616 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003617}
3618
Chandler Carruth1b03c872011-07-02 00:01:44 +00003619static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3620 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003621
John McCallc7ad3812011-01-25 03:31:58 +00003622 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003623
Chandler Carruth87c44602011-07-01 23:49:12 +00003624 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003625 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003626 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003627 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003628 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003629 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003630 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003631 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003632 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003633 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003634 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003635 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003636 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003637 return;
3638 }
Mike Stumpbf916502009-07-24 19:02:52 +00003639
John McCallc7ad3812011-01-25 03:31:58 +00003640 bool typeOK;
3641 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003642 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003643 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003644 case AttributeList::AT_NSReturnsAutoreleased:
3645 case AttributeList::AT_NSReturnsRetained:
3646 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003647 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3648 cf = false;
3649 break;
3650
Sean Hunt8e083e72012-06-19 23:57:03 +00003651 case AttributeList::AT_CFReturnsRetained:
3652 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003653 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3654 cf = true;
3655 break;
3656 }
3657
3658 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003659 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003660 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003661 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003662 }
Mike Stumpbf916502009-07-24 19:02:52 +00003663
Chandler Carruth87c44602011-07-01 23:49:12 +00003664 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003665 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003666 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003667 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003668 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003669 S.Context));
3670 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003671 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003672 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003673 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003674 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003675 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003676 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003677 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003678 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003679 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003680 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003681 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003682 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003683 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003684 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003685 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003686 return;
3687 };
3688}
3689
John McCalldc7c5ad2011-07-22 08:53:00 +00003690static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3691 const AttributeList &attr) {
3692 SourceLocation loc = attr.getLoc();
3693
3694 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3695
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003696 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003697 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003698 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003699 return;
3700 }
3701
3702 // Check that the method returns a normal pointer.
3703 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003704
3705 if (!resultType->isReferenceType() &&
3706 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003707 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3708 << SourceRange(loc)
3709 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3710
3711 // Drop the attribute.
3712 return;
3713 }
3714
3715 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003716 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003717}
3718
John McCall8dfac0b2011-09-30 05:12:12 +00003719/// Handle cf_audited_transfer and cf_unknown_transfer.
3720static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3721 if (!isa<FunctionDecl>(D)) {
3722 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003723 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003724 return;
3725 }
3726
Sean Hunt8e083e72012-06-19 23:57:03 +00003727 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00003728
3729 // Check whether there's a conflicting attribute already present.
3730 Attr *Existing;
3731 if (IsAudited) {
3732 Existing = D->getAttr<CFUnknownTransferAttr>();
3733 } else {
3734 Existing = D->getAttr<CFAuditedTransferAttr>();
3735 }
3736 if (Existing) {
3737 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3738 << A.getName()
3739 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3740 << A.getRange() << Existing->getRange();
3741 return;
3742 }
3743
3744 // All clear; add the attribute.
3745 if (IsAudited) {
3746 D->addAttr(
3747 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3748 } else {
3749 D->addAttr(
3750 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3751 }
3752}
3753
John McCallfe98da02011-09-29 07:17:38 +00003754static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3755 const AttributeList &Attr) {
3756 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3757 if (!RD || RD->isUnion()) {
3758 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003759 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003760 }
3761
3762 IdentifierInfo *ParmName = Attr.getParameterName();
3763
3764 // In Objective-C, verify that the type names an Objective-C type.
3765 // We don't want to check this outside of ObjC because people sometimes
3766 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003767 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003768 // Check for an existing type with this name.
3769 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3770 Sema::LookupOrdinaryName);
3771 if (S.LookupName(R, Sc)) {
3772 NamedDecl *Target = R.getFoundDecl();
3773 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3774 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3775 S.Diag(Target->getLocStart(), diag::note_declared_at);
3776 }
3777 }
3778 }
3779
3780 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3781 ParmName));
3782}
3783
Chandler Carruth1b03c872011-07-02 00:01:44 +00003784static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3785 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003786 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003787
Chandler Carruth87c44602011-07-01 23:49:12 +00003788 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003789 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003790}
3791
Chandler Carruth1b03c872011-07-02 00:01:44 +00003792static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3793 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003794 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003795 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003796 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003797 return;
3798 }
3799
Chandler Carruth87c44602011-07-01 23:49:12 +00003800 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003801 QualType type = vd->getType();
3802
3803 if (!type->isDependentType() &&
3804 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003805 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003806 << type;
3807 return;
3808 }
3809
3810 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3811
3812 // If we have no lifetime yet, check the lifetime we're presumably
3813 // going to infer.
3814 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3815 lifetime = type->getObjCARCImplicitLifetime();
3816
3817 switch (lifetime) {
3818 case Qualifiers::OCL_None:
3819 assert(type->isDependentType() &&
3820 "didn't infer lifetime for non-dependent type?");
3821 break;
3822
3823 case Qualifiers::OCL_Weak: // meaningful
3824 case Qualifiers::OCL_Strong: // meaningful
3825 break;
3826
3827 case Qualifiers::OCL_ExplicitNone:
3828 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003829 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003830 << (lifetime == Qualifiers::OCL_Autoreleasing);
3831 break;
3832 }
3833
Chandler Carruth87c44602011-07-01 23:49:12 +00003834 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003835 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003836}
3837
Francois Pichet11542142010-12-19 06:50:37 +00003838//===----------------------------------------------------------------------===//
3839// Microsoft specific attribute handlers.
3840//===----------------------------------------------------------------------===//
3841
Chandler Carruth1b03c872011-07-02 00:01:44 +00003842static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003843 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003844 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003845 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003846 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003847
Francois Pichet11542142010-12-19 06:50:37 +00003848 Expr *Arg = Attr.getArg(0);
3849 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003850 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003851 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3852 << "uuid" << 1;
3853 return;
3854 }
3855
Chris Lattner5f9e2722011-07-23 10:55:15 +00003856 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003857
3858 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3859 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003860
Francois Pichetd3d3be92010-12-20 01:41:49 +00003861 // Validate GUID length.
3862 if (IsCurly && StrRef.size() != 38) {
3863 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3864 return;
3865 }
3866 if (!IsCurly && StrRef.size() != 36) {
3867 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3868 return;
3869 }
3870
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003871 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003872 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003873 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003874 if (IsCurly) // Skip the optional '{'
3875 ++I;
3876
3877 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003878 if (i == 8 || i == 13 || i == 18 || i == 23) {
3879 if (*I != '-') {
3880 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3881 return;
3882 }
3883 } else if (!isxdigit(*I)) {
3884 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3885 return;
3886 }
3887 I++;
3888 }
Francois Pichet11542142010-12-19 06:50:37 +00003889
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003890 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003891 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003892 } else
Francois Pichet11542142010-12-19 06:50:37 +00003893 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003894}
3895
John McCallc052dbb2012-05-22 21:28:12 +00003896static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3897 if (S.LangOpts.MicrosoftExt) {
3898 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00003899 if (Kind == AttributeList::AT_SingleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00003900 D->addAttr(
3901 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00003902 else if (Kind == AttributeList::AT_MultipleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00003903 D->addAttr(
3904 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00003905 else if (Kind == AttributeList::AT_VirtualInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00003906 D->addAttr(
3907 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
3908 } else
3909 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3910}
3911
3912static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3913 if (S.LangOpts.MicrosoftExt) {
3914 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00003915 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00003916 D->addAttr(
3917 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00003918 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00003919 D->addAttr(
3920 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00003921 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00003922 D->addAttr(
3923 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
3924 } else
3925 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3926}
3927
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00003928static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3929 if (S.LangOpts.MicrosoftExt)
3930 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
3931 else
3932 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3933}
3934
Ted Kremenekb71368d2009-05-09 02:44:38 +00003935//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003936// Top Level Sema Entry Points
3937//===----------------------------------------------------------------------===//
3938
Chandler Carruth1b03c872011-07-02 00:01:44 +00003939static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3940 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003941 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003942 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
3943 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
3944 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003945 default:
3946 break;
3947 }
3948}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003949
Chandler Carruth1b03c872011-07-02 00:01:44 +00003950static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3951 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003952 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003953 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3954 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3955 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003956 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003957 case AttributeList::AT_AddressSpace:
3958 case AttributeList::AT_OpenCLImageAccess:
3959 case AttributeList::AT_ObjCGC:
3960 case AttributeList::AT_VectorSize:
3961 case AttributeList::AT_NeonVectorType:
3962 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00003963 // Ignore these, these are type attributes, handled by
3964 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003965 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003966 case AttributeList::AT_CUDADevice:
3967 case AttributeList::AT_CUDAHost:
3968 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00003969 // Ignore, this is a non-inheritable attribute, handled
3970 // by ProcessNonInheritableDeclAttr.
3971 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003972 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3973 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3974 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3975 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003976 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003977 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003978 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00003979 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003980 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3981 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3982 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003983 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003984 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
3985 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
3986 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
3987 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00003988 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
3989 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003990 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3991 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003992 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003993 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003994 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3995 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3996 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
3997 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3998 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003999 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004000 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004001 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4002 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4003 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4004 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4005 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004006 case AttributeList::AT_ownership_returns:
4007 case AttributeList::AT_ownership_takes:
4008 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004009 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004010 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4011 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4012 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4013 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4014 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4015 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4016 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004017
Sean Hunt8e083e72012-06-19 23:57:03 +00004018 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004019 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004020 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004021 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004022
Sean Hunt8e083e72012-06-19 23:57:03 +00004023 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004024 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4025
Sean Hunt8e083e72012-06-19 23:57:03 +00004026 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004027 handleNSBridgedAttr(S, scope, D, Attr); break;
4028
Sean Hunt8e083e72012-06-19 23:57:03 +00004029 case AttributeList::AT_CFAuditedTransfer:
4030 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004031 handleCFTransferAttr(S, D, Attr); break;
4032
Ted Kremenekb71368d2009-05-09 02:44:38 +00004033 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004034 case AttributeList::AT_CFConsumed:
4035 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4036 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004037 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004038
Sean Hunt8e083e72012-06-19 23:57:03 +00004039 case AttributeList::AT_NSReturnsAutoreleased:
4040 case AttributeList::AT_NSReturnsNotRetained:
4041 case AttributeList::AT_CFReturnsNotRetained:
4042 case AttributeList::AT_NSReturnsRetained:
4043 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004044 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004045
Sean Hunt8e083e72012-06-19 23:57:03 +00004046 case AttributeList::AT_ReqdWorkGroupSize:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004047 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004048
Sean Hunt8e083e72012-06-19 23:57:03 +00004049 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004050 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004051
Sean Hunt8e083e72012-06-19 23:57:03 +00004052 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4053 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4054 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004055 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4056 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004057 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004058 handleArcWeakrefUnavailableAttr (S, D, Attr);
4059 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004060 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004061 handleObjCRootClassAttr(S, D, Attr);
4062 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004063 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004064 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004065 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004066 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4067 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004068 handleReturnsTwiceAttr(S, D, Attr);
4069 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004070 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4071 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4072 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004073 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004074 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4075 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4076 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4077 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004078 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004079 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004080 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004081 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004082 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004083 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004084 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004085 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004086 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4087 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4088 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4089 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4090 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4091 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4092 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4093 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4094 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004095 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004096 // Just ignore
4097 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004098 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004099 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004100 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004101 case AttributeList::AT_StdCall:
4102 case AttributeList::AT_CDecl:
4103 case AttributeList::AT_FastCall:
4104 case AttributeList::AT_ThisCall:
4105 case AttributeList::AT_Pascal:
4106 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004107 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004108 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004109 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004110 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004111 break;
John McCallc052dbb2012-05-22 21:28:12 +00004112
4113 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004114 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004115 handleMsStructAttr(S, D, Attr);
4116 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004117 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004118 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004119 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004120 case AttributeList::AT_SingleInheritance:
4121 case AttributeList::AT_MultipleInheritance:
4122 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004123 handleInheritanceAttr(S, D, Attr);
4124 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004125 case AttributeList::AT_Win64:
4126 case AttributeList::AT_Ptr32:
4127 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004128 handlePortabilityAttr(S, D, Attr);
4129 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004130 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004131 handleForceInlineAttr(S, D, Attr);
4132 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004133
4134 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004135 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004136 handleGuardedVarAttr(S, D, Attr);
4137 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004138 case AttributeList::AT_PtGuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004139 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
4140 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004141 case AttributeList::AT_ScopedLockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004142 handleLockableAttr(S, D, Attr, /*scoped = */true);
4143 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004144 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004145 handleNoAddressSafetyAttr(S, D, Attr);
4146 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004147 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004148 handleNoThreadSafetyAttr(S, D, Attr);
4149 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004150 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004151 handleLockableAttr(S, D, Attr);
4152 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004153 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004154 handleGuardedByAttr(S, D, Attr);
4155 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004156 case AttributeList::AT_PtGuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004157 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
4158 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004159 case AttributeList::AT_ExclusiveLockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004160 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
4161 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004162 case AttributeList::AT_ExclusiveLocksRequired:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004163 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
4164 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004165 case AttributeList::AT_ExclusiveTrylockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004166 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
4167 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004168 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004169 handleLockReturnedAttr(S, D, Attr);
4170 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004171 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004172 handleLocksExcludedAttr(S, D, Attr);
4173 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004174 case AttributeList::AT_SharedLockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004175 handleLockFunAttr(S, D, Attr);
4176 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004177 case AttributeList::AT_SharedLocksRequired:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004178 handleLocksRequiredAttr(S, D, Attr);
4179 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004180 case AttributeList::AT_SharedTrylockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004181 handleTrylockFunAttr(S, D, Attr);
4182 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004183 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004184 handleUnlockFunAttr(S, D, Attr);
4185 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004186 case AttributeList::AT_AcquiredBefore:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004187 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
4188 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004189 case AttributeList::AT_AcquiredAfter:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004190 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
4191 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004192
Chris Lattner803d0802008-06-29 00:43:07 +00004193 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004194 // Ask target about the attribute.
4195 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4196 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004197 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4198 diag::warn_unhandled_ms_attribute_ignored :
4199 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004200 break;
4201 }
4202}
4203
Peter Collingbourne60700392011-01-21 02:08:45 +00004204/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4205/// the attribute applies to decls. If the attribute is a type attribute, just
4206/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4207/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004208static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4209 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004210 bool NonInheritable, bool Inheritable) {
4211 if (Attr.isInvalid())
4212 return;
4213
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004214 // Type attributes are still treated as declaration attributes by
4215 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4216 // want to process them, however, because we will simply warn about ignoring
4217 // them. So instead, we will bail out early.
4218 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004219 return;
4220
4221 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004222 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004223
4224 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004225 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004226}
4227
Chris Lattner803d0802008-06-29 00:43:07 +00004228/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4229/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004230void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004231 const AttributeList *AttrList,
4232 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004233 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004234 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004235 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004236
4237 // GCC accepts
4238 // static int a9 __attribute__((weakref));
4239 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004240 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004241 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004242 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004243 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004244 }
4245}
4246
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004247// Annotation attributes are the only attributes allowed after an access
4248// specifier.
4249bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4250 const AttributeList *AttrList) {
4251 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004252 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004253 handleAnnotateAttr(*this, ASDecl, *l);
4254 } else {
4255 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4256 return true;
4257 }
4258 }
4259
4260 return false;
4261}
4262
John McCalle82247a2011-10-01 05:17:03 +00004263/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4264/// contains any decl attributes that we should warn about.
4265static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4266 for ( ; A; A = A->getNext()) {
4267 // Only warn if the attribute is an unignored, non-type attribute.
4268 if (A->isUsedAsTypeAttr()) continue;
4269 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4270
4271 if (A->getKind() == AttributeList::UnknownAttribute) {
4272 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4273 << A->getName() << A->getRange();
4274 } else {
4275 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4276 << A->getName() << A->getRange();
4277 }
4278 }
4279}
4280
4281/// checkUnusedDeclAttributes - Given a declarator which is not being
4282/// used to build a declaration, complain about any decl attributes
4283/// which might be lying around on it.
4284void Sema::checkUnusedDeclAttributes(Declarator &D) {
4285 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4286 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4287 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4288 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4289}
4290
Ryan Flynne25ff832009-07-30 03:15:39 +00004291/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004292/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004293NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4294 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004295 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004296 NamedDecl *NewD = 0;
4297 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004298 FunctionDecl *NewFD;
4299 // FIXME: Missing call to CheckFunctionDeclaration().
4300 // FIXME: Mangling?
4301 // FIXME: Is the qualifier info correct?
4302 // FIXME: Is the DeclContext correct?
4303 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4304 Loc, Loc, DeclarationName(II),
4305 FD->getType(), FD->getTypeSourceInfo(),
4306 SC_None, SC_None,
4307 false/*isInlineSpecified*/,
4308 FD->hasPrototype(),
4309 false/*isConstexprSpecified*/);
4310 NewD = NewFD;
4311
4312 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004313 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004314
4315 // Fake up parameter variables; they are declared as if this were
4316 // a typedef.
4317 QualType FDTy = FD->getType();
4318 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4319 SmallVector<ParmVarDecl*, 16> Params;
4320 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4321 AE = FT->arg_type_end(); AI != AE; ++AI) {
4322 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4323 Param->setScopeInfo(0, Params.size());
4324 Params.push_back(Param);
4325 }
David Blaikie4278c652011-09-21 18:16:56 +00004326 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004327 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004328 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4329 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004330 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004331 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004332 VD->getStorageClass(),
4333 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004334 if (VD->getQualifier()) {
4335 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004336 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004337 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004338 }
4339 return NewD;
4340}
4341
James Dennett1dfbd922012-06-14 21:40:34 +00004342/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004343/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004344void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004345 if (W.getUsed()) return; // only do this once
4346 W.setUsed(true);
4347 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4348 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004349 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004350 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4351 NDId->getName()));
4352 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004353 WeakTopLevelDecl.push_back(NewD);
4354 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4355 // to insert Decl at TU scope, sorry.
4356 DeclContext *SavedContext = CurContext;
4357 CurContext = Context.getTranslationUnitDecl();
4358 PushOnScopeChains(NewD, S);
4359 CurContext = SavedContext;
4360 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004361 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004362 }
4363}
4364
Chris Lattner0744e5f2008-06-29 00:23:49 +00004365/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4366/// it, apply them to D. This is a bit tricky because PD can have attributes
4367/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004368void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4369 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004370 // It's valid to "forward-declare" #pragma weak, in which case we
4371 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004372 if (Inheritable) {
4373 LoadExternalWeakUndeclaredIdentifiers();
4374 if (!WeakUndeclaredIdentifiers.empty()) {
4375 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4376 if (IdentifierInfo *Id = ND->getIdentifier()) {
4377 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4378 = WeakUndeclaredIdentifiers.find(Id);
4379 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4380 WeakInfo W = I->second;
4381 DeclApplyPragmaWeak(S, ND, W);
4382 WeakUndeclaredIdentifiers[Id] = W;
4383 }
John McCalld4aff0e2010-10-27 00:59:00 +00004384 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004385 }
4386 }
4387 }
4388
Chris Lattner0744e5f2008-06-29 00:23:49 +00004389 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004390 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004391 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004392
Chris Lattner0744e5f2008-06-29 00:23:49 +00004393 // Walk the declarator structure, applying decl attributes that were in a type
4394 // position to the decl itself. This handles cases like:
4395 // int *__attr__(x)** D;
4396 // when X is a decl attribute.
4397 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4398 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004399 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004400
Chris Lattner0744e5f2008-06-29 00:23:49 +00004401 // Finally, apply any attributes on the decl itself.
4402 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004403 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004404}
John McCall54abf7d2009-11-04 02:18:39 +00004405
John McCallf85e1932011-06-15 23:02:42 +00004406/// Is the given declaration allowed to use a forbidden type?
4407static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4408 // Private ivars are always okay. Unfortunately, people don't
4409 // always properly make their ivars private, even in system headers.
4410 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004411 // Function declarations in sys headers will be marked unavailable.
4412 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4413 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004414 return false;
4415
4416 // Require it to be declared in a system header.
4417 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4418}
4419
4420/// Handle a delayed forbidden-type diagnostic.
4421static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4422 Decl *decl) {
4423 if (decl && isForbiddenTypeAllowed(S, decl)) {
4424 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4425 "this system declaration uses an unsupported type"));
4426 return;
4427 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004428 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004429 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004430 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004431 // kind of forbidden type messages on unavailable functions.
4432 if (FD->hasAttr<UnavailableAttr>() &&
4433 diag.getForbiddenTypeDiagnostic() ==
4434 diag::err_arc_array_param_no_ownership) {
4435 diag.Triggered = true;
4436 return;
4437 }
4438 }
John McCallf85e1932011-06-15 23:02:42 +00004439
4440 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4441 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4442 diag.Triggered = true;
4443}
4444
John McCall92576642012-05-07 06:16:41 +00004445void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4446 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004447 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004448 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004449
John McCall92576642012-05-07 06:16:41 +00004450 // When delaying diagnostics to run in the context of a parsed
4451 // declaration, we only want to actually emit anything if parsing
4452 // succeeds.
4453 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004454
John McCall92576642012-05-07 06:16:41 +00004455 // We emit all the active diagnostics in this pool or any of its
4456 // parents. In general, we'll get one pool for the decl spec
4457 // and a child pool for each declarator; in a decl group like:
4458 // deprecated_typedef foo, *bar, baz();
4459 // only the declarator pops will be passed decls. This is correct;
4460 // we really do need to consider delayed diagnostics from the decl spec
4461 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004462 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004463 do {
John McCall13489672012-05-07 06:16:58 +00004464 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004465 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4466 // This const_cast is a bit lame. Really, Triggered should be mutable.
4467 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004468 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004469 continue;
4470
John McCalleee1d542011-02-14 07:13:47 +00004471 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004472 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004473 // Don't bother giving deprecation diagnostics if the decl is invalid.
4474 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004475 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004476 break;
4477
4478 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004479 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004480 break;
John McCallf85e1932011-06-15 23:02:42 +00004481
4482 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004483 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004484 break;
John McCall2f514482010-01-27 03:50:35 +00004485 }
4486 }
John McCall92576642012-05-07 06:16:41 +00004487 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004488}
4489
John McCall13489672012-05-07 06:16:58 +00004490/// Given a set of delayed diagnostics, re-emit them as if they had
4491/// been delayed in the current context instead of in the given pool.
4492/// Essentially, this just moves them to the current pool.
4493void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4494 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4495 assert(curPool && "re-emitting in undelayed context not supported");
4496 curPool->steal(pool);
4497}
4498
John McCall54abf7d2009-11-04 02:18:39 +00004499static bool isDeclDeprecated(Decl *D) {
4500 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004501 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004502 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004503 // A category implicitly has the availability of the interface.
4504 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4505 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004506 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4507 return false;
4508}
4509
John McCall9c3087b2010-08-26 02:13:20 +00004510void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004511 Decl *Ctx) {
4512 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004513 return;
4514
John McCall2f514482010-01-27 03:50:35 +00004515 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004516 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004517 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004518 << DD.getDeprecationDecl()->getDeclName()
4519 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004520 else if (DD.getUnknownObjCClass()) {
4521 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4522 << DD.getDeprecationDecl()->getDeclName();
4523 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4524 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004525 else
4526 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004527 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004528}
4529
Chris Lattner5f9e2722011-07-23 10:55:15 +00004530void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004531 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004532 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004533 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004534 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004535 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4536 UnknownObjCClass,
4537 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004538 return;
4539 }
4540
4541 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004542 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004543 return;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004544 if (!Message.empty()) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004545 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4546 << Message;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004547 Diag(D->getLocation(),
4548 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4549 : diag::note_previous_decl) << D->getDeclName();
4550 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004551 else {
Fariborz Jahanian350e9562012-05-27 16:59:48 +00004552 if (!UnknownObjCClass) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004553 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian350e9562012-05-27 16:59:48 +00004554 Diag(D->getLocation(),
4555 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4556 : diag::note_previous_decl) << D->getDeclName();
4557 }
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004558 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004559 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004560 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4561 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004562 }
John McCall54abf7d2009-11-04 02:18:39 +00004563}