blob: 861b91420d5f7ec8fe3cebf4a72db90a2fc0d7cb [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"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000022#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000024#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000025#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000027using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000028using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029
John McCall883cc2c2011-03-02 12:29:23 +000030/// These constants match the enumerated choices of
31/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000032enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000033 ExpectedFunction,
34 ExpectedUnion,
35 ExpectedVariableOrFunction,
36 ExpectedFunctionOrMethod,
37 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000038 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000041 ExpectedVariable,
42 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000043 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000044 ExpectedFieldOrGlobalVar,
45 ExpectedStruct
John McCall883cc2c2011-03-02 12:29:23 +000046};
47
Chris Lattnere5c5ee12008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruth87c44602011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000063
Chris Lattner6b6b5372008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000068
John McCall183700f2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000070}
71
Daniel Dunbar35682492008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopesd20254f2009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000086}
87
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruth87c44602011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100}
101
John McCall711c52b2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000108}
109
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000131}
132
Chandler Carruth87c44602011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000138
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000140}
141
Chandler Carruth87c44602011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146}
147
Chandler Carruth87c44602011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000154 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000169
John McCall506b57e2010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000173
John McCall506b57e2010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000175
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000189
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000197/// \brief Check if the attribute has exactly as many args as Num. May
198/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000199static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
200 unsigned int Num) {
201 if (Attr.getNumArgs() != Num) {
202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
203 return false;
204 }
205
206 return true;
207}
208
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000209
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000210/// \brief Check if the attribute has at least as many args as Num. May
211/// output an error.
212static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
213 unsigned int Num) {
214 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000215 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
216 return false;
217 }
218
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000219 return true;
220}
221
222///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000223/// \brief Check if passed in Decl is a field or potentially shared global var
224/// \return true if the Decl is a field or potentially shared global variable
225///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000226static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000227 if (isa<FieldDecl>(D))
228 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000229 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000230 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
231
232 return false;
233}
234
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000235/// \brief Check if the passed-in expression is of type int or bool.
236static bool isIntOrBool(Expr *Exp) {
237 QualType QT = Exp->getType();
238 return QT->isBooleanType() || QT->isIntegerType();
239}
240
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000241
242// Check to see if the type is a smart pointer of some kind. We assume
243// it's a smart pointer if it defines both operator-> and operator*.
244static bool threadSafetyCheckIsSmartPointer(Sema &S, const QualType QT) {
245 if (const RecordType *RT = QT->getAs<RecordType>()) {
246 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
247 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
248 if (Res1.first == Res1.second)
249 return false;
250
251 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
252 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
253 if (Res2.first != Res2.second)
254 return true;
255 }
256 return false;
257}
258
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000259///
260/// \brief Check if passed in Decl is a pointer type.
261/// Note that this function may produce an error message.
262/// \return true if the Decl is a pointer type; false otherwise
263///
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000264static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
265 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000266 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000267 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000268 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000269 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000270
271 if (threadSafetyCheckIsSmartPointer(S, QT))
272 return true;
273
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000274 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000275 << Attr.getName()->getName() << QT;
276 } else {
277 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
278 << Attr.getName();
279 }
280 return false;
281}
282
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000283/// \brief Checks that the passed in QualType either is of RecordType or points
284/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000285static const RecordType *getRecordType(QualType QT) {
286 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000287 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000288
289 // Now check if we point to record type.
290 if (const PointerType *PT = QT->getAs<PointerType>())
291 return PT->getPointeeType()->getAs<RecordType>();
292
293 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000294}
295
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000296/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000297/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000298static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
299 QualType Ty) {
300 const RecordType *RT = getRecordType(Ty);
301
302 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000303 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000304 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000305 << Attr.getName() << Ty.getAsString();
306 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000307 }
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000308 // Don't check for lockable if the class hasn't been defined yet.
309 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000310 return;
311 // Warn if the type is not lockable.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000312 if (!RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000313 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000314 << Attr.getName() << Ty.getAsString();
315 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000316 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000317}
318
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000319/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000320/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000321/// \param Sidx The attribute argument index to start checking with.
322/// \param ParamIdxOk Whether an argument can be indexing into a function
323/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000324static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000325 const AttributeList &Attr,
326 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000327 int Sidx = 0,
328 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000329 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000330 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000331
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000332 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000333 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000334 Args.push_back(ArgExp);
335 continue;
336 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000337
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000338 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
339 // Ignore empty strings without warnings
340 if (StrLit->getLength() == 0)
341 continue;
342
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000343 // We allow constant strings to be used as a placeholder for expressions
344 // that are not valid C++ syntax, but warn that they are ignored.
345 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
346 Attr.getName();
347 continue;
348 }
349
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000350 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000351
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000352 // A pointer to member expression of the form &MyClass::mu is treated
353 // specially -- we need to look at the type of the member.
354 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
355 if (UOp->getOpcode() == UO_AddrOf)
356 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
357 if (DRE->getDecl()->isCXXInstanceMember())
358 ArgTy = DRE->getDecl()->getType();
359
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000360 // First see if we can just cast to record type, or point to record type.
361 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000362
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000363 // Now check if we index into a record type function param.
364 if(!RT && ParamIdxOk) {
365 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000366 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
367 if(FD && IL) {
368 unsigned int NumParams = FD->getNumParams();
369 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000370 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
371 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
372 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000373 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
374 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000375 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000376 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000377 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000378 }
379 }
380
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000381 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000382
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000383 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000384 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000385}
386
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000387//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000388// Attribute Implementations
389//===----------------------------------------------------------------------===//
390
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000391// FIXME: All this manual attribute parsing code is gross. At the
392// least add some helper functions to check most argument patterns (#
393// and types of args).
394
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000395static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
396 bool pointer = false) {
397 assert(!Attr.isInvalid());
398
399 if (!checkAttributeNumArgs(S, Attr, 0))
400 return;
401
402 // D must be either a member field or global (potentially shared) variable.
403 if (!mayBeSharedVariable(D)) {
404 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000405 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000406 return;
407 }
408
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000409 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000410 return;
411
412 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000413 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000414 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000415 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000416}
417
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000418static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000419 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000420 assert(!Attr.isInvalid());
421
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000422 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000423 return;
424
425 // D must be either a member field or global (potentially shared) variable.
426 if (!mayBeSharedVariable(D)) {
427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000428 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000429 return;
430 }
431
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000432 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000433 return;
434
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000435 SmallVector<Expr*, 1> Args;
436 // check that all arguments are lockable objects
437 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
438 unsigned Size = Args.size();
439 if (Size != 1)
440 return;
441 Expr *Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000442
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000443 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000444 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000445 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000446 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000447 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000448}
449
450
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000451static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
452 bool scoped = false) {
453 assert(!Attr.isInvalid());
454
455 if (!checkAttributeNumArgs(S, Attr, 0))
456 return;
457
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000458 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000459 if (!isa<CXXRecordDecl>(D)) {
460 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
461 << Attr.getName() << ExpectedClass;
462 return;
463 }
464
465 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000466 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000467 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000468 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000469}
470
471static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
472 const AttributeList &Attr) {
473 assert(!Attr.isInvalid());
474
475 if (!checkAttributeNumArgs(S, Attr, 0))
476 return;
477
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000478 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000479 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
480 << Attr.getName() << ExpectedFunctionOrMethod;
481 return;
482 }
483
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000484 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000485 S.Context));
486}
487
Kostya Serebryany71efba02012-01-24 19:25:38 +0000488static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000489 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000490 assert(!Attr.isInvalid());
491
492 if (!checkAttributeNumArgs(S, Attr, 0))
493 return;
494
495 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
496 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
497 << Attr.getName() << ExpectedFunctionOrMethod;
498 return;
499 }
500
501 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
502 S.Context));
503}
504
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000505static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
506 bool before) {
507 assert(!Attr.isInvalid());
508
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000509 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000510 return;
511
512 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000513 ValueDecl *VD = dyn_cast<ValueDecl>(D);
514 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000515 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000516 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000517 return;
518 }
519
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000520 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000521 QualType QT = VD->getType();
522 if (!QT->isDependentType()) {
523 const RecordType *RT = getRecordType(QT);
524 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000525 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000526 << Attr.getName();
527 return;
528 }
529 }
530
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000531 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000532 // Check that all arguments are lockable objects.
533 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000534 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000535 if (Size == 0)
536 return;
537 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000538
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000539 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000540 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000541 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000542 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000543 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000544 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000545}
546
547static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000548 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000549 assert(!Attr.isInvalid());
550
551 // zero or more arguments ok
552
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000553 // check that the attribute is applied to a function
554 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000555 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
556 << Attr.getName() << ExpectedFunctionOrMethod;
557 return;
558 }
559
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000560 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000561 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000562 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000563 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000564 Expr **StartArg = Size == 0 ? 0 : &Args[0];
565
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000566 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000567 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000568 S.Context, StartArg,
569 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000570 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000571 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000572 S.Context, StartArg,
573 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000574}
575
576static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000577 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000578 assert(!Attr.isInvalid());
579
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000580 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000581 return;
582
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000583 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
585 << Attr.getName() << ExpectedFunctionOrMethod;
586 return;
587 }
588
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000589 if (!isIntOrBool(Attr.getArg(0))) {
590 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
591 << Attr.getName();
592 return;
593 }
594
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000595 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000596 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000597 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000598 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000599 Expr **StartArg = Size == 0 ? 0 : &Args[0];
600
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000601 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000602 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000603 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000604 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000605 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000606 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000607 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000608 S.Context,
609 Attr.getArg(0),
610 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000611}
612
613static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000614 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000615 assert(!Attr.isInvalid());
616
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000617 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000618 return;
619
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000620 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000621 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
622 << Attr.getName() << ExpectedFunctionOrMethod;
623 return;
624 }
625
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000626 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000627 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000628 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000629 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000630 if (Size == 0)
631 return;
632 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000633
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000634 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000635 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000636 S.Context, StartArg,
637 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000638 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000639 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000640 S.Context, StartArg,
641 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000642}
643
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000644static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000645 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000646 assert(!Attr.isInvalid());
647
648 // zero or more arguments ok
649
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000650 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
652 << Attr.getName() << ExpectedFunctionOrMethod;
653 return;
654 }
655
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000656 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000657 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000658 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000659 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000660 Expr **StartArg = Size == 0 ? 0 : &Args[0];
661
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000662 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000663 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000664}
665
666static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000667 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000668 assert(!Attr.isInvalid());
669
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000670 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000671 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000672 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000673
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000674 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
676 << Attr.getName() << ExpectedFunctionOrMethod;
677 return;
678 }
679
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000680 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000681 return;
682
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000683 // check that the argument is lockable object
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000684 checkForLockableRecord(S, D, Attr, Arg->getType());
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000685
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000686 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000687}
688
689static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000690 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000691 assert(!Attr.isInvalid());
692
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000693 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000694 return;
695
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000696 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
698 << Attr.getName() << ExpectedFunctionOrMethod;
699 return;
700 }
701
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000702 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000703 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000704 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000705 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000706 if (Size == 0)
707 return;
708 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000709
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000710 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000711 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000712}
713
714
Chandler Carruth1b03c872011-07-02 00:01:44 +0000715static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
716 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000717 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000718 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000719 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000720 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000721 }
Mike Stumpbf916502009-07-24 19:02:52 +0000722
Chris Lattner6b6b5372008-06-26 18:38:35 +0000723 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000724
725 Expr *sizeExpr;
726
727 // Special case where the argument is a template id.
728 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000729 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000730 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000731 UnqualifiedId id;
732 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000733
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000734 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
735 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000736 if (Size.isInvalid())
737 return;
738
739 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000740 } else {
741 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000742 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000743 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000744
Peter Collingbourne7a730022010-11-23 20:45:58 +0000745 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000746 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000747
748 // Instantiate/Install the vector type, and let Sema build the type for us.
749 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000750 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000751 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000752 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000753 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000754
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000755 // Remember this typedef decl, we will need it later for diagnostics.
756 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000757 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000758}
759
Chandler Carruth1b03c872011-07-02 00:01:44 +0000760static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000761 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000762 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000763 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000764
Chandler Carruth87c44602011-07-01 23:49:12 +0000765 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000766 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000767 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000768 // If the alignment is less than or equal to 8 bits, the packed attribute
769 // has no effect.
770 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000771 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000772 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000773 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000774 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000775 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000776 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000777 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000778}
779
Chandler Carruth1b03c872011-07-02 00:01:44 +0000780static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000781 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000782 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000783 else
784 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
785}
786
Chandler Carruth1b03c872011-07-02 00:01:44 +0000787static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000788 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000789 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000790 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000791
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000792 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000793 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000794 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000795 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000796 return;
797 }
798
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000799 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000800}
801
Ted Kremenek2f041d02011-09-29 07:02:25 +0000802static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
803 // The IBOutlet/IBOutletCollection attributes only apply to instance
804 // variables or properties of Objective-C classes. The outlet must also
805 // have an object reference type.
806 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
807 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000808 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000809 << Attr.getName() << VD->getType() << 0;
810 return false;
811 }
812 }
813 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
814 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000815 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000816 << Attr.getName() << PD->getType() << 1;
817 return false;
818 }
819 }
820 else {
821 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
822 return false;
823 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000824
Ted Kremenek2f041d02011-09-29 07:02:25 +0000825 return true;
826}
827
Chandler Carruth1b03c872011-07-02 00:01:44 +0000828static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000829 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000830 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000831 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000832
833 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000834 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000835
Ted Kremenek2f041d02011-09-29 07:02:25 +0000836 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000837}
838
Chandler Carruth1b03c872011-07-02 00:01:44 +0000839static void handleIBOutletCollection(Sema &S, Decl *D,
840 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000841
842 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000843 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
845 return;
846 }
847
Ted Kremenek2f041d02011-09-29 07:02:25 +0000848 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000849 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000850
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000851 IdentifierInfo *II = Attr.getParameterName();
852 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000853 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000854
John McCallb3d87482010-08-24 05:47:05 +0000855 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000856 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000857 if (!TypeRep) {
858 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
859 return;
860 }
John McCallb3d87482010-08-24 05:47:05 +0000861 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000862 // Diagnose use of non-object type in iboutletcollection attribute.
863 // FIXME. Gnu attribute extension ignores use of builtin types in
864 // attributes. So, __attribute__((iboutletcollection(char))) will be
865 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000866 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000867 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
868 return;
869 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000870 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
871 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000872}
873
Chandler Carruthd309c812011-07-01 23:49:16 +0000874static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000875 if (const RecordType *UT = T->getAsUnionType())
876 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
877 RecordDecl *UD = UT->getDecl();
878 for (RecordDecl::field_iterator it = UD->field_begin(),
879 itend = UD->field_end(); it != itend; ++it) {
880 QualType QT = it->getType();
881 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
882 T = QT;
883 return;
884 }
885 }
886 }
887}
888
Chandler Carruth1b03c872011-07-02 00:01:44 +0000889static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000890 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
891 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000892 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000894 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000895 return;
896 }
Mike Stumpbf916502009-07-24 19:02:52 +0000897
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000898 // In C++ the implicit 'this' function parameter also counts, and they are
899 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000900 bool HasImplicitThisParam = isInstanceMethod(D);
901 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000902
903 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000904 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000905
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000906 for (AttributeList::arg_iterator I=Attr.arg_begin(),
907 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000908
909
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000910 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000911 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000912 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000913 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
914 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000915 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
916 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000917 return;
918 }
Mike Stumpbf916502009-07-24 19:02:52 +0000919
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000920 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000921
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000922 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000923 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000924 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000925 return;
926 }
Mike Stumpbf916502009-07-24 19:02:52 +0000927
Ted Kremenek465172f2008-07-21 22:09:15 +0000928 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000929 if (HasImplicitThisParam) {
930 if (x == 0) {
931 S.Diag(Attr.getLoc(),
932 diag::err_attribute_invalid_implicit_this_argument)
933 << "nonnull" << Ex->getSourceRange();
934 return;
935 }
936 --x;
937 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000938
939 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000940 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000941 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000942
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000943 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000944 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000945 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000946 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000947 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000948 }
Mike Stumpbf916502009-07-24 19:02:52 +0000949
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000950 NonNullArgs.push_back(x);
951 }
Mike Stumpbf916502009-07-24 19:02:52 +0000952
953 // If no arguments were specified to __attribute__((nonnull)) then all pointer
954 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000955 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000956 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
957 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000958 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000959 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000960 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000961 }
Mike Stumpbf916502009-07-24 19:02:52 +0000962
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000963 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000964 if (NonNullArgs.empty()) {
965 // Warn the trivial case only if attribute is not coming from a
966 // macro instantiation.
967 if (Attr.getLoc().isFileID())
968 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000969 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000970 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000971 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000972
973 unsigned* start = &NonNullArgs[0];
974 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000975 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000976 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000977 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000978}
979
Chandler Carruth1b03c872011-07-02 00:01:44 +0000980static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000981 // This attribute must be applied to a function declaration.
982 // The first argument to the attribute must be a string,
983 // the name of the resource, for example "malloc".
984 // The following arguments must be argument indexes, the arguments must be
985 // of integer type for Returns, otherwise of pointer type.
986 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000987 // after being held. free() should be __attribute((ownership_takes)), whereas
988 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000989
990 if (!AL.getParameterName()) {
991 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
992 << AL.getName()->getName() << 1;
993 return;
994 }
995 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000996 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000997 switch (AL.getKind()) {
998 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000999 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001000 if (AL.getNumArgs() < 1) {
1001 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1002 return;
1003 }
Jordy Rose2a479922010-08-12 08:54:03 +00001004 break;
1005 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001006 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001007 if (AL.getNumArgs() < 1) {
1008 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1009 return;
1010 }
Jordy Rose2a479922010-08-12 08:54:03 +00001011 break;
1012 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001013 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001014 if (AL.getNumArgs() > 1) {
1015 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1016 << AL.getNumArgs() + 1;
1017 return;
1018 }
Jordy Rose2a479922010-08-12 08:54:03 +00001019 break;
1020 default:
1021 // This should never happen given how we are called.
1022 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001023 }
1024
Chandler Carruth87c44602011-07-01 23:49:12 +00001025 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001026 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1027 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001028 return;
1029 }
1030
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001031 // In C++ the implicit 'this' function parameter also counts, and they are
1032 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001033 bool HasImplicitThisParam = isInstanceMethod(D);
1034 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001035
Chris Lattner5f9e2722011-07-23 10:55:15 +00001036 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001037
1038 // Normalize the argument, __foo__ becomes foo.
1039 if (Module.startswith("__") && Module.endswith("__"))
1040 Module = Module.substr(2, Module.size() - 4);
1041
Chris Lattner5f9e2722011-07-23 10:55:15 +00001042 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001043
Jordy Rose2a479922010-08-12 08:54:03 +00001044 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1045 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001046
Peter Collingbourne7a730022010-11-23 20:45:58 +00001047 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001048 llvm::APSInt ArgNum(32);
1049 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1050 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1051 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1052 << AL.getName()->getName() << IdxExpr->getSourceRange();
1053 continue;
1054 }
1055
1056 unsigned x = (unsigned) ArgNum.getZExtValue();
1057
1058 if (x > NumArgs || x < 1) {
1059 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1060 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1061 continue;
1062 }
1063 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001064 if (HasImplicitThisParam) {
1065 if (x == 0) {
1066 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1067 << "ownership" << IdxExpr->getSourceRange();
1068 return;
1069 }
1070 --x;
1071 }
1072
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001073 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001074 case OwnershipAttr::Takes:
1075 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001076 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001077 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001078 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1079 // FIXME: Should also highlight argument in decl.
1080 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001081 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001082 << "pointer"
1083 << IdxExpr->getSourceRange();
1084 continue;
1085 }
1086 break;
1087 }
Sean Huntcf807c42010-08-18 23:23:40 +00001088 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001089 if (AL.getNumArgs() > 1) {
1090 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001091 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001092 llvm::APSInt ArgNum(32);
1093 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1094 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1095 S.Diag(AL.getLoc(), diag::err_ownership_type)
1096 << "ownership_returns" << "integer"
1097 << IdxExpr->getSourceRange();
1098 return;
1099 }
1100 }
1101 break;
1102 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001103 } // switch
1104
1105 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001106 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001107 i = D->specific_attr_begin<OwnershipAttr>(),
1108 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001109 i != e; ++i) {
1110 if ((*i)->getOwnKind() != K) {
1111 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1112 I!=E; ++I) {
1113 if (x == *I) {
1114 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1115 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001116 }
1117 }
1118 }
1119 }
1120 OwnershipArgs.push_back(x);
1121 }
1122
1123 unsigned* start = OwnershipArgs.data();
1124 unsigned size = OwnershipArgs.size();
1125 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001126
1127 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1128 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1129 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001130 }
Sean Huntcf807c42010-08-18 23:23:40 +00001131
Chandler Carruth87c44602011-07-01 23:49:12 +00001132 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001133 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001134}
1135
John McCall332bb2a2011-02-08 22:35:49 +00001136/// Whether this declaration has internal linkage for the purposes of
1137/// things that want to complain about things not have internal linkage.
1138static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1139 switch (D->getLinkage()) {
1140 case NoLinkage:
1141 case InternalLinkage:
1142 return true;
1143
1144 // Template instantiations that go from external to unique-external
1145 // shouldn't get diagnosed.
1146 case UniqueExternalLinkage:
1147 return true;
1148
1149 case ExternalLinkage:
1150 return false;
1151 }
1152 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001153}
1154
Chandler Carruth1b03c872011-07-02 00:01:44 +00001155static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001156 // Check the attribute arguments.
1157 if (Attr.getNumArgs() > 1) {
1158 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1159 return;
1160 }
1161
Chandler Carruth87c44602011-07-01 23:49:12 +00001162 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001163 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001164 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001165 return;
1166 }
1167
Chandler Carruth87c44602011-07-01 23:49:12 +00001168 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001169
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001170 // gcc rejects
1171 // class c {
1172 // static int a __attribute__((weakref ("v2")));
1173 // static int b() __attribute__((weakref ("f3")));
1174 // };
1175 // and ignores the attributes of
1176 // void f(void) {
1177 // static int a __attribute__((weakref ("v2")));
1178 // }
1179 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001180 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001181 if (!Ctx->isFileContext()) {
1182 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001183 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001184 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001185 }
1186
1187 // The GCC manual says
1188 //
1189 // At present, a declaration to which `weakref' is attached can only
1190 // be `static'.
1191 //
1192 // It also says
1193 //
1194 // Without a TARGET,
1195 // given as an argument to `weakref' or to `alias', `weakref' is
1196 // equivalent to `weak'.
1197 //
1198 // gcc 4.4.1 will accept
1199 // int a7 __attribute__((weakref));
1200 // as
1201 // int a7 __attribute__((weak));
1202 // This looks like a bug in gcc. We reject that for now. We should revisit
1203 // it if this behaviour is actually used.
1204
John McCall332bb2a2011-02-08 22:35:49 +00001205 if (!hasEffectivelyInternalLinkage(nd)) {
1206 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001207 return;
1208 }
1209
1210 // GCC rejects
1211 // static ((alias ("y"), weakref)).
1212 // Should we? How to check that weakref is before or after alias?
1213
1214 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001215 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001216 Arg = Arg->IgnoreParenCasts();
1217 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1218
Douglas Gregor5cee1192011-07-27 05:40:30 +00001219 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001220 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1221 << "weakref" << 1;
1222 return;
1223 }
1224 // GCC will accept anything as the argument of weakref. Should we
1225 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001226 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001227 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001228 }
1229
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001230 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001231}
1232
Chandler Carruth1b03c872011-07-02 00:01:44 +00001233static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001234 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001235 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001236 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001237 return;
1238 }
Mike Stumpbf916502009-07-24 19:02:52 +00001239
Peter Collingbourne7a730022010-11-23 20:45:58 +00001240 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001241 Arg = Arg->IgnoreParenCasts();
1242 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001243
Douglas Gregor5cee1192011-07-27 05:40:30 +00001244 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001246 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001247 return;
1248 }
Mike Stumpbf916502009-07-24 19:02:52 +00001249
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001250 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001251 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1252 return;
1253 }
1254
Chris Lattner6b6b5372008-06-26 18:38:35 +00001255 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001256
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001257 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001258 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001259}
1260
Chandler Carruth1b03c872011-07-02 00:01:44 +00001261static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001262 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001263 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001264 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001265
Chandler Carruth87c44602011-07-01 23:49:12 +00001266 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001267 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001268 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001269 return;
1270 }
1271
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001272 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001273}
1274
Chandler Carruth1b03c872011-07-02 00:01:44 +00001275static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1276 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001277 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001278 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1280 return;
1281 }
1282
Chandler Carruth87c44602011-07-01 23:49:12 +00001283 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001284 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001285 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001286 return;
1287 }
Mike Stumpbf916502009-07-24 19:02:52 +00001288
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001289 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001290}
1291
Chandler Carruth1b03c872011-07-02 00:01:44 +00001292static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001293 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001294 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001295 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1296 return;
1297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Chandler Carruth87c44602011-07-01 23:49:12 +00001299 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001300 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001301 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001302 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001303 return;
1304 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001305 }
1306
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001307 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001308}
1309
Chandler Carruth1b03c872011-07-02 00:01:44 +00001310static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001311 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001312 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001313 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001314
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001315 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001316}
1317
Chandler Carruth1b03c872011-07-02 00:01:44 +00001318static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001319 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001320 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001321 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001322 else
1323 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001324 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001325}
1326
Chandler Carruth1b03c872011-07-02 00:01:44 +00001327static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001328 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001329 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001330 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001331 else
1332 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001333 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001334}
1335
Chandler Carruth1b03c872011-07-02 00:01:44 +00001336static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001337 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001338
1339 if (S.CheckNoReturnAttr(attr)) return;
1340
Chandler Carruth87c44602011-07-01 23:49:12 +00001341 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001342 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001343 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001344 return;
1345 }
1346
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001347 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001348}
1349
1350bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001351 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001352 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1353 attr.setInvalid();
1354 return true;
1355 }
1356
1357 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001358}
1359
Chandler Carruth1b03c872011-07-02 00:01:44 +00001360static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1361 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001362
1363 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1364 // because 'analyzer_noreturn' does not impact the type.
1365
Chandler Carruth1731e202011-07-11 23:30:35 +00001366 if(!checkAttributeNumArgs(S, Attr, 0))
1367 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001368
Chandler Carruth87c44602011-07-01 23:49:12 +00001369 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1370 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001371 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1372 && !VD->getType()->isFunctionPointerType())) {
1373 S.Diag(Attr.getLoc(),
1374 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1375 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001376 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001377 return;
1378 }
1379 }
1380
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001381 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001382}
1383
John Thompson35cc9622010-08-09 21:53:52 +00001384// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001385static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001386/*
1387 Returning a Vector Class in Registers
1388
Eric Christopherf48f3672010-12-01 22:13:54 +00001389 According to the PPU ABI specifications, a class with a single member of
1390 vector type is returned in memory when used as the return value of a function.
1391 This results in inefficient code when implementing vector classes. To return
1392 the value in a single vector register, add the vecreturn attribute to the
1393 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001394
1395 Example:
1396
1397 struct Vector
1398 {
1399 __vector float xyzw;
1400 } __attribute__((vecreturn));
1401
1402 Vector Add(Vector lhs, Vector rhs)
1403 {
1404 Vector result;
1405 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1406 return result; // This will be returned in a register
1407 }
1408*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001409 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001411 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001412 return;
1413 }
1414
Chandler Carruth87c44602011-07-01 23:49:12 +00001415 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001416 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1417 return;
1418 }
1419
Chandler Carruth87c44602011-07-01 23:49:12 +00001420 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001421 int count = 0;
1422
1423 if (!isa<CXXRecordDecl>(record)) {
1424 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1425 return;
1426 }
1427
1428 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1429 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1430 return;
1431 }
1432
Eric Christopherf48f3672010-12-01 22:13:54 +00001433 for (RecordDecl::field_iterator iter = record->field_begin();
1434 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001435 if ((count == 1) || !iter->getType()->isVectorType()) {
1436 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1437 return;
1438 }
1439 count++;
1440 }
1441
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001442 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001443}
1444
Chandler Carruth1b03c872011-07-02 00:01:44 +00001445static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001446 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001448 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001449 return;
1450 }
1451 // FIXME: Actually store the attribute on the declaration
1452}
1453
Chandler Carruth1b03c872011-07-02 00:01:44 +00001454static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001455 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001456 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001458 return;
1459 }
Mike Stumpbf916502009-07-24 19:02:52 +00001460
Chandler Carruth87c44602011-07-01 23:49:12 +00001461 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1462 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001464 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001465 return;
1466 }
Mike Stumpbf916502009-07-24 19:02:52 +00001467
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001468 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001469}
1470
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001471static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1472 const AttributeList &Attr) {
1473 // check the attribute arguments.
1474 if (Attr.hasParameterOrArguments()) {
1475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1476 return;
1477 }
1478
1479 if (!isa<FunctionDecl>(D)) {
1480 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1481 << Attr.getName() << ExpectedFunction;
1482 return;
1483 }
1484
1485 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1486}
1487
Chandler Carruth1b03c872011-07-02 00:01:44 +00001488static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001489 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001490 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1492 return;
1493 }
Mike Stumpbf916502009-07-24 19:02:52 +00001494
Chandler Carruth87c44602011-07-01 23:49:12 +00001495 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001496 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001497 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1498 return;
1499 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001500 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001502 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001503 return;
1504 }
Mike Stumpbf916502009-07-24 19:02:52 +00001505
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001506 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001507}
1508
Chandler Carruth1b03c872011-07-02 00:01:44 +00001509static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001510 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001511 if (Attr.getNumArgs() > 1) {
1512 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001513 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001514 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001515
1516 int priority = 65535; // FIXME: Do not hardcode such constants.
1517 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001518 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001519 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001520 if (E->isTypeDependent() || E->isValueDependent() ||
1521 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001522 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001523 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001524 return;
1525 }
1526 priority = Idx.getZExtValue();
1527 }
Mike Stumpbf916502009-07-24 19:02:52 +00001528
Chandler Carruth87c44602011-07-01 23:49:12 +00001529 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001531 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001532 return;
1533 }
1534
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001535 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001536 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001537}
1538
Chandler Carruth1b03c872011-07-02 00:01:44 +00001539static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001540 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001541 if (Attr.getNumArgs() > 1) {
1542 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001543 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001544 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001545
1546 int priority = 65535; // FIXME: Do not hardcode such constants.
1547 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001548 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001549 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001550 if (E->isTypeDependent() || E->isValueDependent() ||
1551 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001553 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001554 return;
1555 }
1556 priority = Idx.getZExtValue();
1557 }
Mike Stumpbf916502009-07-24 19:02:52 +00001558
Chandler Carruth87c44602011-07-01 23:49:12 +00001559 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001560 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001561 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001562 return;
1563 }
1564
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001565 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001566 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001567}
1568
Chandler Carruth1b03c872011-07-02 00:01:44 +00001569static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001570 unsigned NumArgs = Attr.getNumArgs();
1571 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001573 return;
1574 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001575
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001576 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001577 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001578 if (NumArgs == 1) {
1579 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001580 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001581 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1582 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001583 return;
1584 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001585 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001586 }
Mike Stumpbf916502009-07-24 19:02:52 +00001587
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001588 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001589}
1590
Chandler Carruth1b03c872011-07-02 00:01:44 +00001591static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001592 unsigned NumArgs = Attr.getNumArgs();
1593 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001594 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001595 return;
1596 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001597
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001598 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001599 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001600 if (NumArgs == 1) {
1601 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001602 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001603 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001604 diag::err_attribute_not_string) << "unavailable";
1605 return;
1606 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001607 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001608 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001609 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001610}
1611
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001612static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1613 const AttributeList &Attr) {
1614 unsigned NumArgs = Attr.getNumArgs();
1615 if (NumArgs > 0) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1617 return;
1618 }
1619
1620 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001621 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001622}
1623
Patrick Beardb2f68202012-04-06 18:12:22 +00001624static void handleObjCRootClassAttr(Sema &S, Decl *D,
1625 const AttributeList &Attr) {
1626 if (!isa<ObjCInterfaceDecl>(D)) {
1627 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1628 return;
1629 }
1630
1631 unsigned NumArgs = Attr.getNumArgs();
1632 if (NumArgs > 0) {
1633 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1634 return;
1635 }
1636
1637 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1638}
1639
Ted Kremenek71207fc2012-01-05 22:47:47 +00001640static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001641 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001642 if (!isa<ObjCInterfaceDecl>(D)) {
1643 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1644 return;
1645 }
1646
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001647 unsigned NumArgs = Attr.getNumArgs();
1648 if (NumArgs > 0) {
1649 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1650 return;
1651 }
1652
Ted Kremenek71207fc2012-01-05 22:47:47 +00001653 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001654 Attr.getRange(), S.Context));
1655}
1656
Chandler Carruth1b03c872011-07-02 00:01:44 +00001657static void handleAvailabilityAttr(Sema &S, Decl *D,
1658 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001659 IdentifierInfo *Platform = Attr.getParameterName();
1660 SourceLocation PlatformLoc = Attr.getParameterLoc();
1661
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001663 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1664 if (PlatformName.empty()) {
1665 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1666 << Platform;
1667
1668 PlatformName = Platform->getName();
1669 }
1670
1671 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1672 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1673 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001674 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001675
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001676 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001677 // of these steps are needed).
1678 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001679 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001680 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1681 << 1 << PlatformName << Deprecated.Version.getAsString()
1682 << 0 << Introduced.Version.getAsString();
1683 return;
1684 }
1685
1686 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001687 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001688 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1689 << 2 << PlatformName << Obsoleted.Version.getAsString()
1690 << 0 << Introduced.Version.getAsString();
1691 return;
1692 }
1693
1694 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001695 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001696 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1697 << 2 << PlatformName << Obsoleted.Version.getAsString()
1698 << 1 << Deprecated.Version.getAsString();
1699 return;
1700 }
1701
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001702 StringRef Str;
1703 const StringLiteral *SE =
1704 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1705 if (SE)
1706 Str = SE->getString();
1707
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001708 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001709 Platform,
1710 Introduced.Version,
1711 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001712 Obsoleted.Version,
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001713 IsUnavailable,
1714 Str));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001715}
1716
Chandler Carruth1b03c872011-07-02 00:01:44 +00001717static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001718 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001719 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001720 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001721
Peter Collingbourne7a730022010-11-23 20:45:58 +00001722 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001723 Arg = Arg->IgnoreParenCasts();
1724 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001725
Douglas Gregor5cee1192011-07-27 05:40:30 +00001726 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001727 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001728 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001729 return;
1730 }
Mike Stumpbf916502009-07-24 19:02:52 +00001731
Chris Lattner5f9e2722011-07-23 10:55:15 +00001732 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001733 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001734
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001735 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001736 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001737 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001738 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001739 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001740 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00001741 else if (TypeStr == "protected") {
1742 // Complain about attempts to use protected visibility on targets
1743 // (like Darwin) that don't support it.
1744 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1745 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1746 type = VisibilityAttr::Default;
1747 } else {
1748 type = VisibilityAttr::Protected;
1749 }
1750 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00001751 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001752 return;
1753 }
Mike Stumpbf916502009-07-24 19:02:52 +00001754
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001755 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001756}
1757
Chandler Carruth1b03c872011-07-02 00:01:44 +00001758static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1759 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001760 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1761 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001762 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001763 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001764 return;
1765 }
1766
Chandler Carruth87c44602011-07-01 23:49:12 +00001767 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1768 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001770 << "objc_method_family" << 1;
1771 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001772 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001773 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001774 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001775 return;
1776 }
1777
Chris Lattner5f9e2722011-07-23 10:55:15 +00001778 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001779 ObjCMethodFamilyAttr::FamilyKind family;
1780 if (param == "none")
1781 family = ObjCMethodFamilyAttr::OMF_None;
1782 else if (param == "alloc")
1783 family = ObjCMethodFamilyAttr::OMF_alloc;
1784 else if (param == "copy")
1785 family = ObjCMethodFamilyAttr::OMF_copy;
1786 else if (param == "init")
1787 family = ObjCMethodFamilyAttr::OMF_init;
1788 else if (param == "mutableCopy")
1789 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1790 else if (param == "new")
1791 family = ObjCMethodFamilyAttr::OMF_new;
1792 else {
1793 // Just warn and ignore it. This is future-proof against new
1794 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001795 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001796 return;
1797 }
1798
John McCallf85e1932011-06-15 23:02:42 +00001799 if (family == ObjCMethodFamilyAttr::OMF_init &&
1800 !method->getResultType()->isObjCObjectPointerType()) {
1801 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1802 << method->getResultType();
1803 // Ignore the attribute.
1804 return;
1805 }
1806
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001807 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001808 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001809}
1810
Chandler Carruth1b03c872011-07-02 00:01:44 +00001811static void handleObjCExceptionAttr(Sema &S, Decl *D,
1812 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001813 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001814 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001815
Chris Lattner0db29ec2009-02-14 08:09:34 +00001816 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1817 if (OCI == 0) {
1818 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1819 return;
1820 }
Mike Stumpbf916502009-07-24 19:02:52 +00001821
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001822 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001823}
1824
Chandler Carruth1b03c872011-07-02 00:01:44 +00001825static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001826 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001827 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001828 return;
1829 }
Richard Smith162e1c12011-04-15 14:24:37 +00001830 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001831 QualType T = TD->getUnderlyingType();
1832 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001833 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001834 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1835 return;
1836 }
1837 }
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001838 else if (!isa<ObjCPropertyDecl>(D)) {
1839 // It is okay to include this attribute on properties, e.g.:
1840 //
1841 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1842 //
1843 // In this case it follows tradition and suppresses an error in the above
1844 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00001845 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001846 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001847 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001848}
1849
Mike Stumpbf916502009-07-24 19:02:52 +00001850static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001851handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001852 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001854 return;
1855 }
1856
1857 if (!isa<FunctionDecl>(D)) {
1858 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1859 return;
1860 }
1861
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001862 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001863}
1864
Chandler Carruth1b03c872011-07-02 00:01:44 +00001865static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001866 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001867 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001868 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001869 return;
1870 }
Mike Stumpbf916502009-07-24 19:02:52 +00001871
Steve Naroff9eae5762008-09-18 16:44:58 +00001872 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001874 return;
1875 }
Mike Stumpbf916502009-07-24 19:02:52 +00001876
Sean Huntcf807c42010-08-18 23:23:40 +00001877 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001878 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001879 type = BlocksAttr::ByRef;
1880 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001881 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001882 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001883 return;
1884 }
Mike Stumpbf916502009-07-24 19:02:52 +00001885
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001886 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001887}
1888
Chandler Carruth1b03c872011-07-02 00:01:44 +00001889static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001890 // check the attribute arguments.
1891 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001892 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001893 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001894 }
1895
John McCall3323fad2011-09-09 07:56:05 +00001896 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001897 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001898 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001899 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001900 if (E->isTypeDependent() || E->isValueDependent() ||
1901 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001902 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001903 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001904 return;
1905 }
Mike Stumpbf916502009-07-24 19:02:52 +00001906
John McCall3323fad2011-09-09 07:56:05 +00001907 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001908 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1909 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001910 return;
1911 }
John McCall3323fad2011-09-09 07:56:05 +00001912
1913 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001914 }
1915
John McCall3323fad2011-09-09 07:56:05 +00001916 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001917 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001918 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001919 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001920 if (E->isTypeDependent() || E->isValueDependent() ||
1921 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001923 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001924 return;
1925 }
1926 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001927
John McCall3323fad2011-09-09 07:56:05 +00001928 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001929 // FIXME: This error message could be improved, it would be nice
1930 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001931 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1932 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001933 return;
1934 }
1935 }
1936
Chandler Carruth87c44602011-07-01 23:49:12 +00001937 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001938 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001939 if (isa<FunctionNoProtoType>(FT)) {
1940 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1941 return;
1942 }
Mike Stumpbf916502009-07-24 19:02:52 +00001943
Chris Lattner897cd902009-03-17 23:03:47 +00001944 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001945 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001946 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001947 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001948 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001949 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001950 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001951 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001952 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00001953 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1954 if (!BD->isVariadic()) {
1955 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1956 return;
1957 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001958 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001959 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001960 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001961 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001962 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001963 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001964 int m = Ty->isFunctionPointerType() ? 0 : 1;
1965 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001966 return;
1967 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001968 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001969 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001970 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001971 return;
1972 }
Anders Carlsson77091822008-10-05 18:05:59 +00001973 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001974 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001975 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001976 return;
1977 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001978 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001979 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001980}
1981
Chandler Carruth1b03c872011-07-02 00:01:44 +00001982static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001983 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001984 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001985 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001986
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001987 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001988 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001989 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001990 return;
1991 }
Mike Stumpbf916502009-07-24 19:02:52 +00001992
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001993 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1994 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1995 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001996 return;
1997 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001998 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1999 if (MD->getResultType()->isVoidType()) {
2000 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2001 << Attr.getName() << 1;
2002 return;
2003 }
2004
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002005 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002006}
2007
Chandler Carruth1b03c872011-07-02 00:01:44 +00002008static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002009 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002010 if (Attr.hasParameterOrArguments()) {
2011 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002012 return;
2013 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002014
Chandler Carruth87c44602011-07-01 23:49:12 +00002015 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002016 if (isa<CXXRecordDecl>(D)) {
2017 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2018 return;
2019 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002020 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2021 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002022 return;
2023 }
2024
Chandler Carruth87c44602011-07-01 23:49:12 +00002025 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002026
2027 // 'weak' only applies to declarations with external linkage.
2028 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002029 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002030 return;
2031 }
Mike Stumpbf916502009-07-24 19:02:52 +00002032
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002033 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002034}
2035
Chandler Carruth1b03c872011-07-02 00:01:44 +00002036static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002037 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002038 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002039 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002040
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002041
2042 // weak_import only applies to variable & function declarations.
2043 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002044 if (!D->canBeWeakImported(isDef)) {
2045 if (isDef)
2046 S.Diag(Attr.getLoc(),
2047 diag::warn_attribute_weak_import_invalid_on_definition)
2048 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002049 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002050 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002051 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002052 // Nothing to warn about here.
2053 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002054 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002055 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002056
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002057 return;
2058 }
2059
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002060 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002061}
2062
Chandler Carruth1b03c872011-07-02 00:01:44 +00002063static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2064 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002065 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002066 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002067 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002068
2069 unsigned WGSize[3];
2070 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002071 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002072 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002073 if (E->isTypeDependent() || E->isValueDependent() ||
2074 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002075 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2076 << "reqd_work_group_size" << E->getSourceRange();
2077 return;
2078 }
2079 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2080 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002081 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002082 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002083 WGSize[2]));
2084}
2085
Chandler Carruth1b03c872011-07-02 00:01:44 +00002086static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002087 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002088 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002089 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002090
2091 // Make sure that there is a string literal as the sections's single
2092 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002093 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002094 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002095 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002096 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002097 return;
2098 }
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Chris Lattner797c3c42009-08-10 19:03:04 +00002100 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002101 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002102 if (!Error.empty()) {
2103 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2104 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002105 return;
2106 }
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002108 // This attribute cannot be applied to local variables.
2109 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2110 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2111 return;
2112 }
2113
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002114 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002115 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002116}
2117
Chris Lattner6b6b5372008-06-26 18:38:35 +00002118
Chandler Carruth1b03c872011-07-02 00:01:44 +00002119static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002120 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002121 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002122 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002123 return;
2124 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002125
Chandler Carruth87c44602011-07-01 23:49:12 +00002126 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002127 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002128 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002129 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002130 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002131 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002132}
2133
Chandler Carruth1b03c872011-07-02 00:01:44 +00002134static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002135 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002136 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002137 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002138 return;
2139 }
Mike Stumpbf916502009-07-24 19:02:52 +00002140
Chandler Carruth87c44602011-07-01 23:49:12 +00002141 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002142 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002143 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002144 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002145 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002146 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002147}
2148
Chandler Carruth1b03c872011-07-02 00:01:44 +00002149static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002150 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002151 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002152 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002153
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002154 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002155}
2156
Chandler Carruth1b03c872011-07-02 00:01:44 +00002157static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002158 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002159 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2160 return;
2161 }
Mike Stumpbf916502009-07-24 19:02:52 +00002162
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002163 if (Attr.getNumArgs() != 0) {
2164 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2165 return;
2166 }
Mike Stumpbf916502009-07-24 19:02:52 +00002167
Chandler Carruth87c44602011-07-01 23:49:12 +00002168 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002169
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002170 if (!VD || !VD->hasLocalStorage()) {
2171 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2172 return;
2173 }
Mike Stumpbf916502009-07-24 19:02:52 +00002174
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002175 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002176 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002177 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002178 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2179 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002180 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002181 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002182 Attr.getParameterName();
2183 return;
2184 }
Mike Stumpbf916502009-07-24 19:02:52 +00002185
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002186 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2187 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002188 S.Diag(Attr.getParameterLoc(),
2189 diag::err_attribute_cleanup_arg_not_function)
2190 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002191 return;
2192 }
2193
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002194 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002195 S.Diag(Attr.getParameterLoc(),
2196 diag::err_attribute_cleanup_func_must_take_one_arg)
2197 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002198 return;
2199 }
Mike Stumpbf916502009-07-24 19:02:52 +00002200
Anders Carlsson89941c12009-02-07 23:16:50 +00002201 // We're currently more strict than GCC about what function types we accept.
2202 // If this ever proves to be a problem it should be easy to fix.
2203 QualType Ty = S.Context.getPointerType(VD->getType());
2204 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002205 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2206 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002207 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002208 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2209 Attr.getParameterName() << ParamTy << Ty;
2210 return;
2211 }
Mike Stumpbf916502009-07-24 19:02:52 +00002212
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002213 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002214 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002215}
2216
Mike Stumpbf916502009-07-24 19:02:52 +00002217/// Handle __attribute__((format_arg((idx)))) attribute based on
2218/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002219static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002220 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002221 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002222
Chandler Carruth87c44602011-07-01 23:49:12 +00002223 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002225 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002226 return;
2227 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002228
2229 // In C++ the implicit 'this' function parameter also counts, and they are
2230 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002231 bool HasImplicitThisParam = isInstanceMethod(D);
2232 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002233 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002234
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002235 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002236 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002237 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002238 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2239 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2241 << "format" << 2 << IdxExpr->getSourceRange();
2242 return;
2243 }
Mike Stumpbf916502009-07-24 19:02:52 +00002244
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002245 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2246 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2247 << "format" << 2 << IdxExpr->getSourceRange();
2248 return;
2249 }
Mike Stumpbf916502009-07-24 19:02:52 +00002250
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002251 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002252
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002253 if (HasImplicitThisParam) {
2254 if (ArgIdx == 0) {
2255 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2256 << "format_arg" << IdxExpr->getSourceRange();
2257 return;
2258 }
2259 ArgIdx--;
2260 }
2261
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002262 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002263 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002264
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002265 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2266 if (not_nsstring_type &&
2267 !isCFStringType(Ty, S.Context) &&
2268 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002269 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002270 // FIXME: Should highlight the actual expression that has the wrong type.
2271 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002272 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002273 << IdxExpr->getSourceRange();
2274 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002275 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002276 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002277 if (!isNSStringType(Ty, S.Context) &&
2278 !isCFStringType(Ty, S.Context) &&
2279 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002280 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002281 // FIXME: Should highlight the actual expression that has the wrong type.
2282 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002283 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002284 << IdxExpr->getSourceRange();
2285 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002286 }
2287
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002288 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002289 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002290}
2291
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002292enum FormatAttrKind {
2293 CFStringFormat,
2294 NSStringFormat,
2295 StrftimeFormat,
2296 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002297 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002298 InvalidFormat
2299};
2300
2301/// getFormatAttrKind - Map from format attribute names to supported format
2302/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002303static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002304 // Check for formats that get handled specially.
2305 if (Format == "NSString")
2306 return NSStringFormat;
2307 if (Format == "CFString")
2308 return CFStringFormat;
2309 if (Format == "strftime")
2310 return StrftimeFormat;
2311
2312 // Otherwise, check for supported formats.
2313 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupas69d53842012-01-27 09:14:17 +00002314 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002315 Format == "zcmn_err" ||
2316 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002317 return SupportedFormat;
2318
Duncan Sandsbc525952010-03-23 14:44:19 +00002319 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2320 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002321 return IgnoredFormat;
2322
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002323 return InvalidFormat;
2324}
2325
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002326/// Handle __attribute__((init_priority(priority))) attributes based on
2327/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002328static void handleInitPriorityAttr(Sema &S, Decl *D,
2329 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002330 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002331 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2332 return;
2333 }
2334
Chandler Carruth87c44602011-07-01 23:49:12 +00002335 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002336 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2337 Attr.setInvalid();
2338 return;
2339 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002340 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002341 if (S.Context.getAsArrayType(T))
2342 T = S.Context.getBaseElementType(T);
2343 if (!T->getAs<RecordType>()) {
2344 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2345 Attr.setInvalid();
2346 return;
2347 }
2348
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002349 if (Attr.getNumArgs() != 1) {
2350 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2351 Attr.setInvalid();
2352 return;
2353 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002354 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002355
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002356 llvm::APSInt priority(32);
2357 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2358 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2359 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2360 << "init_priority" << priorityExpr->getSourceRange();
2361 Attr.setInvalid();
2362 return;
2363 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002364 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002365 if (prioritynum < 101 || prioritynum > 65535) {
2366 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2367 << priorityExpr->getSourceRange();
2368 Attr.setInvalid();
2369 return;
2370 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002371 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002372 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002373}
2374
Mike Stumpbf916502009-07-24 19:02:52 +00002375/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2376/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002377static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002378
Chris Lattner545dd342008-06-28 23:36:30 +00002379 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002380 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002381 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002382 return;
2383 }
2384
Chris Lattner545dd342008-06-28 23:36:30 +00002385 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002386 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002387 return;
2388 }
2389
Chandler Carruth87c44602011-07-01 23:49:12 +00002390 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002391 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002392 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002393 return;
2394 }
2395
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002396 // In C++ the implicit 'this' function parameter also counts, and they are
2397 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002398 bool HasImplicitThisParam = isInstanceMethod(D);
2399 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002400 unsigned FirstIdx = 1;
2401
Chris Lattner5f9e2722011-07-23 10:55:15 +00002402 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002403
2404 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002405 if (Format.startswith("__") && Format.endswith("__"))
2406 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002407
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002408 // Check for supported formats.
2409 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002410
2411 if (Kind == IgnoredFormat)
2412 return;
2413
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002414 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002415 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002416 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002417 return;
2418 }
2419
2420 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002421 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002422 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002423 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2424 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002425 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002426 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002427 return;
2428 }
2429
2430 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002431 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002432 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002433 return;
2434 }
2435
2436 // FIXME: Do we need to bounds check?
2437 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002438
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002439 if (HasImplicitThisParam) {
2440 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002441 S.Diag(Attr.getLoc(),
2442 diag::err_format_attribute_implicit_this_format_string)
2443 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002444 return;
2445 }
2446 ArgIdx--;
2447 }
Mike Stump1eb44332009-09-09 15:08:12 +00002448
Chris Lattner6b6b5372008-06-26 18:38:35 +00002449 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002450 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002451
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002452 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002453 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002454 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2455 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002456 return;
2457 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002458 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002459 // FIXME: do we need to check if the type is NSString*? What are the
2460 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002461 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002462 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002463 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2464 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002465 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002466 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002467 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002468 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002469 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002470 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2471 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002472 return;
2473 }
2474
2475 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002476 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002477 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002478 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2479 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002481 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002482 return;
2483 }
2484
2485 // check if the function is variadic if the 3rd argument non-zero
2486 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002487 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002488 ++NumArgs; // +1 for ...
2489 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002490 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002491 return;
2492 }
2493 }
2494
Chris Lattner3c73c412008-11-19 08:23:25 +00002495 // strftime requires FirstArg to be 0 because it doesn't read from any
2496 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002497 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002498 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002499 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2500 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002501 return;
2502 }
2503 // if 0 it disables parameter checking (to use with e.g. va_list)
2504 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002506 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002507 return;
2508 }
2509
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002510 // Check whether we already have an equivalent format attribute.
2511 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002512 i = D->specific_attr_begin<FormatAttr>(),
2513 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002514 i != e ; ++i) {
2515 FormatAttr *f = *i;
2516 if (f->getType() == Format &&
2517 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2518 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2519 // If we don't have a valid location for this attribute, adopt the
2520 // location.
2521 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002522 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002523 return;
2524 }
2525 }
2526
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002527 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002528 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002529 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002530}
2531
Chandler Carruth1b03c872011-07-02 00:01:44 +00002532static void handleTransparentUnionAttr(Sema &S, Decl *D,
2533 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002534 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002535 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002536 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002537
Chris Lattner6b6b5372008-06-26 18:38:35 +00002538
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002539 // Try to find the underlying union declaration.
2540 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002541 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002542 if (TD && TD->getUnderlyingType()->isUnionType())
2543 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2544 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002545 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002546
2547 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002548 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002549 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002550 return;
2551 }
2552
John McCall5e1cdac2011-10-07 06:10:15 +00002553 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002554 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002555 diag::warn_transparent_union_attribute_not_definition);
2556 return;
2557 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002558
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002559 RecordDecl::field_iterator Field = RD->field_begin(),
2560 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002561 if (Field == FieldEnd) {
2562 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2563 return;
2564 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002565
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002566 FieldDecl *FirstField = *Field;
2567 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002568 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002569 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002570 diag::warn_transparent_union_attribute_floating)
2571 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002572 return;
2573 }
2574
2575 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2576 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2577 for (; Field != FieldEnd; ++Field) {
2578 QualType FieldType = Field->getType();
2579 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2580 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2581 // Warn if we drop the attribute.
2582 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002583 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002584 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002585 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002586 diag::warn_transparent_union_attribute_field_size_align)
2587 << isSize << Field->getDeclName() << FieldBits;
2588 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002589 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002590 diag::note_transparent_union_first_field_size_align)
2591 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002592 return;
2593 }
2594 }
2595
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002596 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002597}
2598
Chandler Carruth1b03c872011-07-02 00:01:44 +00002599static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002600 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002601 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002602 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002603
Peter Collingbourne7a730022010-11-23 20:45:58 +00002604 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002605 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002606
Chris Lattner6b6b5372008-06-26 18:38:35 +00002607 // Make sure that there is a string literal as the annotation's single
2608 // argument.
2609 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002610 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002611 return;
2612 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002613
2614 // Don't duplicate annotations that are already set.
2615 for (specific_attr_iterator<AnnotateAttr>
2616 i = D->specific_attr_begin<AnnotateAttr>(),
2617 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2618 if ((*i)->getAnnotation() == SE->getString())
2619 return;
2620 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002621 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002622 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002623}
2624
Chandler Carruth1b03c872011-07-02 00:01:44 +00002625static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002626 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002627 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002629 return;
2630 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002631
2632 //FIXME: The C++0x version of this attribute has more limited applicabilty
2633 // than GNU's, and should error out when it is used to specify a
2634 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002635
Chris Lattner545dd342008-06-28 23:36:30 +00002636 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002637 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002638 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002639 }
Mike Stumpbf916502009-07-24 19:02:52 +00002640
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002641 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002642}
2643
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002644void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002645 // FIXME: Handle pack-expansions here.
2646 if (DiagnoseUnexpandedParameterPack(E))
2647 return;
2648
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002649 if (E->isTypeDependent() || E->isValueDependent()) {
2650 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002651 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002652 return;
2653 }
2654
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002655 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002656 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002657 llvm::APSInt Alignment(32);
Richard Smith282e7e62012-02-04 09:53:13 +00002658 ExprResult ICE =
2659 VerifyIntegerConstantExpression(E, &Alignment,
2660 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2661 /*AllowFold*/ false);
2662 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002663 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002664 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002665 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2666 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002667 return;
2668 }
2669
Richard Smith282e7e62012-02-04 09:53:13 +00002670 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Sean Huntcf807c42010-08-18 23:23:40 +00002671}
2672
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002673void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002674 // FIXME: Cache the number on the Attr object if non-dependent?
2675 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002676 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002677 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002678}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002679
Chandler Carruthd309c812011-07-01 23:49:16 +00002680/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002681/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002682///
Mike Stumpbf916502009-07-24 19:02:52 +00002683/// Despite what would be logical, the mode attribute is a decl attribute, not a
2684/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2685/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002686static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002687 // This attribute isn't documented, but glibc uses it. It changes
2688 // the width of an int or unsigned int to the specified size.
2689
2690 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002691 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002692 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002693
Chris Lattnerfbf13472008-06-27 22:18:37 +00002694
2695 IdentifierInfo *Name = Attr.getParameterName();
2696 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002697 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002698 return;
2699 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002700
Chris Lattner5f9e2722011-07-23 10:55:15 +00002701 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002702
2703 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002704 if (Str.startswith("__") && Str.endswith("__"))
2705 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002706
2707 unsigned DestWidth = 0;
2708 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002709 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002710 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002711 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002712 switch (Str[0]) {
2713 case 'Q': DestWidth = 8; break;
2714 case 'H': DestWidth = 16; break;
2715 case 'S': DestWidth = 32; break;
2716 case 'D': DestWidth = 64; break;
2717 case 'X': DestWidth = 96; break;
2718 case 'T': DestWidth = 128; break;
2719 }
2720 if (Str[1] == 'F') {
2721 IntegerMode = false;
2722 } else if (Str[1] == 'C') {
2723 IntegerMode = false;
2724 ComplexMode = true;
2725 } else if (Str[1] != 'I') {
2726 DestWidth = 0;
2727 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002728 break;
2729 case 4:
2730 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2731 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002732 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002733 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002734 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002735 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002736 break;
2737 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002738 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002739 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002740 break;
2741 }
2742
2743 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002744 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002745 OldTy = TD->getUnderlyingType();
2746 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2747 OldTy = VD->getType();
2748 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002749 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002750 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002751 return;
2752 }
Eli Friedman73397492009-03-03 06:41:03 +00002753
John McCall183700f2009-09-21 23:43:11 +00002754 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002755 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2756 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002757 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002758 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2759 } else if (ComplexMode) {
2760 if (!OldTy->isComplexType())
2761 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2762 } else {
2763 if (!OldTy->isFloatingType())
2764 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2765 }
2766
Mike Stump390b4cc2009-05-16 07:39:55 +00002767 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2768 // and friends, at least with glibc.
2769 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2770 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002771 // FIXME: Make sure floating-point mappings are accurate
2772 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002773 QualType NewTy;
2774 switch (DestWidth) {
2775 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002776 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002777 return;
2778 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002779 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002780 return;
2781 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002782 if (!IntegerMode) {
2783 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2784 return;
2785 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002786 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002787 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002788 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002789 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002790 break;
2791 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002792 if (!IntegerMode) {
2793 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2794 return;
2795 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002796 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002797 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002798 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002799 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002800 break;
2801 case 32:
2802 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002803 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002804 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002805 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002806 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002807 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002808 break;
2809 case 64:
2810 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002811 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002812 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002813 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002814 NewTy = S.Context.LongTy;
2815 else
2816 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002817 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002818 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002819 NewTy = S.Context.UnsignedLongTy;
2820 else
2821 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002822 break;
Eli Friedman73397492009-03-03 06:41:03 +00002823 case 96:
2824 NewTy = S.Context.LongDoubleTy;
2825 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002826 case 128:
2827 if (!IntegerMode) {
2828 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2829 return;
2830 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002831 if (OldTy->isSignedIntegerType())
2832 NewTy = S.Context.Int128Ty;
2833 else
2834 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002835 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002836 }
2837
Eli Friedman73397492009-03-03 06:41:03 +00002838 if (ComplexMode) {
2839 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002840 }
2841
2842 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002843 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002844 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002845 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002846 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002847 cast<ValueDecl>(D)->setType(NewTy);
2848}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002849
Chandler Carruth1b03c872011-07-02 00:01:44 +00002850static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002851 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002852 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002853 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002854
Chandler Carruth87c44602011-07-01 23:49:12 +00002855 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002857 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002858 return;
2859 }
Mike Stumpbf916502009-07-24 19:02:52 +00002860
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002861 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002862}
2863
Chandler Carruth1b03c872011-07-02 00:01:44 +00002864static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002865 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002866 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002867 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002868
Mike Stumpbf916502009-07-24 19:02:52 +00002869
Chandler Carruth87c44602011-07-01 23:49:12 +00002870 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002872 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002873 return;
2874 }
Mike Stumpbf916502009-07-24 19:02:52 +00002875
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002876 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002877}
2878
Chandler Carruth1b03c872011-07-02 00:01:44 +00002879static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2880 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002881 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002882 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002883 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002884
Chris Lattner7255a2d2010-06-22 00:03:40 +00002885
Chandler Carruth87c44602011-07-01 23:49:12 +00002886 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002887 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002888 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002889 return;
2890 }
2891
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002892 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002893 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002894}
2895
Chandler Carruth1b03c872011-07-02 00:01:44 +00002896static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002897 if (S.LangOpts.CUDA) {
2898 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002899 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2901 return;
2902 }
2903
Chandler Carruth87c44602011-07-01 23:49:12 +00002904 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002905 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002906 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002907 return;
2908 }
2909
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002910 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002911 } else {
2912 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2913 }
2914}
2915
Chandler Carruth1b03c872011-07-02 00:01:44 +00002916static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002917 if (S.LangOpts.CUDA) {
2918 // check the attribute arguments.
2919 if (Attr.getNumArgs() != 0) {
2920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2921 return;
2922 }
2923
Chandler Carruth87c44602011-07-01 23:49:12 +00002924 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002926 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002927 return;
2928 }
2929
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002930 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002931 } else {
2932 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2933 }
2934}
2935
Chandler Carruth1b03c872011-07-02 00:01:44 +00002936static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002937 if (S.LangOpts.CUDA) {
2938 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002939 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002940 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002941
Chandler Carruth87c44602011-07-01 23:49:12 +00002942 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002944 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002945 return;
2946 }
2947
Chandler Carruth87c44602011-07-01 23:49:12 +00002948 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002949 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002950 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002951 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2952 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2953 << FD->getType()
2954 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2955 "void");
2956 } else {
2957 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2958 << FD->getType();
2959 }
2960 return;
2961 }
2962
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002963 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002964 } else {
2965 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2966 }
2967}
2968
Chandler Carruth1b03c872011-07-02 00:01:44 +00002969static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002970 if (S.LangOpts.CUDA) {
2971 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002972 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002973 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002974
Peter Collingbourneced76712010-12-01 03:15:31 +00002975
Chandler Carruth87c44602011-07-01 23:49:12 +00002976 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002977 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002978 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002979 return;
2980 }
2981
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002982 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002983 } else {
2984 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2985 }
2986}
2987
Chandler Carruth1b03c872011-07-02 00:01:44 +00002988static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002989 if (S.LangOpts.CUDA) {
2990 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002991 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002992 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002993
Peter Collingbourneced76712010-12-01 03:15:31 +00002994
Chandler Carruth87c44602011-07-01 23:49:12 +00002995 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002997 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002998 return;
2999 }
3000
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003001 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003002 } else {
3003 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3004 }
3005}
3006
Chandler Carruth1b03c872011-07-02 00:01:44 +00003007static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003008 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003009 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003010 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003011
Chandler Carruth87c44602011-07-01 23:49:12 +00003012 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003013 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003015 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003016 return;
3017 }
Mike Stumpbf916502009-07-24 19:02:52 +00003018
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003019 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003020 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003021 return;
3022 }
Mike Stumpbf916502009-07-24 19:02:52 +00003023
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003024 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003025}
3026
Chandler Carruth1b03c872011-07-02 00:01:44 +00003027static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003028 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003029
Chandler Carruth87c44602011-07-01 23:49:12 +00003030 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003031 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3032 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003033 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003034 return;
3035
Chandler Carruth87c44602011-07-01 23:49:12 +00003036 if (!isa<ObjCMethodDecl>(D)) {
3037 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3038 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003039 return;
3040 }
3041
Chandler Carruth87c44602011-07-01 23:49:12 +00003042 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00003043 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003044 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003045 return;
3046 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003047 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003048 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003049 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003050 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003051 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003052 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003053 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003054 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003055 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003056 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003057 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003058 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003059 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003060 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003061 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003062 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003063 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003064 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003065 return;
3066 }
3067
Chris Lattner5f9e2722011-07-23 10:55:15 +00003068 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003069 PcsAttr::PCSType PCS;
3070 if (StrRef == "aapcs")
3071 PCS = PcsAttr::AAPCS;
3072 else if (StrRef == "aapcs-vfp")
3073 PCS = PcsAttr::AAPCS_VFP;
3074 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003075 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3076 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003077 return;
3078 }
3079
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003080 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003081 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003082 default:
3083 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003084 }
3085}
3086
Chandler Carruth1b03c872011-07-02 00:01:44 +00003087static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003088 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003089 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003090}
3091
John McCall711c52b2011-01-05 12:14:39 +00003092bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3093 if (attr.isInvalid())
3094 return true;
3095
Ted Kremenek831efae2011-04-15 05:49:29 +00003096 if ((attr.getNumArgs() != 0 &&
3097 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3098 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003099 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3100 attr.setInvalid();
3101 return true;
3102 }
3103
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003104 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3105 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003106 switch (attr.getKind()) {
3107 case AttributeList::AT_cdecl: CC = CC_C; break;
3108 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3109 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3110 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3111 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003112 case AttributeList::AT_pcs: {
3113 Expr *Arg = attr.getArg(0);
3114 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003115 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003116 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3117 << "pcs" << 1;
3118 attr.setInvalid();
3119 return true;
3120 }
3121
Chris Lattner5f9e2722011-07-23 10:55:15 +00003122 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003123 if (StrRef == "aapcs") {
3124 CC = CC_AAPCS;
3125 break;
3126 } else if (StrRef == "aapcs-vfp") {
3127 CC = CC_AAPCS_VFP;
3128 break;
3129 }
3130 // FALLS THROUGH
3131 }
David Blaikie7530c032012-01-17 06:56:22 +00003132 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003133 }
3134
3135 return false;
3136}
3137
Chandler Carruth1b03c872011-07-02 00:01:44 +00003138static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003139 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003140
3141 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003142 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003143 return;
3144
Chandler Carruth87c44602011-07-01 23:49:12 +00003145 if (!isa<ObjCMethodDecl>(D)) {
3146 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3147 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003148 return;
3149 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003150
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003151 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003152}
3153
3154/// Checks a regparm attribute, returning true if it is ill-formed and
3155/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003156bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3157 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003158 return true;
3159
Chandler Carruth87c44602011-07-01 23:49:12 +00003160 if (Attr.getNumArgs() != 1) {
3161 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3162 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003163 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003164 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003165
Chandler Carruth87c44602011-07-01 23:49:12 +00003166 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003167 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003168 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003169 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003170 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003171 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003172 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003173 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003174 }
3175
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003176 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003177 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003178 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003179 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003180 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003181 }
3182
John McCall711c52b2011-01-05 12:14:39 +00003183 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003184 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003185 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003186 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003187 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003188 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003189 }
3190
John McCall711c52b2011-01-05 12:14:39 +00003191 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003192}
3193
Chandler Carruth1b03c872011-07-02 00:01:44 +00003194static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003195 if (S.LangOpts.CUDA) {
3196 // check the attribute arguments.
3197 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003198 // FIXME: 0 is not okay.
3199 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003200 return;
3201 }
3202
Chandler Carruth87c44602011-07-01 23:49:12 +00003203 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003204 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003205 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003206 return;
3207 }
3208
3209 Expr *MaxThreadsExpr = Attr.getArg(0);
3210 llvm::APSInt MaxThreads(32);
3211 if (MaxThreadsExpr->isTypeDependent() ||
3212 MaxThreadsExpr->isValueDependent() ||
3213 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3214 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3215 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3216 return;
3217 }
3218
3219 llvm::APSInt MinBlocks(32);
3220 if (Attr.getNumArgs() > 1) {
3221 Expr *MinBlocksExpr = Attr.getArg(1);
3222 if (MinBlocksExpr->isTypeDependent() ||
3223 MinBlocksExpr->isValueDependent() ||
3224 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3226 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3227 return;
3228 }
3229 }
3230
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003231 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003232 MaxThreads.getZExtValue(),
3233 MinBlocks.getZExtValue()));
3234 } else {
3235 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3236 }
3237}
3238
Chris Lattner0744e5f2008-06-29 00:23:49 +00003239//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003240// Checker-specific attribute handlers.
3241//===----------------------------------------------------------------------===//
3242
John McCallc7ad3812011-01-25 03:31:58 +00003243static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003244 return type->isDependentType() ||
3245 type->isObjCObjectPointerType() ||
3246 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003247}
3248static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003249 return type->isDependentType() ||
3250 type->isPointerType() ||
3251 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003252}
3253
Chandler Carruth1b03c872011-07-02 00:01:44 +00003254static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003255 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003256 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003257 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003258 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003259 return;
3260 }
3261
3262 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003263 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003264 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3265 cf = false;
3266 } else {
3267 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3268 cf = true;
3269 }
3270
3271 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003272 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003273 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003274 return;
3275 }
3276
3277 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003278 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003279 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003280 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003281}
3282
Chandler Carruth1b03c872011-07-02 00:01:44 +00003283static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3284 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003285 if (!isa<ObjCMethodDecl>(D)) {
3286 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003287 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003288 return;
3289 }
3290
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003291 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003292}
3293
Chandler Carruth1b03c872011-07-02 00:01:44 +00003294static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3295 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003296
John McCallc7ad3812011-01-25 03:31:58 +00003297 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003298
Chandler Carruth87c44602011-07-01 23:49:12 +00003299 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003300 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003301 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003302 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003303 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruth87c44602011-07-01 23:49:12 +00003304 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003305 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003306 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003307 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003308 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003309 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003310 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003311 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003312 return;
3313 }
Mike Stumpbf916502009-07-24 19:02:52 +00003314
John McCallc7ad3812011-01-25 03:31:58 +00003315 bool typeOK;
3316 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003317 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003318 default: llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003319 case AttributeList::AT_ns_returns_autoreleased:
3320 case AttributeList::AT_ns_returns_retained:
3321 case AttributeList::AT_ns_returns_not_retained:
3322 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3323 cf = false;
3324 break;
3325
3326 case AttributeList::AT_cf_returns_retained:
3327 case AttributeList::AT_cf_returns_not_retained:
3328 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3329 cf = true;
3330 break;
3331 }
3332
3333 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003334 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003335 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003336 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003337 }
Mike Stumpbf916502009-07-24 19:02:52 +00003338
Chandler Carruth87c44602011-07-01 23:49:12 +00003339 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003340 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003341 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003342 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003343 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003344 S.Context));
3345 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003346 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003347 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003348 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003349 return;
3350 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003351 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003352 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003353 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003354 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003355 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003356 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003357 return;
3358 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003359 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003360 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003361 return;
3362 };
3363}
3364
John McCalldc7c5ad2011-07-22 08:53:00 +00003365static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3366 const AttributeList &attr) {
3367 SourceLocation loc = attr.getLoc();
3368
3369 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3370
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003371 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003372 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003373 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003374 return;
3375 }
3376
3377 // Check that the method returns a normal pointer.
3378 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003379
3380 if (!resultType->isReferenceType() &&
3381 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003382 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3383 << SourceRange(loc)
3384 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3385
3386 // Drop the attribute.
3387 return;
3388 }
3389
3390 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003391 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003392}
3393
John McCall8dfac0b2011-09-30 05:12:12 +00003394/// Handle cf_audited_transfer and cf_unknown_transfer.
3395static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3396 if (!isa<FunctionDecl>(D)) {
3397 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003398 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003399 return;
3400 }
3401
3402 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3403
3404 // Check whether there's a conflicting attribute already present.
3405 Attr *Existing;
3406 if (IsAudited) {
3407 Existing = D->getAttr<CFUnknownTransferAttr>();
3408 } else {
3409 Existing = D->getAttr<CFAuditedTransferAttr>();
3410 }
3411 if (Existing) {
3412 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3413 << A.getName()
3414 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3415 << A.getRange() << Existing->getRange();
3416 return;
3417 }
3418
3419 // All clear; add the attribute.
3420 if (IsAudited) {
3421 D->addAttr(
3422 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3423 } else {
3424 D->addAttr(
3425 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3426 }
3427}
3428
John McCallfe98da02011-09-29 07:17:38 +00003429static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3430 const AttributeList &Attr) {
3431 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3432 if (!RD || RD->isUnion()) {
3433 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003434 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003435 }
3436
3437 IdentifierInfo *ParmName = Attr.getParameterName();
3438
3439 // In Objective-C, verify that the type names an Objective-C type.
3440 // We don't want to check this outside of ObjC because people sometimes
3441 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003442 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003443 // Check for an existing type with this name.
3444 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3445 Sema::LookupOrdinaryName);
3446 if (S.LookupName(R, Sc)) {
3447 NamedDecl *Target = R.getFoundDecl();
3448 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3449 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3450 S.Diag(Target->getLocStart(), diag::note_declared_at);
3451 }
3452 }
3453 }
3454
3455 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3456 ParmName));
3457}
3458
Chandler Carruth1b03c872011-07-02 00:01:44 +00003459static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3460 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003461 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003462
Chandler Carruth87c44602011-07-01 23:49:12 +00003463 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003464 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003465}
3466
Chandler Carruth1b03c872011-07-02 00:01:44 +00003467static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3468 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003469 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003470 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003471 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003472 return;
3473 }
3474
Chandler Carruth87c44602011-07-01 23:49:12 +00003475 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003476 QualType type = vd->getType();
3477
3478 if (!type->isDependentType() &&
3479 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003480 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003481 << type;
3482 return;
3483 }
3484
3485 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3486
3487 // If we have no lifetime yet, check the lifetime we're presumably
3488 // going to infer.
3489 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3490 lifetime = type->getObjCARCImplicitLifetime();
3491
3492 switch (lifetime) {
3493 case Qualifiers::OCL_None:
3494 assert(type->isDependentType() &&
3495 "didn't infer lifetime for non-dependent type?");
3496 break;
3497
3498 case Qualifiers::OCL_Weak: // meaningful
3499 case Qualifiers::OCL_Strong: // meaningful
3500 break;
3501
3502 case Qualifiers::OCL_ExplicitNone:
3503 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003504 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003505 << (lifetime == Qualifiers::OCL_Autoreleasing);
3506 break;
3507 }
3508
Chandler Carruth87c44602011-07-01 23:49:12 +00003509 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003510 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003511}
3512
Charles Davisf0122fe2010-02-16 18:27:26 +00003513static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman94287722012-02-23 22:46:33 +00003514 switch (Attr.getKind()) {
3515 default:
3516 return false;
3517 case AttributeList::AT_dllimport:
3518 case AttributeList::AT_dllexport:
3519 case AttributeList::AT_uuid:
3520 case AttributeList::AT_deprecated:
3521 case AttributeList::AT_noreturn:
3522 case AttributeList::AT_nothrow:
3523 case AttributeList::AT_naked:
3524 case AttributeList::AT_noinline:
3525 return true;
3526 }
Francois Pichet11542142010-12-19 06:50:37 +00003527}
3528
3529//===----------------------------------------------------------------------===//
3530// Microsoft specific attribute handlers.
3531//===----------------------------------------------------------------------===//
3532
Chandler Carruth1b03c872011-07-02 00:01:44 +00003533static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003534 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003535 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003536 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003537 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003538
Francois Pichet11542142010-12-19 06:50:37 +00003539 Expr *Arg = Attr.getArg(0);
3540 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003541 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003542 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3543 << "uuid" << 1;
3544 return;
3545 }
3546
Chris Lattner5f9e2722011-07-23 10:55:15 +00003547 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003548
3549 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3550 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003551
Francois Pichetd3d3be92010-12-20 01:41:49 +00003552 // Validate GUID length.
3553 if (IsCurly && StrRef.size() != 38) {
3554 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3555 return;
3556 }
3557 if (!IsCurly && StrRef.size() != 36) {
3558 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3559 return;
3560 }
3561
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003562 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003563 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003564 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003565 if (IsCurly) // Skip the optional '{'
3566 ++I;
3567
3568 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003569 if (i == 8 || i == 13 || i == 18 || i == 23) {
3570 if (*I != '-') {
3571 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3572 return;
3573 }
3574 } else if (!isxdigit(*I)) {
3575 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3576 return;
3577 }
3578 I++;
3579 }
Francois Pichet11542142010-12-19 06:50:37 +00003580
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003581 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003582 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003583 } else
Francois Pichet11542142010-12-19 06:50:37 +00003584 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003585}
3586
Ted Kremenekb71368d2009-05-09 02:44:38 +00003587//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003588// Top Level Sema Entry Points
3589//===----------------------------------------------------------------------===//
3590
Chandler Carruth1b03c872011-07-02 00:01:44 +00003591static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3592 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003593 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003594 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3595 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3596 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003597 default:
3598 break;
3599 }
3600}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003601
Chandler Carruth1b03c872011-07-02 00:01:44 +00003602static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3603 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003604 switch (Attr.getKind()) {
Michael Hane53ac8a2012-03-07 00:12:16 +00003605 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3606 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3607 case AttributeList::AT_iboutletcollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003608 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003609 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003610 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003611 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003612 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003613 case AttributeList::AT_neon_vector_type:
3614 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003615 // Ignore these, these are type attributes, handled by
3616 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003617 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003618 case AttributeList::AT_device:
3619 case AttributeList::AT_host:
3620 case AttributeList::AT_overloadable:
3621 // Ignore, this is a non-inheritable attribute, handled
3622 // by ProcessNonInheritableDeclAttr.
3623 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003624 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3625 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003626 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003627 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003628 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003629 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3630 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3631 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003632 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003633 handleDependencyAttr (S, D, Attr); break;
3634 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3635 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3636 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3637 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3638 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003639 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003640 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003641 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003642 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3643 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3644 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3645 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003646 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003647 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003648 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003649 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3650 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3651 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3652 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3653 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003654 case AttributeList::AT_ownership_returns:
3655 case AttributeList::AT_ownership_takes:
3656 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003657 handleOwnershipAttr (S, D, Attr); break;
3658 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3659 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3660 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3661 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3662 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003663
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003664 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003665 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003666 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003667 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003668
John McCalldc7c5ad2011-07-22 08:53:00 +00003669 case AttributeList::AT_objc_returns_inner_pointer:
3670 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3671
John McCallfe98da02011-09-29 07:17:38 +00003672 case AttributeList::AT_ns_bridged:
3673 handleNSBridgedAttr(S, scope, D, Attr); break;
3674
John McCall8dfac0b2011-09-30 05:12:12 +00003675 case AttributeList::AT_cf_audited_transfer:
3676 case AttributeList::AT_cf_unknown_transfer:
3677 handleCFTransferAttr(S, D, Attr); break;
3678
Ted Kremenekb71368d2009-05-09 02:44:38 +00003679 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003680 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003681 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003682 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003683 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003684
3685 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003686 case AttributeList::AT_ns_returns_not_retained:
3687 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003688 case AttributeList::AT_ns_returns_retained:
3689 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003690 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003691
Michael Hane53ac8a2012-03-07 00:12:16 +00003692 case AttributeList::AT_reqd_work_group_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003693 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003694
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003695 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003696 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003697
Chandler Carruth1b03c872011-07-02 00:01:44 +00003698 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003699 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003700 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3701 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003702 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003703 handleArcWeakrefUnavailableAttr (S, D, Attr);
3704 break;
Patrick Beardb2f68202012-04-06 18:12:22 +00003705 case AttributeList::AT_objc_root_class:
3706 handleObjCRootClassAttr(S, D, Attr);
3707 break;
Ted Kremenek71207fc2012-01-05 22:47:47 +00003708 case AttributeList::AT_objc_requires_property_definitions:
3709 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00003710 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003711 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00003712 case AttributeList::AT_returns_twice:
3713 handleReturnsTwiceAttr(S, D, Attr);
3714 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003715 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3716 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3717 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003718 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003719 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3720 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3721 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003722 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003723 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003724 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003725 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003726 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003727 break;
John McCalld5313b02011-03-02 11:33:24 +00003728 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003729 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003730 break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003731 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003732 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3733 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3734 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3735 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3736 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3737 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3738 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3739 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003740 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003741 // Just ignore
3742 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003743 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003744 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003745 break;
John McCall04a67a62010-02-05 21:31:56 +00003746 case AttributeList::AT_stdcall:
3747 case AttributeList::AT_cdecl:
3748 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003749 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003750 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003751 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003752 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003753 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003754 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003755 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003756 break;
Francois Pichet11542142010-12-19 06:50:37 +00003757 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003758 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003759 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003760
3761 // Thread safety attributes:
3762 case AttributeList::AT_guarded_var:
3763 handleGuardedVarAttr(S, D, Attr);
3764 break;
3765 case AttributeList::AT_pt_guarded_var:
3766 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3767 break;
3768 case AttributeList::AT_scoped_lockable:
3769 handleLockableAttr(S, D, Attr, /*scoped = */true);
3770 break;
Kostya Serebryany71efba02012-01-24 19:25:38 +00003771 case AttributeList::AT_no_address_safety_analysis:
3772 handleNoAddressSafetyAttr(S, D, Attr);
3773 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003774 case AttributeList::AT_no_thread_safety_analysis:
3775 handleNoThreadSafetyAttr(S, D, Attr);
3776 break;
3777 case AttributeList::AT_lockable:
3778 handleLockableAttr(S, D, Attr);
3779 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003780 case AttributeList::AT_guarded_by:
3781 handleGuardedByAttr(S, D, Attr);
3782 break;
3783 case AttributeList::AT_pt_guarded_by:
3784 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3785 break;
3786 case AttributeList::AT_exclusive_lock_function:
3787 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3788 break;
3789 case AttributeList::AT_exclusive_locks_required:
3790 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3791 break;
3792 case AttributeList::AT_exclusive_trylock_function:
3793 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3794 break;
3795 case AttributeList::AT_lock_returned:
3796 handleLockReturnedAttr(S, D, Attr);
3797 break;
3798 case AttributeList::AT_locks_excluded:
3799 handleLocksExcludedAttr(S, D, Attr);
3800 break;
3801 case AttributeList::AT_shared_lock_function:
3802 handleLockFunAttr(S, D, Attr);
3803 break;
3804 case AttributeList::AT_shared_locks_required:
3805 handleLocksRequiredAttr(S, D, Attr);
3806 break;
3807 case AttributeList::AT_shared_trylock_function:
3808 handleTrylockFunAttr(S, D, Attr);
3809 break;
3810 case AttributeList::AT_unlock_function:
3811 handleUnlockFunAttr(S, D, Attr);
3812 break;
3813 case AttributeList::AT_acquired_before:
3814 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3815 break;
3816 case AttributeList::AT_acquired_after:
3817 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3818 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003819
Chris Lattner803d0802008-06-29 00:43:07 +00003820 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003821 // Ask target about the attribute.
3822 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3823 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003824 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3825 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003826 break;
3827 }
3828}
3829
Peter Collingbourne60700392011-01-21 02:08:45 +00003830/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3831/// the attribute applies to decls. If the attribute is a type attribute, just
3832/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3833/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003834static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3835 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003836 bool NonInheritable, bool Inheritable) {
3837 if (Attr.isInvalid())
3838 return;
3839
3840 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3841 // FIXME: Try to deal with other __declspec attributes!
3842 return;
3843
3844 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003845 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003846
3847 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003848 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003849}
3850
Chris Lattner803d0802008-06-29 00:43:07 +00003851/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3852/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003853void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003854 const AttributeList *AttrList,
3855 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003856 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003857 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003858 }
3859
3860 // GCC accepts
3861 // static int a9 __attribute__((weakref));
3862 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003863 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003864 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003865 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003866 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003867 }
3868}
3869
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003870// Annotation attributes are the only attributes allowed after an access
3871// specifier.
3872bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3873 const AttributeList *AttrList) {
3874 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3875 if (l->getKind() == AttributeList::AT_annotate) {
3876 handleAnnotateAttr(*this, ASDecl, *l);
3877 } else {
3878 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3879 return true;
3880 }
3881 }
3882
3883 return false;
3884}
3885
John McCalle82247a2011-10-01 05:17:03 +00003886/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3887/// contains any decl attributes that we should warn about.
3888static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3889 for ( ; A; A = A->getNext()) {
3890 // Only warn if the attribute is an unignored, non-type attribute.
3891 if (A->isUsedAsTypeAttr()) continue;
3892 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3893
3894 if (A->getKind() == AttributeList::UnknownAttribute) {
3895 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3896 << A->getName() << A->getRange();
3897 } else {
3898 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3899 << A->getName() << A->getRange();
3900 }
3901 }
3902}
3903
3904/// checkUnusedDeclAttributes - Given a declarator which is not being
3905/// used to build a declaration, complain about any decl attributes
3906/// which might be lying around on it.
3907void Sema::checkUnusedDeclAttributes(Declarator &D) {
3908 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3909 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3910 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3911 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3912}
3913
Ryan Flynne25ff832009-07-30 03:15:39 +00003914/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3915/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003916NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3917 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003918 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003919 NamedDecl *NewD = 0;
3920 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003921 FunctionDecl *NewFD;
3922 // FIXME: Missing call to CheckFunctionDeclaration().
3923 // FIXME: Mangling?
3924 // FIXME: Is the qualifier info correct?
3925 // FIXME: Is the DeclContext correct?
3926 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3927 Loc, Loc, DeclarationName(II),
3928 FD->getType(), FD->getTypeSourceInfo(),
3929 SC_None, SC_None,
3930 false/*isInlineSpecified*/,
3931 FD->hasPrototype(),
3932 false/*isConstexprSpecified*/);
3933 NewD = NewFD;
3934
3935 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003936 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003937
3938 // Fake up parameter variables; they are declared as if this were
3939 // a typedef.
3940 QualType FDTy = FD->getType();
3941 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3942 SmallVector<ParmVarDecl*, 16> Params;
3943 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3944 AE = FT->arg_type_end(); AI != AE; ++AI) {
3945 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3946 Param->setScopeInfo(0, Params.size());
3947 Params.push_back(Param);
3948 }
David Blaikie4278c652011-09-21 18:16:56 +00003949 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003950 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003951 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3952 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003953 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003954 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003955 VD->getStorageClass(),
3956 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003957 if (VD->getQualifier()) {
3958 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003959 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003960 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003961 }
3962 return NewD;
3963}
3964
3965/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3966/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003967void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003968 if (W.getUsed()) return; // only do this once
3969 W.setUsed(true);
3970 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3971 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003972 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003973 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3974 NDId->getName()));
3975 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003976 WeakTopLevelDecl.push_back(NewD);
3977 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3978 // to insert Decl at TU scope, sorry.
3979 DeclContext *SavedContext = CurContext;
3980 CurContext = Context.getTranslationUnitDecl();
3981 PushOnScopeChains(NewD, S);
3982 CurContext = SavedContext;
3983 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003984 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003985 }
3986}
3987
Chris Lattner0744e5f2008-06-29 00:23:49 +00003988/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3989/// it, apply them to D. This is a bit tricky because PD can have attributes
3990/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003991void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3992 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003993 // It's valid to "forward-declare" #pragma weak, in which case we
3994 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003995 if (Inheritable) {
3996 LoadExternalWeakUndeclaredIdentifiers();
3997 if (!WeakUndeclaredIdentifiers.empty()) {
3998 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3999 if (IdentifierInfo *Id = ND->getIdentifier()) {
4000 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4001 = WeakUndeclaredIdentifiers.find(Id);
4002 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4003 WeakInfo W = I->second;
4004 DeclApplyPragmaWeak(S, ND, W);
4005 WeakUndeclaredIdentifiers[Id] = W;
4006 }
John McCalld4aff0e2010-10-27 00:59:00 +00004007 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004008 }
4009 }
4010 }
4011
Chris Lattner0744e5f2008-06-29 00:23:49 +00004012 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004013 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004014 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004015
Chris Lattner0744e5f2008-06-29 00:23:49 +00004016 // Walk the declarator structure, applying decl attributes that were in a type
4017 // position to the decl itself. This handles cases like:
4018 // int *__attr__(x)** D;
4019 // when X is a decl attribute.
4020 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4021 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004022 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004023
Chris Lattner0744e5f2008-06-29 00:23:49 +00004024 // Finally, apply any attributes on the decl itself.
4025 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004026 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004027}
John McCall54abf7d2009-11-04 02:18:39 +00004028
John McCallf85e1932011-06-15 23:02:42 +00004029/// Is the given declaration allowed to use a forbidden type?
4030static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4031 // Private ivars are always okay. Unfortunately, people don't
4032 // always properly make their ivars private, even in system headers.
4033 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004034 // Function declarations in sys headers will be marked unavailable.
4035 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4036 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004037 return false;
4038
4039 // Require it to be declared in a system header.
4040 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4041}
4042
4043/// Handle a delayed forbidden-type diagnostic.
4044static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4045 Decl *decl) {
4046 if (decl && isForbiddenTypeAllowed(S, decl)) {
4047 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4048 "this system declaration uses an unsupported type"));
4049 return;
4050 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004051 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004052 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4053 // FIXME. we may want to supress diagnostics for all
4054 // kind of forbidden type messages on unavailable functions.
4055 if (FD->hasAttr<UnavailableAttr>() &&
4056 diag.getForbiddenTypeDiagnostic() ==
4057 diag::err_arc_array_param_no_ownership) {
4058 diag.Triggered = true;
4059 return;
4060 }
4061 }
John McCallf85e1932011-06-15 23:02:42 +00004062
4063 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4064 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4065 diag.Triggered = true;
4066}
4067
John McCalleee1d542011-02-14 07:13:47 +00004068// This duplicates a vector push_back but hides the need to know the
4069// size of the type.
4070void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4071 assert(StackSize <= StackCapacity);
4072
4073 // Grow the stack if necessary.
4074 if (StackSize == StackCapacity) {
4075 unsigned newCapacity = 2 * StackCapacity + 2;
4076 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4077 const char *oldBuffer = (const char*) Stack;
4078
4079 if (StackCapacity)
4080 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4081
4082 delete[] oldBuffer;
4083 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4084 StackCapacity = newCapacity;
4085 }
4086
4087 assert(StackSize < StackCapacity);
4088 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00004089}
4090
John McCalleee1d542011-02-14 07:13:47 +00004091void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4092 Decl *decl) {
4093 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00004094
John McCalleee1d542011-02-14 07:13:47 +00004095 // Check the invariants.
4096 assert(DD.StackSize >= state.SavedStackSize);
4097 assert(state.SavedStackSize >= DD.ActiveStackBase);
4098 assert(DD.ParsingDepth > 0);
4099
4100 // Drop the parsing depth.
4101 DD.ParsingDepth--;
4102
4103 // If there are no active diagnostics, we're done.
4104 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00004105 return;
4106
John McCall2f514482010-01-27 03:50:35 +00004107 // We only want to actually emit delayed diagnostics when we
4108 // successfully parsed a decl.
John McCalle8c904f2012-01-26 20:04:03 +00004109 if (decl) {
John McCalleee1d542011-02-14 07:13:47 +00004110 // We emit all the active diagnostics, not just those starting
4111 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00004112 // decl spec and another for each declarator; in a decl group like:
4113 // deprecated_typedef foo, *bar, baz();
4114 // only the declarator pops will be passed decls. This is correct;
4115 // we really do need to consider delayed diagnostics from the decl spec
4116 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00004117 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4118 DelayedDiagnostic &diag = DD.Stack[i];
4119 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004120 continue;
4121
John McCalleee1d542011-02-14 07:13:47 +00004122 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004123 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004124 // Don't bother giving deprecation diagnostics if the decl is invalid.
4125 if (!decl->isInvalidDecl())
4126 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004127 break;
4128
4129 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00004130 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004131 break;
John McCallf85e1932011-06-15 23:02:42 +00004132
4133 case DelayedDiagnostic::ForbiddenType:
4134 handleDelayedForbiddenType(S, diag, decl);
4135 break;
John McCall2f514482010-01-27 03:50:35 +00004136 }
4137 }
4138 }
4139
John McCall58e6f342010-03-16 05:22:47 +00004140 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00004141 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00004142 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00004143
John McCalleee1d542011-02-14 07:13:47 +00004144 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00004145}
4146
4147static bool isDeclDeprecated(Decl *D) {
4148 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004149 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004150 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004151 // A category implicitly has the availability of the interface.
4152 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4153 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004154 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4155 return false;
4156}
4157
John McCall9c3087b2010-08-26 02:13:20 +00004158void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004159 Decl *Ctx) {
4160 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004161 return;
4162
John McCall2f514482010-01-27 03:50:35 +00004163 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004164 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004165 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004166 << DD.getDeprecationDecl()->getDeclName()
4167 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004168 else if (DD.getUnknownObjCClass()) {
4169 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4170 << DD.getDeprecationDecl()->getDeclName();
4171 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4172 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004173 else
4174 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004175 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004176}
4177
Chris Lattner5f9e2722011-07-23 10:55:15 +00004178void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004179 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004180 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004181 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004182 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004183 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4184 UnknownObjCClass,
4185 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004186 return;
4187 }
4188
4189 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004190 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004191 return;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004192 if (!Message.empty()) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004193 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4194 << Message;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004195 Diag(D->getLocation(),
4196 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4197 : diag::note_previous_decl) << D->getDeclName();
4198 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004199 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00004200 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004201 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004202 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004203 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004204 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4205 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004206 }
John McCall54abf7d2009-11-04 02:18:39 +00004207}