blob: aef87c62f4f72e5a5befa23e8286b1f7a42fb783 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000028using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030
John McCall883cc2c2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000046 ExpectedStruct,
47 ExpectedTLSVar
John McCall883cc2c2011-03-02 12:29:23 +000048};
49
Chris Lattnere5c5ee12008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruth87c44602011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000065
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070
John McCall183700f2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000072}
73
Daniel Dunbar35682492008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopesd20254f2009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000087 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000088}
89
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000102}
103
John McCall711c52b2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000110}
111
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000118 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000133}
134
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148}
149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000156 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000175
John McCall506b57e2010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000211
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221 return true;
222}
223
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000224/// \brief Check if IdxExpr is a valid argument index for a function or
225/// instance method D. May output an error.
226///
227/// \returns true if IdxExpr is a valid index.
228static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
229 StringRef AttrName,
230 SourceLocation AttrLoc,
231 unsigned AttrArgNum,
232 const Expr *IdxExpr,
233 uint64_t &Idx)
234{
235 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
236
237 // In C++ the implicit 'this' function parameter also counts.
238 // Parameters are counted from one.
239 const bool HasImplicitThisParam = isInstanceMethod(D);
240 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
241 const unsigned FirstIdx = 1;
242
243 llvm::APSInt IdxInt;
244 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
245 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
246 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
247 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
248 return false;
249 }
250
251 Idx = IdxInt.getLimitedValue();
252 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
253 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
254 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
255 return false;
256 }
257 Idx--; // Convert to zero-based.
258 if (HasImplicitThisParam) {
259 if (Idx == 0) {
260 S.Diag(AttrLoc,
261 diag::err_attribute_invalid_implicit_this_argument)
262 << AttrName << IdxExpr->getSourceRange();
263 return false;
264 }
265 --Idx;
266 }
267
268 return true;
269}
270
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000271///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000272/// \brief Check if passed in Decl is a field or potentially shared global var
273/// \return true if the Decl is a field or potentially shared global variable
274///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000275static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000276 if (isa<FieldDecl>(D))
277 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000278 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000279 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
280
281 return false;
282}
283
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000284/// \brief Check if the passed-in expression is of type int or bool.
285static bool isIntOrBool(Expr *Exp) {
286 QualType QT = Exp->getType();
287 return QT->isBooleanType() || QT->isIntegerType();
288}
289
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000290
291// Check to see if the type is a smart pointer of some kind. We assume
292// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000293static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
294 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
295 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
296 if (Res1.first == Res1.second)
297 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000298
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000299 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
300 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
301 if (Res2.first == Res2.second)
302 return false;
303
304 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000305}
306
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000307/// \brief Check if passed in Decl is a pointer type.
308/// Note that this function may produce an error message.
309/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000310static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
311 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000312 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000313 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000314 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000315 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000316
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000317 if (const RecordType *RT = QT->getAs<RecordType>()) {
318 // If it's an incomplete type, it could be a smart pointer; skip it.
319 // (We don't want to force template instantiation if we can avoid it,
320 // since that would alter the order in which templates are instantiated.)
321 if (RT->isIncompleteType())
322 return true;
323
324 if (threadSafetyCheckIsSmartPointer(S, RT))
325 return true;
326 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000327
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000328 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000329 << Attr.getName()->getName() << QT;
330 } else {
331 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
332 << Attr.getName();
333 }
334 return false;
335}
336
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000337/// \brief Checks that the passed in QualType either is of RecordType or points
338/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000339static const RecordType *getRecordType(QualType QT) {
340 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000341 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000342
343 // Now check if we point to record type.
344 if (const PointerType *PT = QT->getAs<PointerType>())
345 return PT->getPointeeType()->getAs<RecordType>();
346
347 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000348}
349
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000350
Jordy Rosefad5de92012-05-08 03:27:22 +0000351static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
352 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000353 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
354 if (RT->getDecl()->getAttr<LockableAttr>())
355 return true;
356 return false;
357}
358
359
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000360/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000361/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000362static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
363 QualType Ty) {
364 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000365
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000366 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000367 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000368 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000369 << Attr.getName() << Ty.getAsString();
370 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000371 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000372
Michael Hanf1aae3b2012-08-03 17:40:43 +0000373 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000374 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000375 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000376
377 // Allow smart pointers to be used as lockable objects.
378 // FIXME -- Check the type that the smart pointer points to.
379 if (threadSafetyCheckIsSmartPointer(S, RT))
380 return;
381
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000382 // Check if the type is lockable.
383 RecordDecl *RD = RT->getDecl();
384 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000385 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000386
387 // Else check if any base classes are lockable.
388 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
389 CXXBasePaths BPaths(false, false);
390 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
391 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000392 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000393
394 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
395 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000396}
397
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000398/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000399/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000400/// \param Sidx The attribute argument index to start checking with.
401/// \param ParamIdxOk Whether an argument can be indexing into a function
402/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000403static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000404 const AttributeList &Attr,
405 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000406 int Sidx = 0,
407 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000408 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000409 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000410
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000411 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000413 Args.push_back(ArgExp);
414 continue;
415 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000416
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000417 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000418 if (StrLit->getLength() == 0 ||
419 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000420 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000421 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000422 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000423 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000424 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000425
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000426 // We allow constant strings to be used as a placeholder for expressions
427 // that are not valid C++ syntax, but warn that they are ignored.
428 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
429 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000430 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000431 continue;
432 }
433
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000434 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000435
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 // A pointer to member expression of the form &MyClass::mu is treated
437 // specially -- we need to look at the type of the member.
438 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
439 if (UOp->getOpcode() == UO_AddrOf)
440 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
441 if (DRE->getDecl()->isCXXInstanceMember())
442 ArgTy = DRE->getDecl()->getType();
443
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000444 // First see if we can just cast to record type, or point to record type.
445 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000446
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447 // Now check if we index into a record type function param.
448 if(!RT && ParamIdxOk) {
449 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000450 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
451 if(FD && IL) {
452 unsigned int NumParams = FD->getNumParams();
453 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000454 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
455 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
456 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
458 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000459 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 }
463 }
464
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000465 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000466
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000469}
470
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000471//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000472// Attribute Implementations
473//===----------------------------------------------------------------------===//
474
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000475// FIXME: All this manual attribute parsing code is gross. At the
476// least add some helper functions to check most argument patterns (#
477// and types of args).
478
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000479enum ThreadAttributeDeclKind {
480 ThreadExpectedFieldOrGlobalVar,
481 ThreadExpectedFunctionOrMethod,
482 ThreadExpectedClassOrStruct
483};
484
Michael Hanf1aae3b2012-08-03 17:40:43 +0000485static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000486 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000487 assert(!Attr.isInvalid());
488
489 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000490 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000491
492 // D must be either a member field or global (potentially shared) variable.
493 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000494 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
495 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000496 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000497 }
498
Michael Handc691572012-07-23 18:48:41 +0000499 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500}
501
Michael Handc691572012-07-23 18:48:41 +0000502static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
503 if (!checkGuardedVarAttrCommon(S, D, Attr))
504 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000505
Michael Handc691572012-07-23 18:48:41 +0000506 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
507}
508
Michael Hanf1aae3b2012-08-03 17:40:43 +0000509static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000510 const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
513
514 if (!threadSafetyCheckIsPointer(S, D, Attr))
515 return;
516
517 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
518}
519
Michael Hanf1aae3b2012-08-03 17:40:43 +0000520static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
521 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000522 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000523 assert(!Attr.isInvalid());
524
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000525 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000526 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000527
528 // D must be either a member field or global (potentially shared) variable.
529 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000530 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
531 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000532 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000533 }
534
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000535 SmallVector<Expr*, 1> Args;
536 // check that all arguments are lockable objects
537 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
538 unsigned Size = Args.size();
539 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000540 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000541
Michael Handc691572012-07-23 18:48:41 +0000542 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000543
Michael Handc691572012-07-23 18:48:41 +0000544 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000545}
546
Michael Handc691572012-07-23 18:48:41 +0000547static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
548 Expr *Arg = 0;
549 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
550 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000551
Michael Handc691572012-07-23 18:48:41 +0000552 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
553}
554
Michael Hanf1aae3b2012-08-03 17:40:43 +0000555static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000556 const AttributeList &Attr) {
557 Expr *Arg = 0;
558 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
559 return;
560
561 if (!threadSafetyCheckIsPointer(S, D, Attr))
562 return;
563
564 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
565 S.Context, Arg));
566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000570 assert(!Attr.isInvalid());
571
572 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000573 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000574
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000575 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000576 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000577 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
578 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000579 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000580 }
581
Michael Handc691572012-07-23 18:48:41 +0000582 return true;
583}
584
585static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
586 if (!checkLockableAttrCommon(S, D, Attr))
587 return;
588
589 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
590}
591
Michael Hanf1aae3b2012-08-03 17:40:43 +0000592static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000593 const AttributeList &Attr) {
594 if (!checkLockableAttrCommon(S, D, Attr))
595 return;
596
597 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000598}
599
600static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
601 const AttributeList &Attr) {
602 assert(!Attr.isInvalid());
603
604 if (!checkAttributeNumArgs(S, Attr, 0))
605 return;
606
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000607 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000608 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
609 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000610 return;
611 }
612
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000613 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000614 S.Context));
615}
616
Kostya Serebryany71efba02012-01-24 19:25:38 +0000617static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000618 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000619 assert(!Attr.isInvalid());
620
621 if (!checkAttributeNumArgs(S, Attr, 0))
622 return;
623
624 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
626 << Attr.getName() << ExpectedFunctionOrMethod;
627 return;
628 }
629
630 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000631 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000632}
633
Michael Hanf1aae3b2012-08-03 17:40:43 +0000634static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
635 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000636 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000637 assert(!Attr.isInvalid());
638
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000639 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000640 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000641
642 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000643 ValueDecl *VD = dyn_cast<ValueDecl>(D);
644 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000645 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
646 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000647 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000648 }
649
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000650 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000651 QualType QT = VD->getType();
652 if (!QT->isDependentType()) {
653 const RecordType *RT = getRecordType(QT);
654 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000655 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000656 << Attr.getName();
657 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000658 }
659 }
660
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000661 // Check that all arguments are lockable objects.
662 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000663 if (Args.size() == 0)
664 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000665
Michael Handc691572012-07-23 18:48:41 +0000666 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000667}
668
Michael Hanf1aae3b2012-08-03 17:40:43 +0000669static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000670 const AttributeList &Attr) {
671 SmallVector<Expr*, 1> Args;
672 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
673 return;
674
675 Expr **StartArg = &Args[0];
676 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000677 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000678}
679
Michael Hanf1aae3b2012-08-03 17:40:43 +0000680static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000681 const AttributeList &Attr) {
682 SmallVector<Expr*, 1> Args;
683 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
684 return;
685
686 Expr **StartArg = &Args[0];
687 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000688 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000689}
690
Michael Hanf1aae3b2012-08-03 17:40:43 +0000691static bool checkLockFunAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000693 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000694 assert(!Attr.isInvalid());
695
696 // zero or more arguments ok
697
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 // check that the attribute is applied to a function
699 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000700 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
701 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000703 }
704
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000706 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000707
Michael Handc691572012-07-23 18:48:41 +0000708 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000709}
710
Michael Hanf1aae3b2012-08-03 17:40:43 +0000711static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000712 const AttributeList &Attr) {
713 SmallVector<Expr*, 1> Args;
714 if (!checkLockFunAttrCommon(S, D, Attr, Args))
715 return;
716
717 unsigned Size = Args.size();
718 Expr **StartArg = Size == 0 ? 0 : &Args[0];
719 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000721 StartArg, Size));
722}
723
Michael Hanf1aae3b2012-08-03 17:40:43 +0000724static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkLockFunAttrCommon(S, D, Attr, Args))
728 return;
729
730 unsigned Size = Args.size();
731 Expr **StartArg = Size == 0 ? 0 : &Args[0];
732 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000733 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000734 StartArg, Size));
735}
736
Michael Hanf1aae3b2012-08-03 17:40:43 +0000737static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
738 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000739 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000740 assert(!Attr.isInvalid());
741
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000742 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000743 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000744
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000745 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000746 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
747 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000749 }
750
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000751 if (!isIntOrBool(Attr.getArg(0))) {
752 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000753 << Attr.getName();
754 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000755 }
756
757 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000758 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000759
Michael Handc691572012-07-23 18:48:41 +0000760 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761}
762
Michael Hanf1aae3b2012-08-03 17:40:43 +0000763static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000764 const AttributeList &Attr) {
765 SmallVector<Expr*, 2> Args;
766 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
767 return;
768
769 unsigned Size = Args.size();
770 Expr **StartArg = Size == 0 ? 0 : &Args[0];
771 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000772 S.Context,
773 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000774 StartArg, Size));
775}
776
Michael Hanf1aae3b2012-08-03 17:40:43 +0000777static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 2> Args;
780 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
785 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000786 S.Context,
787 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000788 StartArg, Size));
789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static bool checkLocksRequiredCommon(Sema &S, Decl *D,
792 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000793 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000794 assert(!Attr.isInvalid());
795
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000796 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000797 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000798
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000799 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000800 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
801 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000802 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000803 }
804
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000805 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000806 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000807 if (Args.size() == 0)
808 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000809
Michael Handc691572012-07-23 18:48:41 +0000810 return true;
811}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000812
Michael Hanf1aae3b2012-08-03 17:40:43 +0000813static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLocksRequiredCommon(S, D, Attr, Args))
817 return;
818
819 Expr **StartArg = &Args[0];
820 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000821 S.Context,
822 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000823 Args.size()));
824}
825
Michael Hanf1aae3b2012-08-03 17:40:43 +0000826static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000827 const AttributeList &Attr) {
828 SmallVector<Expr*, 1> Args;
829 if (!checkLocksRequiredCommon(S, D, Attr, Args))
830 return;
831
832 Expr **StartArg = &Args[0];
833 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000834 S.Context,
835 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000836 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000837}
838
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000841 assert(!Attr.isInvalid());
842
843 // zero or more arguments ok
844
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000845 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000846 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
847 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000848 return;
849 }
850
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000851 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000852 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000853 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000855 Expr **StartArg = Size == 0 ? 0 : &Args[0];
856
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000857 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000858 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000859}
860
861static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000862 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000863 assert(!Attr.isInvalid());
864
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000865 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000866 return;
867
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000868 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000869 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
870 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000871 return;
872 }
873
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000874 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000875 SmallVector<Expr*, 1> Args;
876 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
877 unsigned Size = Args.size();
878 if (Size == 0)
879 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000880
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000881 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
882 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000883}
884
885static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000887 assert(!Attr.isInvalid());
888
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000889 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000890 return;
891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000893 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
894 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000895 return;
896 }
897
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000898 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000899 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000900 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000901 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000902 if (Size == 0)
903 return;
904 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000905
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000906 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000907 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000908}
909
910
Chandler Carruth1b03c872011-07-02 00:01:44 +0000911static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
912 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000913 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000914 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000915 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000916 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000917 }
Mike Stumpbf916502009-07-24 19:02:52 +0000918
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000920
921 Expr *sizeExpr;
922
923 // Special case where the argument is a template id.
924 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000925 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000926 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000927 UnqualifiedId id;
928 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000929
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000930 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
931 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000932 if (Size.isInvalid())
933 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000934
Douglas Gregor4ac01402011-06-15 16:02:29 +0000935 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000936 } else {
937 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000938 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000939 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000940
Peter Collingbourne7a730022010-11-23 20:45:58 +0000941 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000942 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000943
944 // Instantiate/Install the vector type, and let Sema build the type for us.
945 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000946 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000947 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000948 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000949 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000950
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000951 // Remember this typedef decl, we will need it later for diagnostics.
952 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000953 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000954}
955
Chandler Carruth1b03c872011-07-02 00:01:44 +0000956static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000957 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000958 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000960
Chandler Carruth87c44602011-07-01 23:49:12 +0000961 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000962 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000963 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000964 // If the alignment is less than or equal to 8 bits, the packed attribute
965 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000966 if (!FD->getType()->isDependentType() &&
967 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000968 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000969 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000970 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000971 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000972 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000974 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000975}
976
Chandler Carruth1b03c872011-07-02 00:01:44 +0000977static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000978 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
979 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000980 else
981 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
982}
983
Chandler Carruth1b03c872011-07-02 00:01:44 +0000984static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000985 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000986 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000987 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000988
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000989 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000990 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000991 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000992 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000993 return;
994 }
995
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000996 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000997}
998
Ted Kremenek2f041d02011-09-29 07:02:25 +0000999static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1000 // The IBOutlet/IBOutletCollection attributes only apply to instance
1001 // variables or properties of Objective-C classes. The outlet must also
1002 // have an object reference type.
1003 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1004 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001005 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001006 << Attr.getName() << VD->getType() << 0;
1007 return false;
1008 }
1009 }
1010 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1011 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001012 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001013 << Attr.getName() << PD->getType() << 1;
1014 return false;
1015 }
1016 }
1017 else {
1018 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1019 return false;
1020 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001021
Ted Kremenek2f041d02011-09-29 07:02:25 +00001022 return true;
1023}
1024
Chandler Carruth1b03c872011-07-02 00:01:44 +00001025static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001026 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001027 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001028 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001029
1030 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001031 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001032
Ted Kremenek2f041d02011-09-29 07:02:25 +00001033 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001034}
1035
Chandler Carruth1b03c872011-07-02 00:01:44 +00001036static void handleIBOutletCollection(Sema &S, Decl *D,
1037 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001038
1039 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001040 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1042 return;
1043 }
1044
Ted Kremenek2f041d02011-09-29 07:02:25 +00001045 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001046 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001047
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001048 IdentifierInfo *II = Attr.getParameterName();
1049 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001050 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001051
John McCallb3d87482010-08-24 05:47:05 +00001052 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001053 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001054 if (!TypeRep) {
1055 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1056 return;
1057 }
John McCallb3d87482010-08-24 05:47:05 +00001058 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001059 // Diagnose use of non-object type in iboutletcollection attribute.
1060 // FIXME. Gnu attribute extension ignores use of builtin types in
1061 // attributes. So, __attribute__((iboutletcollection(char))) will be
1062 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001063 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001064 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1065 return;
1066 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001067 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1068 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001069}
1070
Chandler Carruthd309c812011-07-01 23:49:16 +00001071static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001072 if (const RecordType *UT = T->getAsUnionType())
1073 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1074 RecordDecl *UD = UT->getDecl();
1075 for (RecordDecl::field_iterator it = UD->field_begin(),
1076 itend = UD->field_end(); it != itend; ++it) {
1077 QualType QT = it->getType();
1078 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1079 T = QT;
1080 return;
1081 }
1082 }
1083 }
1084}
1085
Nuno Lopes587de5b2012-05-24 00:22:00 +00001086static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001087 if (!isFunctionOrMethod(D)) {
1088 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1089 << "alloc_size" << ExpectedFunctionOrMethod;
1090 return;
1091 }
1092
Nuno Lopes587de5b2012-05-24 00:22:00 +00001093 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1094 return;
1095
1096 // In C++ the implicit 'this' function parameter also counts, and they are
1097 // counted from one.
1098 bool HasImplicitThisParam = isInstanceMethod(D);
1099 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1100
1101 SmallVector<unsigned, 8> SizeArgs;
1102
1103 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1104 E = Attr.arg_end(); I!=E; ++I) {
1105 // The argument must be an integer constant expression.
1106 Expr *Ex = *I;
1107 llvm::APSInt ArgNum;
1108 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1109 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1111 << "alloc_size" << Ex->getSourceRange();
1112 return;
1113 }
1114
1115 uint64_t x = ArgNum.getZExtValue();
1116
1117 if (x < 1 || x > NumArgs) {
1118 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1119 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1120 return;
1121 }
1122
1123 --x;
1124 if (HasImplicitThisParam) {
1125 if (x == 0) {
1126 S.Diag(Attr.getLoc(),
1127 diag::err_attribute_invalid_implicit_this_argument)
1128 << "alloc_size" << Ex->getSourceRange();
1129 return;
1130 }
1131 --x;
1132 }
1133
1134 // check if the function argument is of an integer type
1135 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1136 if (!T->isIntegerType()) {
1137 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1138 << "alloc_size" << Ex->getSourceRange();
1139 return;
1140 }
1141
Nuno Lopes587de5b2012-05-24 00:22:00 +00001142 SizeArgs.push_back(x);
1143 }
1144
1145 // check if the function returns a pointer
1146 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1147 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1148 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1149 }
1150
Nuno Lopes96c67d12012-06-18 16:27:56 +00001151 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1152 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001153}
1154
Chandler Carruth1b03c872011-07-02 00:01:44 +00001155static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001156 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1157 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001158 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001159 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001160 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001161 return;
1162 }
Mike Stumpbf916502009-07-24 19:02:52 +00001163
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001164 // In C++ the implicit 'this' function parameter also counts, and they are
1165 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001166 bool HasImplicitThisParam = isInstanceMethod(D);
1167 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001168
1169 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001170 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001171
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001172 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1173 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001174
1175
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001176 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001177 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001178 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001179 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1180 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001181 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1182 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001183 return;
1184 }
Mike Stumpbf916502009-07-24 19:02:52 +00001185
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001186 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001187
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001188 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001190 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001191 return;
1192 }
Mike Stumpbf916502009-07-24 19:02:52 +00001193
Ted Kremenek465172f2008-07-21 22:09:15 +00001194 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001195 if (HasImplicitThisParam) {
1196 if (x == 0) {
1197 S.Diag(Attr.getLoc(),
1198 diag::err_attribute_invalid_implicit_this_argument)
1199 << "nonnull" << Ex->getSourceRange();
1200 return;
1201 }
1202 --x;
1203 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001204
1205 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001206 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001207 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001208
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001209 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001210 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001211 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001212 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001213 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001214 }
Mike Stumpbf916502009-07-24 19:02:52 +00001215
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001216 NonNullArgs.push_back(x);
1217 }
Mike Stumpbf916502009-07-24 19:02:52 +00001218
1219 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1220 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001221 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001222 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1223 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001224 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001225 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001226 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001227 }
Mike Stumpbf916502009-07-24 19:02:52 +00001228
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001229 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001230 if (NonNullArgs.empty()) {
1231 // Warn the trivial case only if attribute is not coming from a
1232 // macro instantiation.
1233 if (Attr.getLoc().isFileID())
1234 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001235 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001236 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001237 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001238
1239 unsigned* start = &NonNullArgs[0];
1240 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001241 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001242 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001243 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001244}
1245
Chandler Carruth1b03c872011-07-02 00:01:44 +00001246static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001247 // This attribute must be applied to a function declaration.
1248 // The first argument to the attribute must be a string,
1249 // the name of the resource, for example "malloc".
1250 // The following arguments must be argument indexes, the arguments must be
1251 // of integer type for Returns, otherwise of pointer type.
1252 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001253 // after being held. free() should be __attribute((ownership_takes)), whereas
1254 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001255
1256 if (!AL.getParameterName()) {
1257 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1258 << AL.getName()->getName() << 1;
1259 return;
1260 }
1261 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001262 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001263 switch (AL.getKind()) {
1264 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001265 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001266 if (AL.getNumArgs() < 1) {
1267 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1268 return;
1269 }
Jordy Rose2a479922010-08-12 08:54:03 +00001270 break;
1271 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001272 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001273 if (AL.getNumArgs() < 1) {
1274 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1275 return;
1276 }
Jordy Rose2a479922010-08-12 08:54:03 +00001277 break;
1278 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001279 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001280 if (AL.getNumArgs() > 1) {
1281 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1282 << AL.getNumArgs() + 1;
1283 return;
1284 }
Jordy Rose2a479922010-08-12 08:54:03 +00001285 break;
1286 default:
1287 // This should never happen given how we are called.
1288 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001289 }
1290
Chandler Carruth87c44602011-07-01 23:49:12 +00001291 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001292 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1293 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001294 return;
1295 }
1296
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001297 // In C++ the implicit 'this' function parameter also counts, and they are
1298 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001299 bool HasImplicitThisParam = isInstanceMethod(D);
1300 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001301
Chris Lattner5f9e2722011-07-23 10:55:15 +00001302 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001303
1304 // Normalize the argument, __foo__ becomes foo.
1305 if (Module.startswith("__") && Module.endswith("__"))
1306 Module = Module.substr(2, Module.size() - 4);
1307
Chris Lattner5f9e2722011-07-23 10:55:15 +00001308 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001309
Jordy Rose2a479922010-08-12 08:54:03 +00001310 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1311 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001312
Peter Collingbourne7a730022010-11-23 20:45:58 +00001313 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001314 llvm::APSInt ArgNum(32);
1315 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1316 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1317 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1318 << AL.getName()->getName() << IdxExpr->getSourceRange();
1319 continue;
1320 }
1321
1322 unsigned x = (unsigned) ArgNum.getZExtValue();
1323
1324 if (x > NumArgs || x < 1) {
1325 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1326 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1327 continue;
1328 }
1329 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001330 if (HasImplicitThisParam) {
1331 if (x == 0) {
1332 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1333 << "ownership" << IdxExpr->getSourceRange();
1334 return;
1335 }
1336 --x;
1337 }
1338
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001339 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001340 case OwnershipAttr::Takes:
1341 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001343 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001344 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1345 // FIXME: Should also highlight argument in decl.
1346 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001347 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001348 << "pointer"
1349 << IdxExpr->getSourceRange();
1350 continue;
1351 }
1352 break;
1353 }
Sean Huntcf807c42010-08-18 23:23:40 +00001354 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 if (AL.getNumArgs() > 1) {
1356 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001357 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 llvm::APSInt ArgNum(32);
1359 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1360 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1361 S.Diag(AL.getLoc(), diag::err_ownership_type)
1362 << "ownership_returns" << "integer"
1363 << IdxExpr->getSourceRange();
1364 return;
1365 }
1366 }
1367 break;
1368 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001369 } // switch
1370
1371 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001372 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001373 i = D->specific_attr_begin<OwnershipAttr>(),
1374 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001375 i != e; ++i) {
1376 if ((*i)->getOwnKind() != K) {
1377 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1378 I!=E; ++I) {
1379 if (x == *I) {
1380 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1381 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001382 }
1383 }
1384 }
1385 }
1386 OwnershipArgs.push_back(x);
1387 }
1388
1389 unsigned* start = OwnershipArgs.data();
1390 unsigned size = OwnershipArgs.size();
1391 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001392
1393 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1394 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1395 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001396 }
Sean Huntcf807c42010-08-18 23:23:40 +00001397
Chandler Carruth87c44602011-07-01 23:49:12 +00001398 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001399 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001400}
1401
John McCall332bb2a2011-02-08 22:35:49 +00001402/// Whether this declaration has internal linkage for the purposes of
1403/// things that want to complain about things not have internal linkage.
1404static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1405 switch (D->getLinkage()) {
1406 case NoLinkage:
1407 case InternalLinkage:
1408 return true;
1409
1410 // Template instantiations that go from external to unique-external
1411 // shouldn't get diagnosed.
1412 case UniqueExternalLinkage:
1413 return true;
1414
1415 case ExternalLinkage:
1416 return false;
1417 }
1418 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001419}
1420
Chandler Carruth1b03c872011-07-02 00:01:44 +00001421static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001422 // Check the attribute arguments.
1423 if (Attr.getNumArgs() > 1) {
1424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1425 return;
1426 }
1427
Chandler Carruth87c44602011-07-01 23:49:12 +00001428 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001430 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001431 return;
1432 }
1433
Chandler Carruth87c44602011-07-01 23:49:12 +00001434 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001435
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001436 // gcc rejects
1437 // class c {
1438 // static int a __attribute__((weakref ("v2")));
1439 // static int b() __attribute__((weakref ("f3")));
1440 // };
1441 // and ignores the attributes of
1442 // void f(void) {
1443 // static int a __attribute__((weakref ("v2")));
1444 // }
1445 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001446 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001447 if (!Ctx->isFileContext()) {
1448 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001449 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001450 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001451 }
1452
1453 // The GCC manual says
1454 //
1455 // At present, a declaration to which `weakref' is attached can only
1456 // be `static'.
1457 //
1458 // It also says
1459 //
1460 // Without a TARGET,
1461 // given as an argument to `weakref' or to `alias', `weakref' is
1462 // equivalent to `weak'.
1463 //
1464 // gcc 4.4.1 will accept
1465 // int a7 __attribute__((weakref));
1466 // as
1467 // int a7 __attribute__((weak));
1468 // This looks like a bug in gcc. We reject that for now. We should revisit
1469 // it if this behaviour is actually used.
1470
John McCall332bb2a2011-02-08 22:35:49 +00001471 if (!hasEffectivelyInternalLinkage(nd)) {
1472 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001473 return;
1474 }
1475
1476 // GCC rejects
1477 // static ((alias ("y"), weakref)).
1478 // Should we? How to check that weakref is before or after alias?
1479
1480 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001481 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001482 Arg = Arg->IgnoreParenCasts();
1483 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1484
Douglas Gregor5cee1192011-07-27 05:40:30 +00001485 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1487 << "weakref" << 1;
1488 return;
1489 }
1490 // GCC will accept anything as the argument of weakref. Should we
1491 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001492 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001493 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001494 }
1495
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001496 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001497}
1498
Chandler Carruth1b03c872011-07-02 00:01:44 +00001499static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001500 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001501 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503 return;
1504 }
Mike Stumpbf916502009-07-24 19:02:52 +00001505
Peter Collingbourne7a730022010-11-23 20:45:58 +00001506 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001507 Arg = Arg->IgnoreParenCasts();
1508 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001509
Douglas Gregor5cee1192011-07-27 05:40:30 +00001510 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001512 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001513 return;
1514 }
Mike Stumpbf916502009-07-24 19:02:52 +00001515
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001516 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001517 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1518 return;
1519 }
1520
Chris Lattner6b6b5372008-06-26 18:38:35 +00001521 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001522
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001523 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001524 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001525}
1526
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001527static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1528 // Check the attribute arguments.
1529 if (!checkAttributeNumArgs(S, Attr, 0))
1530 return;
1531
1532 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1534 << Attr.getName() << ExpectedFunctionOrMethod;
1535 return;
1536 }
1537
1538 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1539}
1540
Benjamin Krameree409a92012-05-12 21:10:52 +00001541static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1542 // Check the attribute arguments.
1543 if (!checkAttributeNumArgs(S, Attr, 0))
1544 return;
1545
1546 if (!isa<FunctionDecl>(D)) {
1547 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1548 << Attr.getName() << ExpectedFunction;
1549 return;
1550 }
1551
1552 if (D->hasAttr<HotAttr>()) {
1553 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1554 << Attr.getName() << "hot";
1555 return;
1556 }
1557
1558 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1559}
1560
1561static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1562 // Check the attribute arguments.
1563 if (!checkAttributeNumArgs(S, Attr, 0))
1564 return;
1565
1566 if (!isa<FunctionDecl>(D)) {
1567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1568 << Attr.getName() << ExpectedFunction;
1569 return;
1570 }
1571
1572 if (D->hasAttr<ColdAttr>()) {
1573 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1574 << Attr.getName() << "cold";
1575 return;
1576 }
1577
1578 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1579}
1580
Chandler Carruth1b03c872011-07-02 00:01:44 +00001581static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001582 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001583 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001584 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001585
Chandler Carruth87c44602011-07-01 23:49:12 +00001586 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001588 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001589 return;
1590 }
1591
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001592 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001593}
1594
Chandler Carruth1b03c872011-07-02 00:01:44 +00001595static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1596 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001597 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001598 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1600 return;
1601 }
1602
Chandler Carruth87c44602011-07-01 23:49:12 +00001603 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001604 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001605 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001606 return;
1607 }
Mike Stumpbf916502009-07-24 19:02:52 +00001608
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001609 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001610}
1611
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001612static void handleTLSModelAttr(Sema &S, Decl *D,
1613 const AttributeList &Attr) {
1614 // Check the attribute arguments.
1615 if (Attr.getNumArgs() != 1) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1617 return;
1618 }
1619
1620 Expr *Arg = Attr.getArg(0);
1621 Arg = Arg->IgnoreParenCasts();
1622 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1623
1624 // Check that it is a string.
1625 if (!Str) {
1626 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1627 return;
1628 }
1629
1630 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1632 << Attr.getName() << ExpectedTLSVar;
1633 return;
1634 }
1635
1636 // Check that the value.
1637 StringRef Model = Str->getString();
1638 if (Model != "global-dynamic" && Model != "local-dynamic"
1639 && Model != "initial-exec" && Model != "local-exec") {
1640 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1641 return;
1642 }
1643
1644 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1645 Model));
1646}
1647
Chandler Carruth1b03c872011-07-02 00:01:44 +00001648static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001649 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001650 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1652 return;
1653 }
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Chandler Carruth87c44602011-07-01 23:49:12 +00001655 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001656 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001657 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001658 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001659 return;
1660 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001661 }
1662
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001663 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001664}
1665
Chandler Carruth1b03c872011-07-02 00:01:44 +00001666static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001667 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001668 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001669 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001670
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001671 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001672}
1673
Chandler Carruth1b03c872011-07-02 00:01:44 +00001674static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001675 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001676 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001677 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001678 else
1679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001680 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001681}
1682
Chandler Carruth1b03c872011-07-02 00:01:44 +00001683static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001684 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001685 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001686 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001687 else
1688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001689 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001690}
1691
Chandler Carruth1b03c872011-07-02 00:01:44 +00001692static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001693 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001694
1695 if (S.CheckNoReturnAttr(attr)) return;
1696
Chandler Carruth87c44602011-07-01 23:49:12 +00001697 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001698 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001699 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001700 return;
1701 }
1702
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001703 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001704}
1705
1706bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001707 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001708 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1709 attr.setInvalid();
1710 return true;
1711 }
1712
1713 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001714}
1715
Chandler Carruth1b03c872011-07-02 00:01:44 +00001716static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1717 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001718
1719 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1720 // because 'analyzer_noreturn' does not impact the type.
1721
Chandler Carruth1731e202011-07-11 23:30:35 +00001722 if(!checkAttributeNumArgs(S, Attr, 0))
1723 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001724
Chandler Carruth87c44602011-07-01 23:49:12 +00001725 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1726 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001727 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1728 && !VD->getType()->isFunctionPointerType())) {
1729 S.Diag(Attr.getLoc(),
1730 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1731 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001732 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001733 return;
1734 }
1735 }
1736
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001737 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001738}
1739
John Thompson35cc9622010-08-09 21:53:52 +00001740// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001741static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001742/*
1743 Returning a Vector Class in Registers
1744
Eric Christopherf48f3672010-12-01 22:13:54 +00001745 According to the PPU ABI specifications, a class with a single member of
1746 vector type is returned in memory when used as the return value of a function.
1747 This results in inefficient code when implementing vector classes. To return
1748 the value in a single vector register, add the vecreturn attribute to the
1749 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001750
1751 Example:
1752
1753 struct Vector
1754 {
1755 __vector float xyzw;
1756 } __attribute__((vecreturn));
1757
1758 Vector Add(Vector lhs, Vector rhs)
1759 {
1760 Vector result;
1761 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1762 return result; // This will be returned in a register
1763 }
1764*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001765 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001767 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001768 return;
1769 }
1770
Chandler Carruth87c44602011-07-01 23:49:12 +00001771 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001772 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1773 return;
1774 }
1775
Chandler Carruth87c44602011-07-01 23:49:12 +00001776 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001777 int count = 0;
1778
1779 if (!isa<CXXRecordDecl>(record)) {
1780 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1781 return;
1782 }
1783
1784 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1785 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1786 return;
1787 }
1788
Eric Christopherf48f3672010-12-01 22:13:54 +00001789 for (RecordDecl::field_iterator iter = record->field_begin();
1790 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001791 if ((count == 1) || !iter->getType()->isVectorType()) {
1792 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1793 return;
1794 }
1795 count++;
1796 }
1797
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001798 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001799}
1800
Chandler Carruth1b03c872011-07-02 00:01:44 +00001801static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001802 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001804 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001805 return;
1806 }
1807 // FIXME: Actually store the attribute on the declaration
1808}
1809
Chandler Carruth1b03c872011-07-02 00:01:44 +00001810static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001811 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001812 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001814 return;
1815 }
Mike Stumpbf916502009-07-24 19:02:52 +00001816
Chandler Carruth87c44602011-07-01 23:49:12 +00001817 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001818 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001820 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001821 return;
1822 }
Mike Stumpbf916502009-07-24 19:02:52 +00001823
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001824 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001825}
1826
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001827static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1828 const AttributeList &Attr) {
1829 // check the attribute arguments.
1830 if (Attr.hasParameterOrArguments()) {
1831 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1832 return;
1833 }
1834
1835 if (!isa<FunctionDecl>(D)) {
1836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1837 << Attr.getName() << ExpectedFunction;
1838 return;
1839 }
1840
1841 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1842}
1843
Chandler Carruth1b03c872011-07-02 00:01:44 +00001844static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001845 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001846 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1848 return;
1849 }
Mike Stumpbf916502009-07-24 19:02:52 +00001850
Chandler Carruth87c44602011-07-01 23:49:12 +00001851 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001852 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001853 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1854 return;
1855 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001856 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001858 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001859 return;
1860 }
Mike Stumpbf916502009-07-24 19:02:52 +00001861
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001862 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001863}
1864
Chandler Carruth1b03c872011-07-02 00:01:44 +00001865static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001866 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001867 if (Attr.getNumArgs() > 1) {
1868 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001869 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001870 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001871
1872 int priority = 65535; // FIXME: Do not hardcode such constants.
1873 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001874 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001875 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001876 if (E->isTypeDependent() || E->isValueDependent() ||
1877 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001879 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001880 return;
1881 }
1882 priority = Idx.getZExtValue();
1883 }
Mike Stumpbf916502009-07-24 19:02:52 +00001884
Chandler Carruth87c44602011-07-01 23:49:12 +00001885 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001886 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001887 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001888 return;
1889 }
1890
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001891 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001892 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001893}
1894
Chandler Carruth1b03c872011-07-02 00:01:44 +00001895static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001896 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001897 if (Attr.getNumArgs() > 1) {
1898 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001899 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001900 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001901
1902 int priority = 65535; // FIXME: Do not hardcode such constants.
1903 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001904 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001905 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001906 if (E->isTypeDependent() || E->isValueDependent() ||
1907 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001908 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001909 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001910 return;
1911 }
1912 priority = Idx.getZExtValue();
1913 }
Mike Stumpbf916502009-07-24 19:02:52 +00001914
Chandler Carruth87c44602011-07-01 23:49:12 +00001915 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001916 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001917 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001918 return;
1919 }
1920
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001921 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001922 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001923}
1924
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001925template <typename AttrTy>
1926static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1927 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001928 unsigned NumArgs = Attr.getNumArgs();
1929 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001930 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001931 return;
1932 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001933
1934 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001935 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001936 if (NumArgs == 1) {
1937 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001938 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001939 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001940 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001941 return;
1942 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001943 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001944 }
Mike Stumpbf916502009-07-24 19:02:52 +00001945
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001946 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001947}
1948
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001949static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1950 const AttributeList &Attr) {
1951 unsigned NumArgs = Attr.getNumArgs();
1952 if (NumArgs > 0) {
1953 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1954 return;
1955 }
1956
1957 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001958 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001959}
1960
Patrick Beardb2f68202012-04-06 18:12:22 +00001961static void handleObjCRootClassAttr(Sema &S, Decl *D,
1962 const AttributeList &Attr) {
1963 if (!isa<ObjCInterfaceDecl>(D)) {
1964 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1965 return;
1966 }
1967
1968 unsigned NumArgs = Attr.getNumArgs();
1969 if (NumArgs > 0) {
1970 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1971 return;
1972 }
1973
1974 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1975}
1976
Ted Kremenek71207fc2012-01-05 22:47:47 +00001977static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001978 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001979 if (!isa<ObjCInterfaceDecl>(D)) {
1980 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1981 return;
1982 }
1983
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001984 unsigned NumArgs = Attr.getNumArgs();
1985 if (NumArgs > 0) {
1986 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1987 return;
1988 }
1989
Ted Kremenek71207fc2012-01-05 22:47:47 +00001990 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001991 Attr.getRange(), S.Context));
1992}
1993
Jordy Rosefad5de92012-05-08 03:27:22 +00001994static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1995 IdentifierInfo *Platform,
1996 VersionTuple Introduced,
1997 VersionTuple Deprecated,
1998 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001999 StringRef PlatformName
2000 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2001 if (PlatformName.empty())
2002 PlatformName = Platform->getName();
2003
2004 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2005 // of these steps are needed).
2006 if (!Introduced.empty() && !Deprecated.empty() &&
2007 !(Introduced <= Deprecated)) {
2008 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2009 << 1 << PlatformName << Deprecated.getAsString()
2010 << 0 << Introduced.getAsString();
2011 return true;
2012 }
2013
2014 if (!Introduced.empty() && !Obsoleted.empty() &&
2015 !(Introduced <= Obsoleted)) {
2016 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2017 << 2 << PlatformName << Obsoleted.getAsString()
2018 << 0 << Introduced.getAsString();
2019 return true;
2020 }
2021
2022 if (!Deprecated.empty() && !Obsoleted.empty() &&
2023 !(Deprecated <= Obsoleted)) {
2024 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2025 << 2 << PlatformName << Obsoleted.getAsString()
2026 << 1 << Deprecated.getAsString();
2027 return true;
2028 }
2029
2030 return false;
2031}
2032
Rafael Espindola599f1b72012-05-13 03:25:18 +00002033AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2034 IdentifierInfo *Platform,
2035 VersionTuple Introduced,
2036 VersionTuple Deprecated,
2037 VersionTuple Obsoleted,
2038 bool IsUnavailable,
2039 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002040 VersionTuple MergedIntroduced = Introduced;
2041 VersionTuple MergedDeprecated = Deprecated;
2042 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002043 bool FoundAny = false;
2044
Rafael Espindola98ae8342012-05-10 02:50:16 +00002045 if (D->hasAttrs()) {
2046 AttrVec &Attrs = D->getAttrs();
2047 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2048 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2049 if (!OldAA) {
2050 ++i;
2051 continue;
2052 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002053
Rafael Espindola98ae8342012-05-10 02:50:16 +00002054 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2055 if (OldPlatform != Platform) {
2056 ++i;
2057 continue;
2058 }
2059
2060 FoundAny = true;
2061 VersionTuple OldIntroduced = OldAA->getIntroduced();
2062 VersionTuple OldDeprecated = OldAA->getDeprecated();
2063 VersionTuple OldObsoleted = OldAA->getObsoleted();
2064 bool OldIsUnavailable = OldAA->getUnavailable();
2065 StringRef OldMessage = OldAA->getMessage();
2066
2067 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2068 OldIntroduced != Introduced) ||
2069 (!OldDeprecated.empty() && !Deprecated.empty() &&
2070 OldDeprecated != Deprecated) ||
2071 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2072 OldObsoleted != Obsoleted) ||
2073 (OldIsUnavailable != IsUnavailable) ||
2074 (OldMessage != Message)) {
2075 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2076 Diag(Range.getBegin(), diag::note_previous_attribute);
2077 Attrs.erase(Attrs.begin() + i);
2078 --e;
2079 continue;
2080 }
2081
2082 VersionTuple MergedIntroduced2 = MergedIntroduced;
2083 VersionTuple MergedDeprecated2 = MergedDeprecated;
2084 VersionTuple MergedObsoleted2 = MergedObsoleted;
2085
2086 if (MergedIntroduced2.empty())
2087 MergedIntroduced2 = OldIntroduced;
2088 if (MergedDeprecated2.empty())
2089 MergedDeprecated2 = OldDeprecated;
2090 if (MergedObsoleted2.empty())
2091 MergedObsoleted2 = OldObsoleted;
2092
2093 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2094 MergedIntroduced2, MergedDeprecated2,
2095 MergedObsoleted2)) {
2096 Attrs.erase(Attrs.begin() + i);
2097 --e;
2098 continue;
2099 }
2100
2101 MergedIntroduced = MergedIntroduced2;
2102 MergedDeprecated = MergedDeprecated2;
2103 MergedObsoleted = MergedObsoleted2;
2104 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002105 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002106 }
2107
2108 if (FoundAny &&
2109 MergedIntroduced == Introduced &&
2110 MergedDeprecated == Deprecated &&
2111 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002112 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002113
Rafael Espindola98ae8342012-05-10 02:50:16 +00002114 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002115 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002116 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2117 Introduced, Deprecated,
2118 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002119 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002120 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002121}
2122
Chandler Carruth1b03c872011-07-02 00:01:44 +00002123static void handleAvailabilityAttr(Sema &S, Decl *D,
2124 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002125 IdentifierInfo *Platform = Attr.getParameterName();
2126 SourceLocation PlatformLoc = Attr.getParameterLoc();
2127
Rafael Espindola3b294362012-05-06 19:56:25 +00002128 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002129 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2130 << Platform;
2131
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002132 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2133 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2134 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002135 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002136 StringRef Str;
2137 const StringLiteral *SE =
2138 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2139 if (SE)
2140 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002141
Rafael Espindola599f1b72012-05-13 03:25:18 +00002142 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2143 Platform,
2144 Introduced.Version,
2145 Deprecated.Version,
2146 Obsoleted.Version,
2147 IsUnavailable, Str);
2148 if (NewAttr)
2149 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002150}
2151
Rafael Espindola599f1b72012-05-13 03:25:18 +00002152VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2153 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002154 if (isa<TypedefNameDecl>(D)) {
2155 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002156 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002157 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002158 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2159 if (ExistingAttr) {
2160 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2161 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002162 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002163 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2164 Diag(Range.getBegin(), diag::note_previous_attribute);
2165 D->dropAttr<VisibilityAttr>();
2166 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002167 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002168}
2169
Chandler Carruth1b03c872011-07-02 00:01:44 +00002170static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002171 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002172 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002173 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002174
Peter Collingbourne7a730022010-11-23 20:45:58 +00002175 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002176 Arg = Arg->IgnoreParenCasts();
2177 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002178
Douglas Gregor5cee1192011-07-27 05:40:30 +00002179 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002181 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002182 return;
2183 }
Mike Stumpbf916502009-07-24 19:02:52 +00002184
Chris Lattner5f9e2722011-07-23 10:55:15 +00002185 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002186 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002187
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002188 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002189 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002190 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002191 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002192 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002193 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002194 else if (TypeStr == "protected") {
2195 // Complain about attempts to use protected visibility on targets
2196 // (like Darwin) that don't support it.
2197 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2198 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2199 type = VisibilityAttr::Default;
2200 } else {
2201 type = VisibilityAttr::Protected;
2202 }
2203 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002204 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002205 return;
2206 }
Mike Stumpbf916502009-07-24 19:02:52 +00002207
Rafael Espindola599f1b72012-05-13 03:25:18 +00002208 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2209 if (NewAttr)
2210 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002211}
2212
Chandler Carruth1b03c872011-07-02 00:01:44 +00002213static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2214 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002215 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2216 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002218 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002219 return;
2220 }
2221
Chandler Carruth87c44602011-07-01 23:49:12 +00002222 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2223 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002225 << "objc_method_family" << 1;
2226 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002228 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002229 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002230 return;
2231 }
2232
Chris Lattner5f9e2722011-07-23 10:55:15 +00002233 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002234 ObjCMethodFamilyAttr::FamilyKind family;
2235 if (param == "none")
2236 family = ObjCMethodFamilyAttr::OMF_None;
2237 else if (param == "alloc")
2238 family = ObjCMethodFamilyAttr::OMF_alloc;
2239 else if (param == "copy")
2240 family = ObjCMethodFamilyAttr::OMF_copy;
2241 else if (param == "init")
2242 family = ObjCMethodFamilyAttr::OMF_init;
2243 else if (param == "mutableCopy")
2244 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2245 else if (param == "new")
2246 family = ObjCMethodFamilyAttr::OMF_new;
2247 else {
2248 // Just warn and ignore it. This is future-proof against new
2249 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002250 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002251 return;
2252 }
2253
John McCallf85e1932011-06-15 23:02:42 +00002254 if (family == ObjCMethodFamilyAttr::OMF_init &&
2255 !method->getResultType()->isObjCObjectPointerType()) {
2256 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2257 << method->getResultType();
2258 // Ignore the attribute.
2259 return;
2260 }
2261
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002262 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002263 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002264}
2265
Chandler Carruth1b03c872011-07-02 00:01:44 +00002266static void handleObjCExceptionAttr(Sema &S, Decl *D,
2267 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002268 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002269 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002270
Chris Lattner0db29ec2009-02-14 08:09:34 +00002271 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2272 if (OCI == 0) {
2273 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2274 return;
2275 }
Mike Stumpbf916502009-07-24 19:02:52 +00002276
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002277 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002278}
2279
Chandler Carruth1b03c872011-07-02 00:01:44 +00002280static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002281 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002282 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002283 return;
2284 }
Richard Smith162e1c12011-04-15 14:24:37 +00002285 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002286 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002287 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002288 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2289 return;
2290 }
2291 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002292 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2293 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002294 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002295 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2296 return;
2297 }
2298 }
2299 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002300 // It is okay to include this attribute on properties, e.g.:
2301 //
2302 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2303 //
2304 // In this case it follows tradition and suppresses an error in the above
2305 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002306 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002307 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002308 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002309}
2310
Mike Stumpbf916502009-07-24 19:02:52 +00002311static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002312handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002313 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002314 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002315 return;
2316 }
2317
2318 if (!isa<FunctionDecl>(D)) {
2319 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2320 return;
2321 }
2322
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002323 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002324}
2325
Chandler Carruth1b03c872011-07-02 00:01:44 +00002326static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002327 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002328 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002329 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002330 return;
2331 }
Mike Stumpbf916502009-07-24 19:02:52 +00002332
Steve Naroff9eae5762008-09-18 16:44:58 +00002333 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002334 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002335 return;
2336 }
Mike Stumpbf916502009-07-24 19:02:52 +00002337
Sean Huntcf807c42010-08-18 23:23:40 +00002338 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002339 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002340 type = BlocksAttr::ByRef;
2341 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002342 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002343 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002344 return;
2345 }
Mike Stumpbf916502009-07-24 19:02:52 +00002346
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002347 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002348}
2349
Chandler Carruth1b03c872011-07-02 00:01:44 +00002350static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002351 // check the attribute arguments.
2352 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002353 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002354 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002355 }
2356
John McCall3323fad2011-09-09 07:56:05 +00002357 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002358 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002359 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002360 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002361 if (E->isTypeDependent() || E->isValueDependent() ||
2362 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002363 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002364 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002365 return;
2366 }
Mike Stumpbf916502009-07-24 19:02:52 +00002367
John McCall3323fad2011-09-09 07:56:05 +00002368 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002369 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2370 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002371 return;
2372 }
John McCall3323fad2011-09-09 07:56:05 +00002373
2374 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002375 }
2376
John McCall3323fad2011-09-09 07:56:05 +00002377 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002378 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002379 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002380 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002381 if (E->isTypeDependent() || E->isValueDependent() ||
2382 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002383 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002384 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002385 return;
2386 }
2387 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002388
John McCall3323fad2011-09-09 07:56:05 +00002389 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002390 // FIXME: This error message could be improved, it would be nice
2391 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002392 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2393 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002394 return;
2395 }
2396 }
2397
Chandler Carruth87c44602011-07-01 23:49:12 +00002398 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002399 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002400 if (isa<FunctionNoProtoType>(FT)) {
2401 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2402 return;
2403 }
Mike Stumpbf916502009-07-24 19:02:52 +00002404
Chris Lattner897cd902009-03-17 23:03:47 +00002405 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002406 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002407 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002408 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002409 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002410 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002411 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002412 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002413 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002414 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2415 if (!BD->isVariadic()) {
2416 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2417 return;
2418 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002419 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002420 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002421 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002422 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002423 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002424 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002425 int m = Ty->isFunctionPointerType() ? 0 : 1;
2426 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002427 return;
2428 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002429 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002431 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002432 return;
2433 }
Anders Carlsson77091822008-10-05 18:05:59 +00002434 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002436 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002437 return;
2438 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002439 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002440 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002441}
2442
Chandler Carruth1b03c872011-07-02 00:01:44 +00002443static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002444 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002445 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002446 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002447
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002448 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002449 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002450 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002451 return;
2452 }
Mike Stumpbf916502009-07-24 19:02:52 +00002453
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002454 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2455 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2456 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002457 return;
2458 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002459 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2460 if (MD->getResultType()->isVoidType()) {
2461 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2462 << Attr.getName() << 1;
2463 return;
2464 }
2465
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002466 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002467}
2468
Chandler Carruth1b03c872011-07-02 00:01:44 +00002469static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002470 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002471 if (Attr.hasParameterOrArguments()) {
2472 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002473 return;
2474 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002475
Chandler Carruth87c44602011-07-01 23:49:12 +00002476 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002477 if (isa<CXXRecordDecl>(D)) {
2478 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2479 return;
2480 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002481 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2482 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002483 return;
2484 }
2485
Chandler Carruth87c44602011-07-01 23:49:12 +00002486 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002487
2488 // 'weak' only applies to declarations with external linkage.
2489 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002490 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002491 return;
2492 }
Mike Stumpbf916502009-07-24 19:02:52 +00002493
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002494 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002495}
2496
Chandler Carruth1b03c872011-07-02 00:01:44 +00002497static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002498 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002499 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002500 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002501
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002502
2503 // weak_import only applies to variable & function declarations.
2504 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002505 if (!D->canBeWeakImported(isDef)) {
2506 if (isDef)
2507 S.Diag(Attr.getLoc(),
2508 diag::warn_attribute_weak_import_invalid_on_definition)
2509 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002510 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002511 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002512 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002513 // Nothing to warn about here.
2514 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002515 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002516 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002517
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002518 return;
2519 }
2520
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002521 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002522}
2523
Tanya Lattner0df579e2012-07-09 22:06:01 +00002524// Handles reqd_work_group_size and work_group_size_hint.
2525static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002526 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002527 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2528 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2529
Nate Begeman6f3d8382009-06-26 06:32:41 +00002530 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002531 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002532
2533 unsigned WGSize[3];
2534 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002535 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002536 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002537 if (E->isTypeDependent() || E->isValueDependent() ||
2538 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002540 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002541 return;
2542 }
2543 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2544 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002545
2546 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2547 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2548 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2549 if (!(A->getXDim() == WGSize[0] &&
2550 A->getYDim() == WGSize[1] &&
2551 A->getZDim() == WGSize[2])) {
2552 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2553 Attr.getName();
2554 }
2555 }
2556
2557 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2558 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2559 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2560 if (!(A->getXDim() == WGSize[0] &&
2561 A->getYDim() == WGSize[1] &&
2562 A->getZDim() == WGSize[2])) {
2563 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2564 Attr.getName();
2565 }
2566 }
2567
2568 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2569 D->addAttr(::new (S.Context)
2570 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2571 WGSize[0], WGSize[1], WGSize[2]));
2572 else
2573 D->addAttr(::new (S.Context)
2574 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2575 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002576}
2577
Rafael Espindola599f1b72012-05-13 03:25:18 +00002578SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2579 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002580 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2581 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002582 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002583 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2584 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002585 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002586 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002587 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002588}
2589
Chandler Carruth1b03c872011-07-02 00:01:44 +00002590static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002591 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002592 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002593 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002594
2595 // Make sure that there is a string literal as the sections's single
2596 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002597 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002598 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002599 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002600 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002601 return;
2602 }
Mike Stump1eb44332009-09-09 15:08:12 +00002603
Chris Lattner797c3c42009-08-10 19:03:04 +00002604 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002605 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002606 if (!Error.empty()) {
2607 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2608 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002609 return;
2610 }
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002612 // This attribute cannot be applied to local variables.
2613 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2614 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2615 return;
2616 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002617 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2618 SE->getString());
2619 if (NewAttr)
2620 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002621}
2622
Chris Lattner6b6b5372008-06-26 18:38:35 +00002623
Chandler Carruth1b03c872011-07-02 00:01:44 +00002624static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002625 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002626 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002628 return;
2629 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002630
Chandler Carruth87c44602011-07-01 23:49:12 +00002631 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002632 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002633 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002634 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002635 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002636 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002637}
2638
Chandler Carruth1b03c872011-07-02 00:01:44 +00002639static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002640 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002641 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002643 return;
2644 }
Mike Stumpbf916502009-07-24 19:02:52 +00002645
Chandler Carruth87c44602011-07-01 23:49:12 +00002646 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002647 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002648 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002649 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002650 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002651 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002652}
2653
Chandler Carruth1b03c872011-07-02 00:01:44 +00002654static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002655 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002656 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002657 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002658
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002659 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002660}
2661
Chandler Carruth1b03c872011-07-02 00:01:44 +00002662static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002663 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002664 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2665 return;
2666 }
Mike Stumpbf916502009-07-24 19:02:52 +00002667
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002668 if (Attr.getNumArgs() != 0) {
2669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2670 return;
2671 }
Mike Stumpbf916502009-07-24 19:02:52 +00002672
Chandler Carruth87c44602011-07-01 23:49:12 +00002673 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002674
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002675 if (!VD || !VD->hasLocalStorage()) {
2676 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2677 return;
2678 }
Mike Stumpbf916502009-07-24 19:02:52 +00002679
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002680 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002681 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002682 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002683 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2684 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002685 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002686 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002687 Attr.getParameterName();
2688 return;
2689 }
Mike Stumpbf916502009-07-24 19:02:52 +00002690
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002691 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2692 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002693 S.Diag(Attr.getParameterLoc(),
2694 diag::err_attribute_cleanup_arg_not_function)
2695 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002696 return;
2697 }
2698
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002699 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002700 S.Diag(Attr.getParameterLoc(),
2701 diag::err_attribute_cleanup_func_must_take_one_arg)
2702 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002703 return;
2704 }
Mike Stumpbf916502009-07-24 19:02:52 +00002705
Anders Carlsson89941c12009-02-07 23:16:50 +00002706 // We're currently more strict than GCC about what function types we accept.
2707 // If this ever proves to be a problem it should be easy to fix.
2708 QualType Ty = S.Context.getPointerType(VD->getType());
2709 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002710 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2711 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002712 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002713 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2714 Attr.getParameterName() << ParamTy << Ty;
2715 return;
2716 }
Mike Stumpbf916502009-07-24 19:02:52 +00002717
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002718 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002719 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002720}
2721
Mike Stumpbf916502009-07-24 19:02:52 +00002722/// Handle __attribute__((format_arg((idx)))) attribute based on
2723/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002724static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002725 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002726 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002727
Chandler Carruth87c44602011-07-01 23:49:12 +00002728 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002730 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002731 return;
2732 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002733
2734 // In C++ the implicit 'this' function parameter also counts, and they are
2735 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002736 bool HasImplicitThisParam = isInstanceMethod(D);
2737 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002738 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002739
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002740 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002741 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002742 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002743 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2744 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002745 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2746 << "format" << 2 << IdxExpr->getSourceRange();
2747 return;
2748 }
Mike Stumpbf916502009-07-24 19:02:52 +00002749
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002750 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2751 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2752 << "format" << 2 << IdxExpr->getSourceRange();
2753 return;
2754 }
Mike Stumpbf916502009-07-24 19:02:52 +00002755
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002756 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002757
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002758 if (HasImplicitThisParam) {
2759 if (ArgIdx == 0) {
2760 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2761 << "format_arg" << IdxExpr->getSourceRange();
2762 return;
2763 }
2764 ArgIdx--;
2765 }
2766
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002767 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002768 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002769
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002770 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2771 if (not_nsstring_type &&
2772 !isCFStringType(Ty, S.Context) &&
2773 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002774 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002775 // FIXME: Should highlight the actual expression that has the wrong type.
2776 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002777 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002778 << IdxExpr->getSourceRange();
2779 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002780 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002781 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002782 if (!isNSStringType(Ty, S.Context) &&
2783 !isCFStringType(Ty, S.Context) &&
2784 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002785 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002786 // FIXME: Should highlight the actual expression that has the wrong type.
2787 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002788 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002789 << IdxExpr->getSourceRange();
2790 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002791 }
2792
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002793 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002794 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002795}
2796
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002797enum FormatAttrKind {
2798 CFStringFormat,
2799 NSStringFormat,
2800 StrftimeFormat,
2801 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002802 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002803 InvalidFormat
2804};
2805
2806/// getFormatAttrKind - Map from format attribute names to supported format
2807/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002808static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002809 return llvm::StringSwitch<FormatAttrKind>(Format)
2810 // Check for formats that get handled specially.
2811 .Case("NSString", NSStringFormat)
2812 .Case("CFString", CFStringFormat)
2813 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002814
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002815 // Otherwise, check for supported formats.
2816 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2817 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2818 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002819
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002820 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2821 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002822}
2823
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002824/// Handle __attribute__((init_priority(priority))) attributes based on
2825/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002826static void handleInitPriorityAttr(Sema &S, Decl *D,
2827 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002828 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002829 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2830 return;
2831 }
2832
Chandler Carruth87c44602011-07-01 23:49:12 +00002833 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002834 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2835 Attr.setInvalid();
2836 return;
2837 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002838 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002839 if (S.Context.getAsArrayType(T))
2840 T = S.Context.getBaseElementType(T);
2841 if (!T->getAs<RecordType>()) {
2842 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2843 Attr.setInvalid();
2844 return;
2845 }
2846
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002847 if (Attr.getNumArgs() != 1) {
2848 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2849 Attr.setInvalid();
2850 return;
2851 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002852 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002853
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002854 llvm::APSInt priority(32);
2855 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2856 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2857 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2858 << "init_priority" << priorityExpr->getSourceRange();
2859 Attr.setInvalid();
2860 return;
2861 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002862 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002863 if (prioritynum < 101 || prioritynum > 65535) {
2864 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2865 << priorityExpr->getSourceRange();
2866 Attr.setInvalid();
2867 return;
2868 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002869 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002870 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002871}
2872
Rafael Espindola599f1b72012-05-13 03:25:18 +00002873FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2874 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002875 // Check whether we already have an equivalent format attribute.
2876 for (specific_attr_iterator<FormatAttr>
2877 i = D->specific_attr_begin<FormatAttr>(),
2878 e = D->specific_attr_end<FormatAttr>();
2879 i != e ; ++i) {
2880 FormatAttr *f = *i;
2881 if (f->getType() == Format &&
2882 f->getFormatIdx() == FormatIdx &&
2883 f->getFirstArg() == FirstArg) {
2884 // If we don't have a valid location for this attribute, adopt the
2885 // location.
2886 if (f->getLocation().isInvalid())
2887 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002888 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002889 }
2890 }
2891
Rafael Espindola599f1b72012-05-13 03:25:18 +00002892 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2893 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002894}
2895
Mike Stumpbf916502009-07-24 19:02:52 +00002896/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2897/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002898static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002899
Chris Lattner545dd342008-06-28 23:36:30 +00002900 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002902 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002903 return;
2904 }
2905
Chris Lattner545dd342008-06-28 23:36:30 +00002906 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002908 return;
2909 }
2910
Chandler Carruth87c44602011-07-01 23:49:12 +00002911 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002912 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002913 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002914 return;
2915 }
2916
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002917 // In C++ the implicit 'this' function parameter also counts, and they are
2918 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002919 bool HasImplicitThisParam = isInstanceMethod(D);
2920 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002921 unsigned FirstIdx = 1;
2922
Chris Lattner5f9e2722011-07-23 10:55:15 +00002923 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002924
2925 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002926 if (Format.startswith("__") && Format.endswith("__"))
2927 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002928
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002929 // Check for supported formats.
2930 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002931
2932 if (Kind == IgnoredFormat)
2933 return;
2934
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002935 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002936 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002937 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002938 return;
2939 }
2940
2941 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002942 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002943 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002944 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2945 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002946 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002947 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002948 return;
2949 }
2950
2951 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002952 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002953 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002954 return;
2955 }
2956
2957 // FIXME: Do we need to bounds check?
2958 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002959
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002960 if (HasImplicitThisParam) {
2961 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002962 S.Diag(Attr.getLoc(),
2963 diag::err_format_attribute_implicit_this_format_string)
2964 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002965 return;
2966 }
2967 ArgIdx--;
2968 }
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Chris Lattner6b6b5372008-06-26 18:38:35 +00002970 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002971 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002972
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002973 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002974 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002975 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2976 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002977 return;
2978 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002979 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002980 // FIXME: do we need to check if the type is NSString*? What are the
2981 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002982 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002983 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002984 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2985 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002986 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002987 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002988 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002989 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002990 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002991 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2992 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002993 return;
2994 }
2995
2996 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002997 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002998 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002999 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3000 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003002 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003003 return;
3004 }
3005
3006 // check if the function is variadic if the 3rd argument non-zero
3007 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003008 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003009 ++NumArgs; // +1 for ...
3010 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003011 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003012 return;
3013 }
3014 }
3015
Chris Lattner3c73c412008-11-19 08:23:25 +00003016 // strftime requires FirstArg to be 0 because it doesn't read from any
3017 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003018 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003019 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003020 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3021 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003022 return;
3023 }
3024 // if 0 it disables parameter checking (to use with e.g. va_list)
3025 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003027 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003028 return;
3029 }
3030
Rafael Espindola599f1b72012-05-13 03:25:18 +00003031 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3032 Idx.getZExtValue(),
3033 FirstArg.getZExtValue());
3034 if (NewAttr)
3035 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003036}
3037
Chandler Carruth1b03c872011-07-02 00:01:44 +00003038static void handleTransparentUnionAttr(Sema &S, Decl *D,
3039 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003040 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003041 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003042 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003043
Chris Lattner6b6b5372008-06-26 18:38:35 +00003044
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003045 // Try to find the underlying union declaration.
3046 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003047 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003048 if (TD && TD->getUnderlyingType()->isUnionType())
3049 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3050 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003051 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003052
3053 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003054 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003055 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003056 return;
3057 }
3058
John McCall5e1cdac2011-10-07 06:10:15 +00003059 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003060 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003061 diag::warn_transparent_union_attribute_not_definition);
3062 return;
3063 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003064
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003065 RecordDecl::field_iterator Field = RD->field_begin(),
3066 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003067 if (Field == FieldEnd) {
3068 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3069 return;
3070 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003071
David Blaikie581deb32012-06-06 20:45:41 +00003072 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003073 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003074 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003075 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003076 diag::warn_transparent_union_attribute_floating)
3077 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003078 return;
3079 }
3080
3081 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3082 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3083 for (; Field != FieldEnd; ++Field) {
3084 QualType FieldType = Field->getType();
3085 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3086 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3087 // Warn if we drop the attribute.
3088 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003089 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003090 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003091 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003092 diag::warn_transparent_union_attribute_field_size_align)
3093 << isSize << Field->getDeclName() << FieldBits;
3094 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003095 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003096 diag::note_transparent_union_first_field_size_align)
3097 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003098 return;
3099 }
3100 }
3101
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003102 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003103}
3104
Chandler Carruth1b03c872011-07-02 00:01:44 +00003105static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003106 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003107 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003108 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003109
Peter Collingbourne7a730022010-11-23 20:45:58 +00003110 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003111 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003112
Chris Lattner6b6b5372008-06-26 18:38:35 +00003113 // Make sure that there is a string literal as the annotation's single
3114 // argument.
3115 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003116 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003117 return;
3118 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003119
3120 // Don't duplicate annotations that are already set.
3121 for (specific_attr_iterator<AnnotateAttr>
3122 i = D->specific_attr_begin<AnnotateAttr>(),
3123 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3124 if ((*i)->getAnnotation() == SE->getString())
3125 return;
3126 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003127 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003128 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003129}
3130
Chandler Carruth1b03c872011-07-02 00:01:44 +00003131static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003132 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003133 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003134 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003135 return;
3136 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003137
Sean Huntbbd37c62009-11-21 08:43:09 +00003138 //FIXME: The C++0x version of this attribute has more limited applicabilty
3139 // than GNU's, and should error out when it is used to specify a
3140 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141
Chris Lattner545dd342008-06-28 23:36:30 +00003142 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003143 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3144 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003145 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003146 }
Mike Stumpbf916502009-07-24 19:02:52 +00003147
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003148 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3149 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003150}
3151
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003152void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3153 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003154 // FIXME: Handle pack-expansions here.
3155 if (DiagnoseUnexpandedParameterPack(E))
3156 return;
3157
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003158 if (E->isTypeDependent() || E->isValueDependent()) {
3159 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003160 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3161 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003162 return;
3163 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003164
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003165 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003166 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003167 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003168 ExprResult ICE
3169 = VerifyIntegerConstantExpression(E, &Alignment,
3170 diag::err_aligned_attribute_argument_not_int,
3171 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003172 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003173 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003174 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003175 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3176 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003177 return;
3178 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003179 if (isDeclSpec) {
3180 // We've already verified it's a power of 2, now let's make sure it's
3181 // 8192 or less.
3182 if (Alignment.getZExtValue() > 8192) {
3183 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3184 << E->getSourceRange();
3185 return;
3186 }
3187 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003188
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003189 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3190 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003191}
3192
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003193void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3194 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003195 // FIXME: Cache the number on the Attr object if non-dependent?
3196 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003197 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3198 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003199 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003200}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003201
Chandler Carruthd309c812011-07-01 23:49:16 +00003202/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003203/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003204///
Mike Stumpbf916502009-07-24 19:02:52 +00003205/// Despite what would be logical, the mode attribute is a decl attribute, not a
3206/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3207/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003208static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003209 // This attribute isn't documented, but glibc uses it. It changes
3210 // the width of an int or unsigned int to the specified size.
3211
3212 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003213 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003214 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003215
Chris Lattnerfbf13472008-06-27 22:18:37 +00003216
3217 IdentifierInfo *Name = Attr.getParameterName();
3218 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003219 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003220 return;
3221 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003222
Chris Lattner5f9e2722011-07-23 10:55:15 +00003223 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003224
3225 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003226 if (Str.startswith("__") && Str.endswith("__"))
3227 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003228
3229 unsigned DestWidth = 0;
3230 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003231 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003232 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003233 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003234 switch (Str[0]) {
3235 case 'Q': DestWidth = 8; break;
3236 case 'H': DestWidth = 16; break;
3237 case 'S': DestWidth = 32; break;
3238 case 'D': DestWidth = 64; break;
3239 case 'X': DestWidth = 96; break;
3240 case 'T': DestWidth = 128; break;
3241 }
3242 if (Str[1] == 'F') {
3243 IntegerMode = false;
3244 } else if (Str[1] == 'C') {
3245 IntegerMode = false;
3246 ComplexMode = true;
3247 } else if (Str[1] != 'I') {
3248 DestWidth = 0;
3249 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003250 break;
3251 case 4:
3252 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3253 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003254 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003255 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003256 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003257 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003258 break;
3259 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003260 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003261 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003262 break;
3263 }
3264
3265 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003266 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003267 OldTy = TD->getUnderlyingType();
3268 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3269 OldTy = VD->getType();
3270 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003271 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003272 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003273 return;
3274 }
Eli Friedman73397492009-03-03 06:41:03 +00003275
John McCall183700f2009-09-21 23:43:11 +00003276 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003277 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3278 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003279 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003280 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3281 } else if (ComplexMode) {
3282 if (!OldTy->isComplexType())
3283 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3284 } else {
3285 if (!OldTy->isFloatingType())
3286 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3287 }
3288
Mike Stump390b4cc2009-05-16 07:39:55 +00003289 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3290 // and friends, at least with glibc.
3291 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3292 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003293 // FIXME: Make sure floating-point mappings are accurate
3294 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003295 QualType NewTy;
3296 switch (DestWidth) {
3297 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003298 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003299 return;
3300 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003301 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003302 return;
3303 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003304 if (!IntegerMode) {
3305 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3306 return;
3307 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003308 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003309 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003310 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003311 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003312 break;
3313 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003314 if (!IntegerMode) {
3315 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3316 return;
3317 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003318 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003319 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003320 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003321 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003322 break;
3323 case 32:
3324 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003325 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003326 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003327 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003328 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003329 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003330 break;
3331 case 64:
3332 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003333 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003334 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003335 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003336 NewTy = S.Context.LongTy;
3337 else
3338 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003339 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003340 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003341 NewTy = S.Context.UnsignedLongTy;
3342 else
3343 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003344 break;
Eli Friedman73397492009-03-03 06:41:03 +00003345 case 96:
3346 NewTy = S.Context.LongDoubleTy;
3347 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003348 case 128:
3349 if (!IntegerMode) {
3350 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3351 return;
3352 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003353 if (OldTy->isSignedIntegerType())
3354 NewTy = S.Context.Int128Ty;
3355 else
3356 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003357 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003358 }
3359
Eli Friedman73397492009-03-03 06:41:03 +00003360 if (ComplexMode) {
3361 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003362 }
3363
3364 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003365 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003366 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003367 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003368 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003369 cast<ValueDecl>(D)->setType(NewTy);
3370}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003371
Chandler Carruth1b03c872011-07-02 00:01:44 +00003372static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003373 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003374 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003375 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003376
Nick Lewycky78d1a102012-07-24 01:40:49 +00003377 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3378 if (!VD->hasGlobalStorage())
3379 S.Diag(Attr.getLoc(),
3380 diag::warn_attribute_requires_functions_or_static_globals)
3381 << Attr.getName();
3382 } else if (!isFunctionOrMethod(D)) {
3383 S.Diag(Attr.getLoc(),
3384 diag::warn_attribute_requires_functions_or_static_globals)
3385 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003386 return;
3387 }
Mike Stumpbf916502009-07-24 19:02:52 +00003388
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003389 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003390}
3391
Chandler Carruth1b03c872011-07-02 00:01:44 +00003392static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003393 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003394 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003395 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003396
Mike Stumpbf916502009-07-24 19:02:52 +00003397
Chandler Carruth87c44602011-07-01 23:49:12 +00003398 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003399 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003400 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003401 return;
3402 }
Mike Stumpbf916502009-07-24 19:02:52 +00003403
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003404 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003405}
3406
Chandler Carruth1b03c872011-07-02 00:01:44 +00003407static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3408 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003409 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003410 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003411 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003412
Chris Lattner7255a2d2010-06-22 00:03:40 +00003413
Chandler Carruth87c44602011-07-01 23:49:12 +00003414 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003416 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003417 return;
3418 }
3419
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003420 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003421 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003422}
3423
Chandler Carruth1b03c872011-07-02 00:01:44 +00003424static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003425 if (S.LangOpts.CUDA) {
3426 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003427 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3429 return;
3430 }
3431
Chandler Carruth87c44602011-07-01 23:49:12 +00003432 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003433 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003434 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003435 return;
3436 }
3437
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003438 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003439 } else {
3440 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3441 }
3442}
3443
Chandler Carruth1b03c872011-07-02 00:01:44 +00003444static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003445 if (S.LangOpts.CUDA) {
3446 // check the attribute arguments.
3447 if (Attr.getNumArgs() != 0) {
3448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3449 return;
3450 }
3451
Chandler Carruth87c44602011-07-01 23:49:12 +00003452 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003453 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003454 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003455 return;
3456 }
3457
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003458 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003459 } else {
3460 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3461 }
3462}
3463
Chandler Carruth1b03c872011-07-02 00:01:44 +00003464static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003465 if (S.LangOpts.CUDA) {
3466 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003467 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003468 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003469
Chandler Carruth87c44602011-07-01 23:49:12 +00003470 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003472 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003473 return;
3474 }
3475
Chandler Carruth87c44602011-07-01 23:49:12 +00003476 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003477 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003478 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003479 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3480 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3481 << FD->getType()
3482 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3483 "void");
3484 } else {
3485 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3486 << FD->getType();
3487 }
3488 return;
3489 }
3490
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003491 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003492 } else {
3493 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3494 }
3495}
3496
Chandler Carruth1b03c872011-07-02 00:01:44 +00003497static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003498 if (S.LangOpts.CUDA) {
3499 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003500 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003501 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003502
Peter Collingbourneced76712010-12-01 03:15:31 +00003503
Chandler Carruth87c44602011-07-01 23:49:12 +00003504 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003505 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003506 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003507 return;
3508 }
3509
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003510 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003511 } else {
3512 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3513 }
3514}
3515
Chandler Carruth1b03c872011-07-02 00:01:44 +00003516static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003517 if (S.LangOpts.CUDA) {
3518 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003519 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003520 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003521
Peter Collingbourneced76712010-12-01 03:15:31 +00003522
Chandler Carruth87c44602011-07-01 23:49:12 +00003523 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003525 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003526 return;
3527 }
3528
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003529 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003530 } else {
3531 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3532 }
3533}
3534
Chandler Carruth1b03c872011-07-02 00:01:44 +00003535static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003536 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003537 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003538 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003539
Chandler Carruth87c44602011-07-01 23:49:12 +00003540 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003541 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003542 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003543 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003544 return;
3545 }
Mike Stumpbf916502009-07-24 19:02:52 +00003546
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003547 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003548 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003549 return;
3550 }
Mike Stumpbf916502009-07-24 19:02:52 +00003551
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003552 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003553}
3554
Chandler Carruth1b03c872011-07-02 00:01:44 +00003555static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003556 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003557
Chandler Carruth87c44602011-07-01 23:49:12 +00003558 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003559 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3560 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003561 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003562 return;
3563
Chandler Carruth87c44602011-07-01 23:49:12 +00003564 if (!isa<ObjCMethodDecl>(D)) {
3565 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3566 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003567 return;
3568 }
3569
Chandler Carruth87c44602011-07-01 23:49:12 +00003570 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003571 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003572 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003573 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003574 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003575 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003576 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003577 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003578 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003579 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003580 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003581 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003582 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003583 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003584 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003585 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003586 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003587 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003588 switch (CC) {
3589 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003590 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003591 break;
3592 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003593 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003594 break;
3595 default:
3596 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003597 }
3598
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003599 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuff263366f2012-10-16 22:30:41 +00003600 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003601 }
Derek Schuff263366f2012-10-16 22:30:41 +00003602 case AttributeList::AT_PnaclCall:
3603 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3604 return;
3605
Abramo Bagnarae215f722010-04-30 13:10:51 +00003606 default:
3607 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003608 }
3609}
3610
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003612 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003613 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003614}
3615
John McCall711c52b2011-01-05 12:14:39 +00003616bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3617 if (attr.isInvalid())
3618 return true;
3619
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003620 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3621 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3622 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003623 attr.setInvalid();
3624 return true;
3625 }
3626
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003627 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3628 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003629 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003630 case AttributeList::AT_CDecl: CC = CC_C; break;
3631 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3632 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3633 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3634 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3635 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003636 Expr *Arg = attr.getArg(0);
3637 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003638 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003639 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3640 << "pcs" << 1;
3641 attr.setInvalid();
3642 return true;
3643 }
3644
Chris Lattner5f9e2722011-07-23 10:55:15 +00003645 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003646 if (StrRef == "aapcs") {
3647 CC = CC_AAPCS;
3648 break;
3649 } else if (StrRef == "aapcs-vfp") {
3650 CC = CC_AAPCS_VFP;
3651 break;
3652 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003653
3654 attr.setInvalid();
3655 Diag(attr.getLoc(), diag::err_invalid_pcs);
3656 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003657 }
Derek Schuff263366f2012-10-16 22:30:41 +00003658 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
David Blaikie7530c032012-01-17 06:56:22 +00003659 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003660 }
3661
Aaron Ballman82bfa192012-10-02 14:26:08 +00003662 const TargetInfo &TI = Context.getTargetInfo();
3663 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3664 if (A == TargetInfo::CCCR_Warning) {
3665 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
3666 CC = TI.getDefaultCallingConv();
3667 }
3668
John McCall711c52b2011-01-05 12:14:39 +00003669 return false;
3670}
3671
Chandler Carruth1b03c872011-07-02 00:01:44 +00003672static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003673 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003674
3675 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003676 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003677 return;
3678
Chandler Carruth87c44602011-07-01 23:49:12 +00003679 if (!isa<ObjCMethodDecl>(D)) {
3680 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3681 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003682 return;
3683 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003684
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003685 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003686}
3687
3688/// Checks a regparm attribute, returning true if it is ill-formed and
3689/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003690bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3691 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003692 return true;
3693
Chandler Carruth87c44602011-07-01 23:49:12 +00003694 if (Attr.getNumArgs() != 1) {
3695 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3696 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003697 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003698 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003699
Chandler Carruth87c44602011-07-01 23:49:12 +00003700 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003701 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003702 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003703 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003704 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003705 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003706 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003707 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003708 }
3709
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003710 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003711 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003712 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003713 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003714 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003715 }
3716
John McCall711c52b2011-01-05 12:14:39 +00003717 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003718 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003719 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003720 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003721 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003722 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003723 }
3724
John McCall711c52b2011-01-05 12:14:39 +00003725 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003726}
3727
Chandler Carruth1b03c872011-07-02 00:01:44 +00003728static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003729 if (S.LangOpts.CUDA) {
3730 // check the attribute arguments.
3731 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003732 // FIXME: 0 is not okay.
3733 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003734 return;
3735 }
3736
Chandler Carruth87c44602011-07-01 23:49:12 +00003737 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003739 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003740 return;
3741 }
3742
3743 Expr *MaxThreadsExpr = Attr.getArg(0);
3744 llvm::APSInt MaxThreads(32);
3745 if (MaxThreadsExpr->isTypeDependent() ||
3746 MaxThreadsExpr->isValueDependent() ||
3747 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3748 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3749 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3750 return;
3751 }
3752
3753 llvm::APSInt MinBlocks(32);
3754 if (Attr.getNumArgs() > 1) {
3755 Expr *MinBlocksExpr = Attr.getArg(1);
3756 if (MinBlocksExpr->isTypeDependent() ||
3757 MinBlocksExpr->isValueDependent() ||
3758 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3759 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3760 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3761 return;
3762 }
3763 }
3764
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003765 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003766 MaxThreads.getZExtValue(),
3767 MinBlocks.getZExtValue()));
3768 } else {
3769 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3770 }
3771}
3772
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003773static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3774 const AttributeList &Attr) {
3775 StringRef AttrName = Attr.getName()->getName();
3776 if (!Attr.getParameterName()) {
3777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3778 << Attr.getName() << /* arg num = */ 1;
3779 return;
3780 }
3781
3782 if (Attr.getNumArgs() != 2) {
3783 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3784 << /* required args = */ 3;
3785 return;
3786 }
3787
3788 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3789
3790 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3792 << Attr.getName() << ExpectedFunctionOrMethod;
3793 return;
3794 }
3795
3796 uint64_t ArgumentIdx;
3797 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3798 Attr.getLoc(), 2,
3799 Attr.getArg(0), ArgumentIdx))
3800 return;
3801
3802 uint64_t TypeTagIdx;
3803 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3804 Attr.getLoc(), 3,
3805 Attr.getArg(1), TypeTagIdx))
3806 return;
3807
3808 bool IsPointer = (AttrName == "pointer_with_type_tag");
3809 if (IsPointer) {
3810 // Ensure that buffer has a pointer type.
3811 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3812 if (!BufferTy->isPointerType()) {
3813 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3814 << AttrName;
3815 }
3816 }
3817
3818 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3819 S.Context,
3820 ArgumentKind,
3821 ArgumentIdx,
3822 TypeTagIdx,
3823 IsPointer));
3824}
3825
3826static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3827 const AttributeList &Attr) {
3828 IdentifierInfo *PointerKind = Attr.getParameterName();
3829 if (!PointerKind) {
3830 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3831 << "type_tag_for_datatype" << 1;
3832 return;
3833 }
3834
3835 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3836
3837 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3838 Attr.getRange(),
3839 S.Context,
3840 PointerKind,
3841 MatchingCType,
3842 Attr.getLayoutCompatible(),
3843 Attr.getMustBeNull()));
3844}
3845
Chris Lattner0744e5f2008-06-29 00:23:49 +00003846//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003847// Checker-specific attribute handlers.
3848//===----------------------------------------------------------------------===//
3849
John McCallc7ad3812011-01-25 03:31:58 +00003850static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003851 return type->isDependentType() ||
3852 type->isObjCObjectPointerType() ||
3853 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003854}
3855static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003856 return type->isDependentType() ||
3857 type->isPointerType() ||
3858 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003859}
3860
Chandler Carruth1b03c872011-07-02 00:01:44 +00003861static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003862 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003863 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003864 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003865 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003866 return;
3867 }
3868
3869 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003870 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003871 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3872 cf = false;
3873 } else {
3874 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3875 cf = true;
3876 }
3877
3878 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003879 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003880 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003881 return;
3882 }
3883
3884 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003885 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003886 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003887 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003888}
3889
Chandler Carruth1b03c872011-07-02 00:01:44 +00003890static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3891 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003892 if (!isa<ObjCMethodDecl>(D)) {
3893 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003894 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003895 return;
3896 }
3897
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003898 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003899}
3900
Chandler Carruth1b03c872011-07-02 00:01:44 +00003901static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3902 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003903
John McCallc7ad3812011-01-25 03:31:58 +00003904 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003905
Chandler Carruth87c44602011-07-01 23:49:12 +00003906 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003907 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003908 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003909 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003910 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003911 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3912 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003913 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003914 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003915 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003916 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003917 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003918 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003919 return;
3920 }
Mike Stumpbf916502009-07-24 19:02:52 +00003921
John McCallc7ad3812011-01-25 03:31:58 +00003922 bool typeOK;
3923 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003924 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003925 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003926 case AttributeList::AT_NSReturnsAutoreleased:
3927 case AttributeList::AT_NSReturnsRetained:
3928 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003929 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3930 cf = false;
3931 break;
3932
Sean Hunt8e083e72012-06-19 23:57:03 +00003933 case AttributeList::AT_CFReturnsRetained:
3934 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003935 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3936 cf = true;
3937 break;
3938 }
3939
3940 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003941 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003942 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003943 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003944 }
Mike Stumpbf916502009-07-24 19:02:52 +00003945
Chandler Carruth87c44602011-07-01 23:49:12 +00003946 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003947 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003948 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003949 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003950 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003951 S.Context));
3952 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003953 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003954 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003955 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003956 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003957 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003958 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003959 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003960 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003961 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003962 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003963 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003964 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003965 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003966 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003967 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003968 return;
3969 };
3970}
3971
John McCalldc7c5ad2011-07-22 08:53:00 +00003972static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3973 const AttributeList &attr) {
3974 SourceLocation loc = attr.getLoc();
3975
3976 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3977
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003978 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003979 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003980 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003981 return;
3982 }
3983
3984 // Check that the method returns a normal pointer.
3985 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003986
3987 if (!resultType->isReferenceType() &&
3988 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003989 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3990 << SourceRange(loc)
3991 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3992
3993 // Drop the attribute.
3994 return;
3995 }
3996
3997 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003998 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003999}
4000
Fariborz Jahanian84101132012-09-07 23:46:23 +00004001static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4002 const AttributeList &attr) {
4003 SourceLocation loc = attr.getLoc();
4004 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4005
4006 if (!method) {
4007 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4008 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4009 return;
4010 }
4011 DeclContext *DC = method->getDeclContext();
4012 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4013 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4014 << attr.getName() << 0;
4015 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4016 return;
4017 }
4018 if (method->getMethodFamily() == OMF_dealloc) {
4019 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4020 << attr.getName() << 1;
4021 return;
4022 }
4023
4024 method->addAttr(
4025 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4026}
4027
John McCall8dfac0b2011-09-30 05:12:12 +00004028/// Handle cf_audited_transfer and cf_unknown_transfer.
4029static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4030 if (!isa<FunctionDecl>(D)) {
4031 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004032 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004033 return;
4034 }
4035
Sean Hunt8e083e72012-06-19 23:57:03 +00004036 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004037
4038 // Check whether there's a conflicting attribute already present.
4039 Attr *Existing;
4040 if (IsAudited) {
4041 Existing = D->getAttr<CFUnknownTransferAttr>();
4042 } else {
4043 Existing = D->getAttr<CFAuditedTransferAttr>();
4044 }
4045 if (Existing) {
4046 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4047 << A.getName()
4048 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4049 << A.getRange() << Existing->getRange();
4050 return;
4051 }
4052
4053 // All clear; add the attribute.
4054 if (IsAudited) {
4055 D->addAttr(
4056 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4057 } else {
4058 D->addAttr(
4059 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4060 }
4061}
4062
John McCallfe98da02011-09-29 07:17:38 +00004063static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4064 const AttributeList &Attr) {
4065 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4066 if (!RD || RD->isUnion()) {
4067 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004068 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004069 }
4070
4071 IdentifierInfo *ParmName = Attr.getParameterName();
4072
4073 // In Objective-C, verify that the type names an Objective-C type.
4074 // We don't want to check this outside of ObjC because people sometimes
4075 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004076 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004077 // Check for an existing type with this name.
4078 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4079 Sema::LookupOrdinaryName);
4080 if (S.LookupName(R, Sc)) {
4081 NamedDecl *Target = R.getFoundDecl();
4082 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4083 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4084 S.Diag(Target->getLocStart(), diag::note_declared_at);
4085 }
4086 }
4087 }
4088
4089 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4090 ParmName));
4091}
4092
Chandler Carruth1b03c872011-07-02 00:01:44 +00004093static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4094 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004095 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004096
Chandler Carruth87c44602011-07-01 23:49:12 +00004097 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004098 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004099}
4100
Chandler Carruth1b03c872011-07-02 00:01:44 +00004101static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4102 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004103 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004104 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004105 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004106 return;
4107 }
4108
Chandler Carruth87c44602011-07-01 23:49:12 +00004109 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004110 QualType type = vd->getType();
4111
4112 if (!type->isDependentType() &&
4113 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004114 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004115 << type;
4116 return;
4117 }
4118
4119 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4120
4121 // If we have no lifetime yet, check the lifetime we're presumably
4122 // going to infer.
4123 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4124 lifetime = type->getObjCARCImplicitLifetime();
4125
4126 switch (lifetime) {
4127 case Qualifiers::OCL_None:
4128 assert(type->isDependentType() &&
4129 "didn't infer lifetime for non-dependent type?");
4130 break;
4131
4132 case Qualifiers::OCL_Weak: // meaningful
4133 case Qualifiers::OCL_Strong: // meaningful
4134 break;
4135
4136 case Qualifiers::OCL_ExplicitNone:
4137 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004139 << (lifetime == Qualifiers::OCL_Autoreleasing);
4140 break;
4141 }
4142
Chandler Carruth87c44602011-07-01 23:49:12 +00004143 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004144 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004145}
4146
Francois Pichet11542142010-12-19 06:50:37 +00004147//===----------------------------------------------------------------------===//
4148// Microsoft specific attribute handlers.
4149//===----------------------------------------------------------------------===//
4150
Chandler Carruth1b03c872011-07-02 00:01:44 +00004151static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004152 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004153 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004154 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004155 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004156
Francois Pichet11542142010-12-19 06:50:37 +00004157 Expr *Arg = Attr.getArg(0);
4158 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004159 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4161 << "uuid" << 1;
4162 return;
4163 }
4164
Chris Lattner5f9e2722011-07-23 10:55:15 +00004165 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004166
4167 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4168 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004169
Francois Pichetd3d3be92010-12-20 01:41:49 +00004170 // Validate GUID length.
4171 if (IsCurly && StrRef.size() != 38) {
4172 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4173 return;
4174 }
4175 if (!IsCurly && StrRef.size() != 36) {
4176 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4177 return;
4178 }
4179
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004180 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004181 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004182 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004183 if (IsCurly) // Skip the optional '{'
4184 ++I;
4185
4186 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004187 if (i == 8 || i == 13 || i == 18 || i == 23) {
4188 if (*I != '-') {
4189 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4190 return;
4191 }
4192 } else if (!isxdigit(*I)) {
4193 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4194 return;
4195 }
4196 I++;
4197 }
Francois Pichet11542142010-12-19 06:50:37 +00004198
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004199 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004200 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004201 } else
Francois Pichet11542142010-12-19 06:50:37 +00004202 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004203}
4204
John McCallc052dbb2012-05-22 21:28:12 +00004205static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Joao Matos679fc932012-09-04 17:18:12 +00004206 if (S.LangOpts.MicrosoftExt) {
4207 AttributeList::Kind Kind = Attr.getKind();
4208 if (Kind == AttributeList::AT_SingleInheritance)
4209 D->addAttr(
4210 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4211 else if (Kind == AttributeList::AT_MultipleInheritance)
4212 D->addAttr(
4213 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4214 else if (Kind == AttributeList::AT_VirtualInheritance)
4215 D->addAttr(
4216 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4217 } else
John McCallc052dbb2012-05-22 21:28:12 +00004218 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4219}
4220
4221static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4222 if (S.LangOpts.MicrosoftExt) {
4223 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004224 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004225 D->addAttr(
4226 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004227 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004228 D->addAttr(
4229 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004230 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004231 D->addAttr(
4232 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4233 } else
4234 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4235}
4236
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004237static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4238 if (S.LangOpts.MicrosoftExt)
4239 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4240 else
4241 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4242}
4243
Ted Kremenekb71368d2009-05-09 02:44:38 +00004244//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004245// Top Level Sema Entry Points
4246//===----------------------------------------------------------------------===//
4247
Chandler Carruth1b03c872011-07-02 00:01:44 +00004248static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4249 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004250 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004251 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4252 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4253 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004254 default:
4255 break;
4256 }
4257}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004258
Chandler Carruth1b03c872011-07-02 00:01:44 +00004259static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4260 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004261 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004262 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4263 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4264 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004265 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004266 case AttributeList::AT_AddressSpace:
4267 case AttributeList::AT_OpenCLImageAccess:
4268 case AttributeList::AT_ObjCGC:
4269 case AttributeList::AT_VectorSize:
4270 case AttributeList::AT_NeonVectorType:
4271 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004272 // Ignore these, these are type attributes, handled by
4273 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004274 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004275 case AttributeList::AT_CUDADevice:
4276 case AttributeList::AT_CUDAHost:
4277 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004278 // Ignore, this is a non-inheritable attribute, handled
4279 // by ProcessNonInheritableDeclAttr.
4280 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004281 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4282 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4283 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4284 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004285 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004286 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004287 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004288 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004289 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4290 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4291 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004292 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004293 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4294 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4295 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4296 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004297 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4298 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004299 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4300 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004301 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004302 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004303 case AttributeList::AT_MinSize:
4304 handleMinSizeAttr(S, D, Attr);
4305 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004306 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4307 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4308 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4309 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4310 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004311 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004312 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004313 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4314 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4315 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4316 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4317 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004318 case AttributeList::AT_ownership_returns:
4319 case AttributeList::AT_ownership_takes:
4320 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004321 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004322 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4323 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4324 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4325 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4326 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4327 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4328 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004329
Sean Hunt8e083e72012-06-19 23:57:03 +00004330 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004331 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004332 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004333 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004334
Sean Hunt8e083e72012-06-19 23:57:03 +00004335 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004336 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4337
Fariborz Jahanian84101132012-09-07 23:46:23 +00004338 case AttributeList::AT_ObjCRequiresSuper:
4339 handleObjCRequiresSuperAttr(S, D, Attr); break;
4340
Sean Hunt8e083e72012-06-19 23:57:03 +00004341 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004342 handleNSBridgedAttr(S, scope, D, Attr); break;
4343
Sean Hunt8e083e72012-06-19 23:57:03 +00004344 case AttributeList::AT_CFAuditedTransfer:
4345 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004346 handleCFTransferAttr(S, D, Attr); break;
4347
Ted Kremenekb71368d2009-05-09 02:44:38 +00004348 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004349 case AttributeList::AT_CFConsumed:
4350 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4351 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004352 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004353
Sean Hunt8e083e72012-06-19 23:57:03 +00004354 case AttributeList::AT_NSReturnsAutoreleased:
4355 case AttributeList::AT_NSReturnsNotRetained:
4356 case AttributeList::AT_CFReturnsNotRetained:
4357 case AttributeList::AT_NSReturnsRetained:
4358 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004359 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004360
Tanya Lattner0df579e2012-07-09 22:06:01 +00004361 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004362 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004363 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004364
Sean Hunt8e083e72012-06-19 23:57:03 +00004365 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004366 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004367
Sean Hunt8e083e72012-06-19 23:57:03 +00004368 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4369 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4370 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004371 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4372 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004374 handleArcWeakrefUnavailableAttr (S, D, Attr);
4375 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004376 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004377 handleObjCRootClassAttr(S, D, Attr);
4378 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004379 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004380 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004381 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004382 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4383 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004384 handleReturnsTwiceAttr(S, D, Attr);
4385 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004386 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4387 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4388 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004389 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004390 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4391 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4392 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4393 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004394 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004395 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004396 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004397 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004398 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004399 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004400 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004401 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004402 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4403 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4404 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4405 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4406 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4407 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4408 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4409 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4410 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004411 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004412 // Just ignore
4413 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004414 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004415 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004416 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004417 case AttributeList::AT_StdCall:
4418 case AttributeList::AT_CDecl:
4419 case AttributeList::AT_FastCall:
4420 case AttributeList::AT_ThisCall:
4421 case AttributeList::AT_Pascal:
4422 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004423 case AttributeList::AT_PnaclCall:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004424 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004425 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004426 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004427 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004428 break;
John McCallc052dbb2012-05-22 21:28:12 +00004429
4430 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004431 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004432 handleMsStructAttr(S, D, Attr);
4433 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004434 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004435 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004436 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004437 case AttributeList::AT_SingleInheritance:
4438 case AttributeList::AT_MultipleInheritance:
4439 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004440 handleInheritanceAttr(S, D, Attr);
4441 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004442 case AttributeList::AT_Win64:
4443 case AttributeList::AT_Ptr32:
4444 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004445 handlePortabilityAttr(S, D, Attr);
4446 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004447 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004448 handleForceInlineAttr(S, D, Attr);
4449 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004450
4451 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004452 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004453 handleGuardedVarAttr(S, D, Attr);
4454 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004455 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004456 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004457 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004458 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004459 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004460 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004461 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004462 handleNoAddressSafetyAttr(S, D, Attr);
4463 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004464 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004465 handleNoThreadSafetyAttr(S, D, Attr);
4466 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004467 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004468 handleLockableAttr(S, D, Attr);
4469 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004470 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004471 handleGuardedByAttr(S, D, Attr);
4472 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004473 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004474 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004475 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004476 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004477 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004478 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004479 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004480 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004481 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004482 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004483 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004484 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004485 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004486 handleLockReturnedAttr(S, D, Attr);
4487 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004488 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004489 handleLocksExcludedAttr(S, D, Attr);
4490 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004491 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004492 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004493 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004494 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004495 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004496 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004497 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004498 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004499 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004500 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004501 handleUnlockFunAttr(S, D, Attr);
4502 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004503 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004504 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004505 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004506 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004507 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004508 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004509
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004510 // Type safety attributes.
4511 case AttributeList::AT_ArgumentWithTypeTag:
4512 handleArgumentWithTypeTagAttr(S, D, Attr);
4513 break;
4514 case AttributeList::AT_TypeTagForDatatype:
4515 handleTypeTagForDatatypeAttr(S, D, Attr);
4516 break;
4517
Chris Lattner803d0802008-06-29 00:43:07 +00004518 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004519 // Ask target about the attribute.
4520 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4521 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004522 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4523 diag::warn_unhandled_ms_attribute_ignored :
4524 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004525 break;
4526 }
4527}
4528
Peter Collingbourne60700392011-01-21 02:08:45 +00004529/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4530/// the attribute applies to decls. If the attribute is a type attribute, just
4531/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4532/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004533static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4534 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004535 bool NonInheritable, bool Inheritable) {
4536 if (Attr.isInvalid())
4537 return;
4538
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004539 // Type attributes are still treated as declaration attributes by
4540 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4541 // want to process them, however, because we will simply warn about ignoring
4542 // them. So instead, we will bail out early.
4543 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004544 return;
4545
4546 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004547 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004548
4549 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004550 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004551}
4552
Chris Lattner803d0802008-06-29 00:43:07 +00004553/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4554/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004555void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004556 const AttributeList *AttrList,
4557 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004558 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004559 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004560 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004561
4562 // GCC accepts
4563 // static int a9 __attribute__((weakref));
4564 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004565 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004566 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004567 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004568 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004569 }
4570}
4571
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004572// Annotation attributes are the only attributes allowed after an access
4573// specifier.
4574bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4575 const AttributeList *AttrList) {
4576 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004577 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004578 handleAnnotateAttr(*this, ASDecl, *l);
4579 } else {
4580 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4581 return true;
4582 }
4583 }
4584
4585 return false;
4586}
4587
John McCalle82247a2011-10-01 05:17:03 +00004588/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4589/// contains any decl attributes that we should warn about.
4590static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4591 for ( ; A; A = A->getNext()) {
4592 // Only warn if the attribute is an unignored, non-type attribute.
4593 if (A->isUsedAsTypeAttr()) continue;
4594 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4595
4596 if (A->getKind() == AttributeList::UnknownAttribute) {
4597 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4598 << A->getName() << A->getRange();
4599 } else {
4600 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4601 << A->getName() << A->getRange();
4602 }
4603 }
4604}
4605
4606/// checkUnusedDeclAttributes - Given a declarator which is not being
4607/// used to build a declaration, complain about any decl attributes
4608/// which might be lying around on it.
4609void Sema::checkUnusedDeclAttributes(Declarator &D) {
4610 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4611 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4612 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4613 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4614}
4615
Ryan Flynne25ff832009-07-30 03:15:39 +00004616/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004617/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004618NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4619 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004620 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004621 NamedDecl *NewD = 0;
4622 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004623 FunctionDecl *NewFD;
4624 // FIXME: Missing call to CheckFunctionDeclaration().
4625 // FIXME: Mangling?
4626 // FIXME: Is the qualifier info correct?
4627 // FIXME: Is the DeclContext correct?
4628 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4629 Loc, Loc, DeclarationName(II),
4630 FD->getType(), FD->getTypeSourceInfo(),
4631 SC_None, SC_None,
4632 false/*isInlineSpecified*/,
4633 FD->hasPrototype(),
4634 false/*isConstexprSpecified*/);
4635 NewD = NewFD;
4636
4637 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004638 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004639
4640 // Fake up parameter variables; they are declared as if this were
4641 // a typedef.
4642 QualType FDTy = FD->getType();
4643 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4644 SmallVector<ParmVarDecl*, 16> Params;
4645 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4646 AE = FT->arg_type_end(); AI != AE; ++AI) {
4647 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4648 Param->setScopeInfo(0, Params.size());
4649 Params.push_back(Param);
4650 }
David Blaikie4278c652011-09-21 18:16:56 +00004651 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004652 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004653 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4654 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004655 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004656 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004657 VD->getStorageClass(),
4658 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004659 if (VD->getQualifier()) {
4660 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004661 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004662 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004663 }
4664 return NewD;
4665}
4666
James Dennett1dfbd922012-06-14 21:40:34 +00004667/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004668/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004669void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004670 if (W.getUsed()) return; // only do this once
4671 W.setUsed(true);
4672 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4673 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004674 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004675 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4676 NDId->getName()));
4677 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004678 WeakTopLevelDecl.push_back(NewD);
4679 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4680 // to insert Decl at TU scope, sorry.
4681 DeclContext *SavedContext = CurContext;
4682 CurContext = Context.getTranslationUnitDecl();
4683 PushOnScopeChains(NewD, S);
4684 CurContext = SavedContext;
4685 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004686 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004687 }
4688}
4689
Chris Lattner0744e5f2008-06-29 00:23:49 +00004690/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4691/// it, apply them to D. This is a bit tricky because PD can have attributes
4692/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004693void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4694 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004695 // It's valid to "forward-declare" #pragma weak, in which case we
4696 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004697 if (Inheritable) {
4698 LoadExternalWeakUndeclaredIdentifiers();
4699 if (!WeakUndeclaredIdentifiers.empty()) {
4700 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4701 if (IdentifierInfo *Id = ND->getIdentifier()) {
4702 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4703 = WeakUndeclaredIdentifiers.find(Id);
4704 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4705 WeakInfo W = I->second;
4706 DeclApplyPragmaWeak(S, ND, W);
4707 WeakUndeclaredIdentifiers[Id] = W;
4708 }
John McCalld4aff0e2010-10-27 00:59:00 +00004709 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004710 }
4711 }
4712 }
4713
Chris Lattner0744e5f2008-06-29 00:23:49 +00004714 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004715 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004716 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004717
Chris Lattner0744e5f2008-06-29 00:23:49 +00004718 // Walk the declarator structure, applying decl attributes that were in a type
4719 // position to the decl itself. This handles cases like:
4720 // int *__attr__(x)** D;
4721 // when X is a decl attribute.
4722 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4723 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004724 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004725
Chris Lattner0744e5f2008-06-29 00:23:49 +00004726 // Finally, apply any attributes on the decl itself.
4727 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004728 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004729}
John McCall54abf7d2009-11-04 02:18:39 +00004730
John McCallf85e1932011-06-15 23:02:42 +00004731/// Is the given declaration allowed to use a forbidden type?
4732static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4733 // Private ivars are always okay. Unfortunately, people don't
4734 // always properly make their ivars private, even in system headers.
4735 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004736 // Function declarations in sys headers will be marked unavailable.
4737 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4738 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004739 return false;
4740
4741 // Require it to be declared in a system header.
4742 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4743}
4744
4745/// Handle a delayed forbidden-type diagnostic.
4746static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4747 Decl *decl) {
4748 if (decl && isForbiddenTypeAllowed(S, decl)) {
4749 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4750 "this system declaration uses an unsupported type"));
4751 return;
4752 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004753 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004754 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004755 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004756 // kind of forbidden type messages on unavailable functions.
4757 if (FD->hasAttr<UnavailableAttr>() &&
4758 diag.getForbiddenTypeDiagnostic() ==
4759 diag::err_arc_array_param_no_ownership) {
4760 diag.Triggered = true;
4761 return;
4762 }
4763 }
John McCallf85e1932011-06-15 23:02:42 +00004764
4765 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4766 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4767 diag.Triggered = true;
4768}
4769
John McCall92576642012-05-07 06:16:41 +00004770void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4771 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004772 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004773 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004774
John McCall92576642012-05-07 06:16:41 +00004775 // When delaying diagnostics to run in the context of a parsed
4776 // declaration, we only want to actually emit anything if parsing
4777 // succeeds.
4778 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004779
John McCall92576642012-05-07 06:16:41 +00004780 // We emit all the active diagnostics in this pool or any of its
4781 // parents. In general, we'll get one pool for the decl spec
4782 // and a child pool for each declarator; in a decl group like:
4783 // deprecated_typedef foo, *bar, baz();
4784 // only the declarator pops will be passed decls. This is correct;
4785 // we really do need to consider delayed diagnostics from the decl spec
4786 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004787 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004788 do {
John McCall13489672012-05-07 06:16:58 +00004789 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004790 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4791 // This const_cast is a bit lame. Really, Triggered should be mutable.
4792 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004793 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004794 continue;
4795
John McCalleee1d542011-02-14 07:13:47 +00004796 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004797 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004798 // Don't bother giving deprecation diagnostics if the decl is invalid.
4799 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004800 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004801 break;
4802
4803 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004804 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004805 break;
John McCallf85e1932011-06-15 23:02:42 +00004806
4807 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004808 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004809 break;
John McCall2f514482010-01-27 03:50:35 +00004810 }
4811 }
John McCall92576642012-05-07 06:16:41 +00004812 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004813}
4814
John McCall13489672012-05-07 06:16:58 +00004815/// Given a set of delayed diagnostics, re-emit them as if they had
4816/// been delayed in the current context instead of in the given pool.
4817/// Essentially, this just moves them to the current pool.
4818void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4819 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4820 assert(curPool && "re-emitting in undelayed context not supported");
4821 curPool->steal(pool);
4822}
4823
John McCall54abf7d2009-11-04 02:18:39 +00004824static bool isDeclDeprecated(Decl *D) {
4825 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004826 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004827 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004828 // A category implicitly has the availability of the interface.
4829 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4830 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004831 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4832 return false;
4833}
4834
Eli Friedmanc3b23082012-08-08 21:52:41 +00004835static void
4836DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4837 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004838 const ObjCInterfaceDecl *UnknownObjCClass,
4839 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00004840 DeclarationName Name = D->getDeclName();
4841 if (!Message.empty()) {
4842 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4843 S.Diag(D->getLocation(),
4844 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4845 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004846 if (ObjCPropery)
4847 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4848 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004849 } else if (!UnknownObjCClass) {
4850 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4851 S.Diag(D->getLocation(),
4852 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4853 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004854 if (ObjCPropery)
4855 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4856 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004857 } else {
4858 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4859 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4860 }
4861}
4862
John McCall9c3087b2010-08-26 02:13:20 +00004863void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004864 Decl *Ctx) {
4865 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004866 return;
4867
John McCall2f514482010-01-27 03:50:35 +00004868 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004869 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4870 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004871 DD.getUnknownObjCClass(),
4872 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00004873}
4874
Chris Lattner5f9e2722011-07-23 10:55:15 +00004875void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004876 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004877 const ObjCInterfaceDecl *UnknownObjCClass,
4878 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00004879 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004880 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004881 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4882 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004883 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004884 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004885 return;
4886 }
4887
4888 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004889 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004890 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004891 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00004892}