blob: 0e1c43c7a476d20ebfc23815c4fc8ea1f72cca79 [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
Rafael Espindola45a0b262012-04-26 01:26:03 +00001755 VisibilityAttr *PrevAttr = D->getCanonicalDecl()->getAttr<VisibilityAttr>();
1756 if (PrevAttr) {
1757 VisibilityAttr::VisibilityType PrevVisibility = PrevAttr->getVisibility();
1758 if (PrevVisibility != type) {
1759 S.Diag(Attr.getLoc(), diag::err_mismatched_visibilit);
1760 S.Diag(PrevAttr->getLocation(), diag::note_previous_attribute);
1761 return;
1762 }
1763 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001764 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001765}
1766
Chandler Carruth1b03c872011-07-02 00:01:44 +00001767static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1768 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001769 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1770 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001771 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001772 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001773 return;
1774 }
1775
Chandler Carruth87c44602011-07-01 23:49:12 +00001776 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1777 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1778 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001779 << "objc_method_family" << 1;
1780 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001782 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001783 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001784 return;
1785 }
1786
Chris Lattner5f9e2722011-07-23 10:55:15 +00001787 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001788 ObjCMethodFamilyAttr::FamilyKind family;
1789 if (param == "none")
1790 family = ObjCMethodFamilyAttr::OMF_None;
1791 else if (param == "alloc")
1792 family = ObjCMethodFamilyAttr::OMF_alloc;
1793 else if (param == "copy")
1794 family = ObjCMethodFamilyAttr::OMF_copy;
1795 else if (param == "init")
1796 family = ObjCMethodFamilyAttr::OMF_init;
1797 else if (param == "mutableCopy")
1798 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1799 else if (param == "new")
1800 family = ObjCMethodFamilyAttr::OMF_new;
1801 else {
1802 // Just warn and ignore it. This is future-proof against new
1803 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001804 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001805 return;
1806 }
1807
John McCallf85e1932011-06-15 23:02:42 +00001808 if (family == ObjCMethodFamilyAttr::OMF_init &&
1809 !method->getResultType()->isObjCObjectPointerType()) {
1810 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1811 << method->getResultType();
1812 // Ignore the attribute.
1813 return;
1814 }
1815
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001816 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001817 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001818}
1819
Chandler Carruth1b03c872011-07-02 00:01:44 +00001820static void handleObjCExceptionAttr(Sema &S, Decl *D,
1821 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001822 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001823 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001824
Chris Lattner0db29ec2009-02-14 08:09:34 +00001825 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1826 if (OCI == 0) {
1827 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1828 return;
1829 }
Mike Stumpbf916502009-07-24 19:02:52 +00001830
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001831 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001832}
1833
Chandler Carruth1b03c872011-07-02 00:01:44 +00001834static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001835 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001836 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001837 return;
1838 }
Richard Smith162e1c12011-04-15 14:24:37 +00001839 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001840 QualType T = TD->getUnderlyingType();
1841 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001842 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001843 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1844 return;
1845 }
1846 }
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001847 else if (!isa<ObjCPropertyDecl>(D)) {
1848 // It is okay to include this attribute on properties, e.g.:
1849 //
1850 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1851 //
1852 // In this case it follows tradition and suppresses an error in the above
1853 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00001854 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001855 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001856 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001857}
1858
Mike Stumpbf916502009-07-24 19:02:52 +00001859static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001860handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001861 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001862 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001863 return;
1864 }
1865
1866 if (!isa<FunctionDecl>(D)) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1868 return;
1869 }
1870
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001871 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001872}
1873
Chandler Carruth1b03c872011-07-02 00:01:44 +00001874static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001875 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001876 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001877 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001878 return;
1879 }
Mike Stumpbf916502009-07-24 19:02:52 +00001880
Steve Naroff9eae5762008-09-18 16:44:58 +00001881 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001882 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001883 return;
1884 }
Mike Stumpbf916502009-07-24 19:02:52 +00001885
Sean Huntcf807c42010-08-18 23:23:40 +00001886 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001887 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001888 type = BlocksAttr::ByRef;
1889 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001890 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001891 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001892 return;
1893 }
Mike Stumpbf916502009-07-24 19:02:52 +00001894
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001895 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001896}
1897
Chandler Carruth1b03c872011-07-02 00:01:44 +00001898static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001899 // check the attribute arguments.
1900 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001902 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001903 }
1904
John McCall3323fad2011-09-09 07:56:05 +00001905 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001906 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001907 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001908 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001909 if (E->isTypeDependent() || E->isValueDependent() ||
1910 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001911 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001912 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001913 return;
1914 }
Mike Stumpbf916502009-07-24 19:02:52 +00001915
John McCall3323fad2011-09-09 07:56:05 +00001916 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1918 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001919 return;
1920 }
John McCall3323fad2011-09-09 07:56:05 +00001921
1922 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001923 }
1924
John McCall3323fad2011-09-09 07:56:05 +00001925 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001926 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001927 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001928 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001929 if (E->isTypeDependent() || E->isValueDependent() ||
1930 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001931 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001932 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001933 return;
1934 }
1935 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001936
John McCall3323fad2011-09-09 07:56:05 +00001937 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001938 // FIXME: This error message could be improved, it would be nice
1939 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001940 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1941 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001942 return;
1943 }
1944 }
1945
Chandler Carruth87c44602011-07-01 23:49:12 +00001946 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001947 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001948 if (isa<FunctionNoProtoType>(FT)) {
1949 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1950 return;
1951 }
Mike Stumpbf916502009-07-24 19:02:52 +00001952
Chris Lattner897cd902009-03-17 23:03:47 +00001953 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001954 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001955 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001956 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001957 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001958 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001959 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001960 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001961 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00001962 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1963 if (!BD->isVariadic()) {
1964 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1965 return;
1966 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001967 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001968 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001969 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001970 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001971 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001972 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001973 int m = Ty->isFunctionPointerType() ? 0 : 1;
1974 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001975 return;
1976 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001977 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001978 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001979 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001980 return;
1981 }
Anders Carlsson77091822008-10-05 18:05:59 +00001982 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001984 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001985 return;
1986 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001987 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001988 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001989}
1990
Chandler Carruth1b03c872011-07-02 00:01:44 +00001991static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001992 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001993 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001994 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001995
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001996 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001998 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001999 return;
2000 }
Mike Stumpbf916502009-07-24 19:02:52 +00002001
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002002 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2003 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2004 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002005 return;
2006 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002007 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2008 if (MD->getResultType()->isVoidType()) {
2009 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2010 << Attr.getName() << 1;
2011 return;
2012 }
2013
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002014 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002015}
2016
Chandler Carruth1b03c872011-07-02 00:01:44 +00002017static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002018 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002019 if (Attr.hasParameterOrArguments()) {
2020 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002021 return;
2022 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002023
Chandler Carruth87c44602011-07-01 23:49:12 +00002024 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002025 if (isa<CXXRecordDecl>(D)) {
2026 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2027 return;
2028 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002029 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2030 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002031 return;
2032 }
2033
Chandler Carruth87c44602011-07-01 23:49:12 +00002034 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002035
2036 // 'weak' only applies to declarations with external linkage.
2037 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002038 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002039 return;
2040 }
Mike Stumpbf916502009-07-24 19:02:52 +00002041
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002042 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002043}
2044
Chandler Carruth1b03c872011-07-02 00:01:44 +00002045static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002046 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002047 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002048 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002049
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002050
2051 // weak_import only applies to variable & function declarations.
2052 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002053 if (!D->canBeWeakImported(isDef)) {
2054 if (isDef)
2055 S.Diag(Attr.getLoc(),
2056 diag::warn_attribute_weak_import_invalid_on_definition)
2057 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002058 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002059 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002060 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002061 // Nothing to warn about here.
2062 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002063 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002064 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002065
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002066 return;
2067 }
2068
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002069 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002070}
2071
Chandler Carruth1b03c872011-07-02 00:01:44 +00002072static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2073 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002074 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002075 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002076 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002077
2078 unsigned WGSize[3];
2079 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002080 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002081 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002082 if (E->isTypeDependent() || E->isValueDependent() ||
2083 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002084 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2085 << "reqd_work_group_size" << E->getSourceRange();
2086 return;
2087 }
2088 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2089 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002090 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002091 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002092 WGSize[2]));
2093}
2094
Chandler Carruth1b03c872011-07-02 00:01:44 +00002095static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002096 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002097 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002098 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002099
2100 // Make sure that there is a string literal as the sections's single
2101 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002102 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002103 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002104 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002105 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002106 return;
2107 }
Mike Stump1eb44332009-09-09 15:08:12 +00002108
Chris Lattner797c3c42009-08-10 19:03:04 +00002109 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002110 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002111 if (!Error.empty()) {
2112 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2113 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002114 return;
2115 }
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002117 // This attribute cannot be applied to local variables.
2118 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2119 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2120 return;
2121 }
2122
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002123 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002124 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002125}
2126
Chris Lattner6b6b5372008-06-26 18:38:35 +00002127
Chandler Carruth1b03c872011-07-02 00:01:44 +00002128static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002129 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002130 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002132 return;
2133 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002134
Chandler Carruth87c44602011-07-01 23:49:12 +00002135 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002136 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002137 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002138 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002139 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002140 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002141}
2142
Chandler Carruth1b03c872011-07-02 00:01:44 +00002143static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002144 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002145 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002146 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002147 return;
2148 }
Mike Stumpbf916502009-07-24 19:02:52 +00002149
Chandler Carruth87c44602011-07-01 23:49:12 +00002150 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002151 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002152 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002153 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002154 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002155 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002156}
2157
Chandler Carruth1b03c872011-07-02 00:01:44 +00002158static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002159 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002160 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002161 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002162
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002163 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002164}
2165
Chandler Carruth1b03c872011-07-02 00:01:44 +00002166static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002167 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2169 return;
2170 }
Mike Stumpbf916502009-07-24 19:02:52 +00002171
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002172 if (Attr.getNumArgs() != 0) {
2173 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2174 return;
2175 }
Mike Stumpbf916502009-07-24 19:02:52 +00002176
Chandler Carruth87c44602011-07-01 23:49:12 +00002177 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002178
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002179 if (!VD || !VD->hasLocalStorage()) {
2180 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2181 return;
2182 }
Mike Stumpbf916502009-07-24 19:02:52 +00002183
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002184 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002185 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002186 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002187 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2188 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002189 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002190 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002191 Attr.getParameterName();
2192 return;
2193 }
Mike Stumpbf916502009-07-24 19:02:52 +00002194
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002195 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2196 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002197 S.Diag(Attr.getParameterLoc(),
2198 diag::err_attribute_cleanup_arg_not_function)
2199 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002200 return;
2201 }
2202
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002203 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002204 S.Diag(Attr.getParameterLoc(),
2205 diag::err_attribute_cleanup_func_must_take_one_arg)
2206 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002207 return;
2208 }
Mike Stumpbf916502009-07-24 19:02:52 +00002209
Anders Carlsson89941c12009-02-07 23:16:50 +00002210 // We're currently more strict than GCC about what function types we accept.
2211 // If this ever proves to be a problem it should be easy to fix.
2212 QualType Ty = S.Context.getPointerType(VD->getType());
2213 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002214 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2215 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002216 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002217 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2218 Attr.getParameterName() << ParamTy << Ty;
2219 return;
2220 }
Mike Stumpbf916502009-07-24 19:02:52 +00002221
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002222 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002223 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002224}
2225
Mike Stumpbf916502009-07-24 19:02:52 +00002226/// Handle __attribute__((format_arg((idx)))) attribute based on
2227/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002228static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002229 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002230 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002231
Chandler Carruth87c44602011-07-01 23:49:12 +00002232 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002233 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002234 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002235 return;
2236 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002237
2238 // In C++ the implicit 'this' function parameter also counts, and they are
2239 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002240 bool HasImplicitThisParam = isInstanceMethod(D);
2241 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002242 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002243
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002244 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002245 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002246 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002247 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2248 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2250 << "format" << 2 << IdxExpr->getSourceRange();
2251 return;
2252 }
Mike Stumpbf916502009-07-24 19:02:52 +00002253
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002254 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2255 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2256 << "format" << 2 << IdxExpr->getSourceRange();
2257 return;
2258 }
Mike Stumpbf916502009-07-24 19:02:52 +00002259
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002260 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002261
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002262 if (HasImplicitThisParam) {
2263 if (ArgIdx == 0) {
2264 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2265 << "format_arg" << IdxExpr->getSourceRange();
2266 return;
2267 }
2268 ArgIdx--;
2269 }
2270
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002271 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002272 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002273
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002274 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2275 if (not_nsstring_type &&
2276 !isCFStringType(Ty, S.Context) &&
2277 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002278 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002279 // FIXME: Should highlight the actual expression that has the wrong type.
2280 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002281 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002282 << IdxExpr->getSourceRange();
2283 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002284 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002285 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002286 if (!isNSStringType(Ty, S.Context) &&
2287 !isCFStringType(Ty, S.Context) &&
2288 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002289 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002290 // FIXME: Should highlight the actual expression that has the wrong type.
2291 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002292 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002293 << IdxExpr->getSourceRange();
2294 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002295 }
2296
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002297 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002298 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002299}
2300
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002301enum FormatAttrKind {
2302 CFStringFormat,
2303 NSStringFormat,
2304 StrftimeFormat,
2305 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002306 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002307 InvalidFormat
2308};
2309
2310/// getFormatAttrKind - Map from format attribute names to supported format
2311/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002312static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002313 // Check for formats that get handled specially.
2314 if (Format == "NSString")
2315 return NSStringFormat;
2316 if (Format == "CFString")
2317 return CFStringFormat;
2318 if (Format == "strftime")
2319 return StrftimeFormat;
2320
2321 // Otherwise, check for supported formats.
2322 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupas69d53842012-01-27 09:14:17 +00002323 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002324 Format == "zcmn_err" ||
2325 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002326 return SupportedFormat;
2327
Duncan Sandsbc525952010-03-23 14:44:19 +00002328 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2329 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002330 return IgnoredFormat;
2331
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002332 return InvalidFormat;
2333}
2334
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002335/// Handle __attribute__((init_priority(priority))) attributes based on
2336/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002337static void handleInitPriorityAttr(Sema &S, Decl *D,
2338 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002339 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002340 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2341 return;
2342 }
2343
Chandler Carruth87c44602011-07-01 23:49:12 +00002344 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002345 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2346 Attr.setInvalid();
2347 return;
2348 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002349 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002350 if (S.Context.getAsArrayType(T))
2351 T = S.Context.getBaseElementType(T);
2352 if (!T->getAs<RecordType>()) {
2353 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2354 Attr.setInvalid();
2355 return;
2356 }
2357
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002358 if (Attr.getNumArgs() != 1) {
2359 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2360 Attr.setInvalid();
2361 return;
2362 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002363 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002364
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002365 llvm::APSInt priority(32);
2366 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2367 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2368 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2369 << "init_priority" << priorityExpr->getSourceRange();
2370 Attr.setInvalid();
2371 return;
2372 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002373 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002374 if (prioritynum < 101 || prioritynum > 65535) {
2375 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2376 << priorityExpr->getSourceRange();
2377 Attr.setInvalid();
2378 return;
2379 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002380 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002381 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002382}
2383
Mike Stumpbf916502009-07-24 19:02:52 +00002384/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2385/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002386static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002387
Chris Lattner545dd342008-06-28 23:36:30 +00002388 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002389 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002390 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002391 return;
2392 }
2393
Chris Lattner545dd342008-06-28 23:36:30 +00002394 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002396 return;
2397 }
2398
Chandler Carruth87c44602011-07-01 23:49:12 +00002399 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002400 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002401 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002402 return;
2403 }
2404
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002405 // In C++ the implicit 'this' function parameter also counts, and they are
2406 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002407 bool HasImplicitThisParam = isInstanceMethod(D);
2408 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002409 unsigned FirstIdx = 1;
2410
Chris Lattner5f9e2722011-07-23 10:55:15 +00002411 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002412
2413 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002414 if (Format.startswith("__") && Format.endswith("__"))
2415 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002416
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002417 // Check for supported formats.
2418 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002419
2420 if (Kind == IgnoredFormat)
2421 return;
2422
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002423 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002424 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002425 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002426 return;
2427 }
2428
2429 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002430 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002431 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002432 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2433 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002434 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002435 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002436 return;
2437 }
2438
2439 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002440 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002441 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002442 return;
2443 }
2444
2445 // FIXME: Do we need to bounds check?
2446 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002447
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002448 if (HasImplicitThisParam) {
2449 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002450 S.Diag(Attr.getLoc(),
2451 diag::err_format_attribute_implicit_this_format_string)
2452 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002453 return;
2454 }
2455 ArgIdx--;
2456 }
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Chris Lattner6b6b5372008-06-26 18:38:35 +00002458 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002459 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002460
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002461 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002462 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002463 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2464 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002465 return;
2466 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002467 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002468 // FIXME: do we need to check if the type is NSString*? What are the
2469 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002470 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002471 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002472 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2473 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002474 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002475 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002476 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002477 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002478 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002479 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2480 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002481 return;
2482 }
2483
2484 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002485 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002486 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002487 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2488 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002489 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002490 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002491 return;
2492 }
2493
2494 // check if the function is variadic if the 3rd argument non-zero
2495 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002496 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002497 ++NumArgs; // +1 for ...
2498 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002499 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002500 return;
2501 }
2502 }
2503
Chris Lattner3c73c412008-11-19 08:23:25 +00002504 // strftime requires FirstArg to be 0 because it doesn't read from any
2505 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002506 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002507 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002508 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2509 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002510 return;
2511 }
2512 // if 0 it disables parameter checking (to use with e.g. va_list)
2513 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002515 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002516 return;
2517 }
2518
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002519 // Check whether we already have an equivalent format attribute.
2520 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002521 i = D->specific_attr_begin<FormatAttr>(),
2522 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002523 i != e ; ++i) {
2524 FormatAttr *f = *i;
2525 if (f->getType() == Format &&
2526 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2527 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2528 // If we don't have a valid location for this attribute, adopt the
2529 // location.
2530 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002531 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002532 return;
2533 }
2534 }
2535
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002536 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002537 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002538 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002539}
2540
Chandler Carruth1b03c872011-07-02 00:01:44 +00002541static void handleTransparentUnionAttr(Sema &S, Decl *D,
2542 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002543 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002544 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002545 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002546
Chris Lattner6b6b5372008-06-26 18:38:35 +00002547
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002548 // Try to find the underlying union declaration.
2549 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002550 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002551 if (TD && TD->getUnderlyingType()->isUnionType())
2552 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2553 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002554 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002555
2556 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002557 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002558 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002559 return;
2560 }
2561
John McCall5e1cdac2011-10-07 06:10:15 +00002562 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002563 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002564 diag::warn_transparent_union_attribute_not_definition);
2565 return;
2566 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002567
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002568 RecordDecl::field_iterator Field = RD->field_begin(),
2569 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002570 if (Field == FieldEnd) {
2571 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2572 return;
2573 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002574
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002575 FieldDecl *FirstField = *Field;
2576 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002577 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002578 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002579 diag::warn_transparent_union_attribute_floating)
2580 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002581 return;
2582 }
2583
2584 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2585 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2586 for (; Field != FieldEnd; ++Field) {
2587 QualType FieldType = Field->getType();
2588 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2589 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2590 // Warn if we drop the attribute.
2591 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002592 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002593 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002594 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002595 diag::warn_transparent_union_attribute_field_size_align)
2596 << isSize << Field->getDeclName() << FieldBits;
2597 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002598 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002599 diag::note_transparent_union_first_field_size_align)
2600 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002601 return;
2602 }
2603 }
2604
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002605 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002606}
2607
Chandler Carruth1b03c872011-07-02 00:01:44 +00002608static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002609 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002610 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002611 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002612
Peter Collingbourne7a730022010-11-23 20:45:58 +00002613 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002614 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002615
Chris Lattner6b6b5372008-06-26 18:38:35 +00002616 // Make sure that there is a string literal as the annotation's single
2617 // argument.
2618 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002619 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002620 return;
2621 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002622
2623 // Don't duplicate annotations that are already set.
2624 for (specific_attr_iterator<AnnotateAttr>
2625 i = D->specific_attr_begin<AnnotateAttr>(),
2626 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2627 if ((*i)->getAnnotation() == SE->getString())
2628 return;
2629 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002630 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002631 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002632}
2633
Chandler Carruth1b03c872011-07-02 00:01:44 +00002634static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002635 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002636 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002638 return;
2639 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002640
2641 //FIXME: The C++0x version of this attribute has more limited applicabilty
2642 // than GNU's, and should error out when it is used to specify a
2643 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002644
Chris Lattner545dd342008-06-28 23:36:30 +00002645 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002646 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002647 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002648 }
Mike Stumpbf916502009-07-24 19:02:52 +00002649
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002650 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002651}
2652
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002653void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002654 // FIXME: Handle pack-expansions here.
2655 if (DiagnoseUnexpandedParameterPack(E))
2656 return;
2657
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002658 if (E->isTypeDependent() || E->isValueDependent()) {
2659 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002660 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002661 return;
2662 }
2663
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002664 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002665 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002666 llvm::APSInt Alignment(32);
Richard Smith282e7e62012-02-04 09:53:13 +00002667 ExprResult ICE =
2668 VerifyIntegerConstantExpression(E, &Alignment,
2669 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2670 /*AllowFold*/ false);
2671 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002672 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002673 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002674 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2675 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002676 return;
2677 }
2678
Richard Smith282e7e62012-02-04 09:53:13 +00002679 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Sean Huntcf807c42010-08-18 23:23:40 +00002680}
2681
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002682void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002683 // FIXME: Cache the number on the Attr object if non-dependent?
2684 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002685 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002686 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002687}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002688
Chandler Carruthd309c812011-07-01 23:49:16 +00002689/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002690/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002691///
Mike Stumpbf916502009-07-24 19:02:52 +00002692/// Despite what would be logical, the mode attribute is a decl attribute, not a
2693/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2694/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002695static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002696 // This attribute isn't documented, but glibc uses it. It changes
2697 // the width of an int or unsigned int to the specified size.
2698
2699 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002700 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002701 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002702
Chris Lattnerfbf13472008-06-27 22:18:37 +00002703
2704 IdentifierInfo *Name = Attr.getParameterName();
2705 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002706 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002707 return;
2708 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002709
Chris Lattner5f9e2722011-07-23 10:55:15 +00002710 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002711
2712 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002713 if (Str.startswith("__") && Str.endswith("__"))
2714 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002715
2716 unsigned DestWidth = 0;
2717 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002718 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002719 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002720 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002721 switch (Str[0]) {
2722 case 'Q': DestWidth = 8; break;
2723 case 'H': DestWidth = 16; break;
2724 case 'S': DestWidth = 32; break;
2725 case 'D': DestWidth = 64; break;
2726 case 'X': DestWidth = 96; break;
2727 case 'T': DestWidth = 128; break;
2728 }
2729 if (Str[1] == 'F') {
2730 IntegerMode = false;
2731 } else if (Str[1] == 'C') {
2732 IntegerMode = false;
2733 ComplexMode = true;
2734 } else if (Str[1] != 'I') {
2735 DestWidth = 0;
2736 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002737 break;
2738 case 4:
2739 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2740 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002741 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002742 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002743 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002744 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002745 break;
2746 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002747 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002748 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002749 break;
2750 }
2751
2752 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002753 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002754 OldTy = TD->getUnderlyingType();
2755 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2756 OldTy = VD->getType();
2757 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002758 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002759 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002760 return;
2761 }
Eli Friedman73397492009-03-03 06:41:03 +00002762
John McCall183700f2009-09-21 23:43:11 +00002763 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002764 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2765 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002766 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002767 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2768 } else if (ComplexMode) {
2769 if (!OldTy->isComplexType())
2770 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2771 } else {
2772 if (!OldTy->isFloatingType())
2773 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2774 }
2775
Mike Stump390b4cc2009-05-16 07:39:55 +00002776 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2777 // and friends, at least with glibc.
2778 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2779 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002780 // FIXME: Make sure floating-point mappings are accurate
2781 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002782 QualType NewTy;
2783 switch (DestWidth) {
2784 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002785 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002786 return;
2787 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002788 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002789 return;
2790 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002791 if (!IntegerMode) {
2792 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2793 return;
2794 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002795 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002796 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002797 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002798 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002799 break;
2800 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002801 if (!IntegerMode) {
2802 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2803 return;
2804 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002805 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002806 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002807 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002808 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002809 break;
2810 case 32:
2811 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002812 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002813 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002814 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002815 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002816 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002817 break;
2818 case 64:
2819 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002820 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002821 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002822 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002823 NewTy = S.Context.LongTy;
2824 else
2825 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002826 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002827 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002828 NewTy = S.Context.UnsignedLongTy;
2829 else
2830 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002831 break;
Eli Friedman73397492009-03-03 06:41:03 +00002832 case 96:
2833 NewTy = S.Context.LongDoubleTy;
2834 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002835 case 128:
2836 if (!IntegerMode) {
2837 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2838 return;
2839 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002840 if (OldTy->isSignedIntegerType())
2841 NewTy = S.Context.Int128Ty;
2842 else
2843 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002844 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002845 }
2846
Eli Friedman73397492009-03-03 06:41:03 +00002847 if (ComplexMode) {
2848 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002849 }
2850
2851 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002852 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002853 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002854 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002855 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002856 cast<ValueDecl>(D)->setType(NewTy);
2857}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002858
Chandler Carruth1b03c872011-07-02 00:01:44 +00002859static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002860 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002861 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002862 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002863
Chandler Carruth87c44602011-07-01 23:49:12 +00002864 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002866 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002867 return;
2868 }
Mike Stumpbf916502009-07-24 19:02:52 +00002869
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002870 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002871}
2872
Chandler Carruth1b03c872011-07-02 00:01:44 +00002873static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002874 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002875 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002876 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002877
Mike Stumpbf916502009-07-24 19:02:52 +00002878
Chandler Carruth87c44602011-07-01 23:49:12 +00002879 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002880 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002881 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002882 return;
2883 }
Mike Stumpbf916502009-07-24 19:02:52 +00002884
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002885 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002886}
2887
Chandler Carruth1b03c872011-07-02 00:01:44 +00002888static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2889 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002890 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002891 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002892 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002893
Chris Lattner7255a2d2010-06-22 00:03:40 +00002894
Chandler Carruth87c44602011-07-01 23:49:12 +00002895 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002897 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002898 return;
2899 }
2900
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002901 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002902 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002903}
2904
Chandler Carruth1b03c872011-07-02 00:01:44 +00002905static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002906 if (S.LangOpts.CUDA) {
2907 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002908 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2910 return;
2911 }
2912
Chandler Carruth87c44602011-07-01 23:49:12 +00002913 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002914 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002915 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002916 return;
2917 }
2918
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002919 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002920 } else {
2921 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2922 }
2923}
2924
Chandler Carruth1b03c872011-07-02 00:01:44 +00002925static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002926 if (S.LangOpts.CUDA) {
2927 // check the attribute arguments.
2928 if (Attr.getNumArgs() != 0) {
2929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2930 return;
2931 }
2932
Chandler Carruth87c44602011-07-01 23:49:12 +00002933 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002935 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002936 return;
2937 }
2938
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002939 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002940 } else {
2941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2942 }
2943}
2944
Chandler Carruth1b03c872011-07-02 00:01:44 +00002945static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002946 if (S.LangOpts.CUDA) {
2947 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002948 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002949 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002950
Chandler Carruth87c44602011-07-01 23:49:12 +00002951 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002953 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002954 return;
2955 }
2956
Chandler Carruth87c44602011-07-01 23:49:12 +00002957 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002958 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002959 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002960 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2961 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2962 << FD->getType()
2963 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2964 "void");
2965 } else {
2966 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2967 << FD->getType();
2968 }
2969 return;
2970 }
2971
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002972 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002973 } else {
2974 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2975 }
2976}
2977
Chandler Carruth1b03c872011-07-02 00:01:44 +00002978static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002979 if (S.LangOpts.CUDA) {
2980 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002981 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002982 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002983
Peter Collingbourneced76712010-12-01 03:15:31 +00002984
Chandler Carruth87c44602011-07-01 23:49:12 +00002985 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002986 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002987 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002988 return;
2989 }
2990
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002991 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002992 } else {
2993 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2994 }
2995}
2996
Chandler Carruth1b03c872011-07-02 00:01:44 +00002997static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002998 if (S.LangOpts.CUDA) {
2999 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003000 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003001 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003002
Peter Collingbourneced76712010-12-01 03:15:31 +00003003
Chandler Carruth87c44602011-07-01 23:49:12 +00003004 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003006 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003007 return;
3008 }
3009
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003010 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003011 } else {
3012 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3013 }
3014}
3015
Chandler Carruth1b03c872011-07-02 00:01:44 +00003016static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003017 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003018 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003019 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003020
Chandler Carruth87c44602011-07-01 23:49:12 +00003021 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003022 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003023 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003024 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003025 return;
3026 }
Mike Stumpbf916502009-07-24 19:02:52 +00003027
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003028 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003029 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003030 return;
3031 }
Mike Stumpbf916502009-07-24 19:02:52 +00003032
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003033 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003034}
3035
Chandler Carruth1b03c872011-07-02 00:01:44 +00003036static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003037 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003038
Chandler Carruth87c44602011-07-01 23:49:12 +00003039 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003040 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3041 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003042 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003043 return;
3044
Chandler Carruth87c44602011-07-01 23:49:12 +00003045 if (!isa<ObjCMethodDecl>(D)) {
3046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3047 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003048 return;
3049 }
3050
Chandler Carruth87c44602011-07-01 23:49:12 +00003051 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00003052 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003053 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003054 return;
3055 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003056 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003057 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003058 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003059 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003060 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003061 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003062 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003063 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003064 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003065 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003066 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003067 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003068 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003069 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003070 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003071 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003072 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003073 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003074 return;
3075 }
3076
Chris Lattner5f9e2722011-07-23 10:55:15 +00003077 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003078 PcsAttr::PCSType PCS;
3079 if (StrRef == "aapcs")
3080 PCS = PcsAttr::AAPCS;
3081 else if (StrRef == "aapcs-vfp")
3082 PCS = PcsAttr::AAPCS_VFP;
3083 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003084 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3085 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003086 return;
3087 }
3088
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003089 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003090 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003091 default:
3092 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003093 }
3094}
3095
Chandler Carruth1b03c872011-07-02 00:01:44 +00003096static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003097 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003098 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003099}
3100
John McCall711c52b2011-01-05 12:14:39 +00003101bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3102 if (attr.isInvalid())
3103 return true;
3104
Ted Kremenek831efae2011-04-15 05:49:29 +00003105 if ((attr.getNumArgs() != 0 &&
3106 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3107 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003108 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3109 attr.setInvalid();
3110 return true;
3111 }
3112
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003113 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3114 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003115 switch (attr.getKind()) {
3116 case AttributeList::AT_cdecl: CC = CC_C; break;
3117 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3118 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3119 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3120 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003121 case AttributeList::AT_pcs: {
3122 Expr *Arg = attr.getArg(0);
3123 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003124 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003125 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3126 << "pcs" << 1;
3127 attr.setInvalid();
3128 return true;
3129 }
3130
Chris Lattner5f9e2722011-07-23 10:55:15 +00003131 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003132 if (StrRef == "aapcs") {
3133 CC = CC_AAPCS;
3134 break;
3135 } else if (StrRef == "aapcs-vfp") {
3136 CC = CC_AAPCS_VFP;
3137 break;
3138 }
3139 // FALLS THROUGH
3140 }
David Blaikie7530c032012-01-17 06:56:22 +00003141 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003142 }
3143
3144 return false;
3145}
3146
Chandler Carruth1b03c872011-07-02 00:01:44 +00003147static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003148 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003149
3150 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003151 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003152 return;
3153
Chandler Carruth87c44602011-07-01 23:49:12 +00003154 if (!isa<ObjCMethodDecl>(D)) {
3155 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3156 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003157 return;
3158 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003159
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003160 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003161}
3162
3163/// Checks a regparm attribute, returning true if it is ill-formed and
3164/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003165bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3166 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003167 return true;
3168
Chandler Carruth87c44602011-07-01 23:49:12 +00003169 if (Attr.getNumArgs() != 1) {
3170 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3171 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003172 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003173 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003174
Chandler Carruth87c44602011-07-01 23:49:12 +00003175 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003176 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003177 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003178 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003179 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003180 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003181 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003182 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003183 }
3184
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003185 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003186 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003187 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003188 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003189 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003190 }
3191
John McCall711c52b2011-01-05 12:14:39 +00003192 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003193 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003194 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003195 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003196 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003197 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003198 }
3199
John McCall711c52b2011-01-05 12:14:39 +00003200 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003201}
3202
Chandler Carruth1b03c872011-07-02 00:01:44 +00003203static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003204 if (S.LangOpts.CUDA) {
3205 // check the attribute arguments.
3206 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003207 // FIXME: 0 is not okay.
3208 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003209 return;
3210 }
3211
Chandler Carruth87c44602011-07-01 23:49:12 +00003212 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003213 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003214 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003215 return;
3216 }
3217
3218 Expr *MaxThreadsExpr = Attr.getArg(0);
3219 llvm::APSInt MaxThreads(32);
3220 if (MaxThreadsExpr->isTypeDependent() ||
3221 MaxThreadsExpr->isValueDependent() ||
3222 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3224 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3225 return;
3226 }
3227
3228 llvm::APSInt MinBlocks(32);
3229 if (Attr.getNumArgs() > 1) {
3230 Expr *MinBlocksExpr = Attr.getArg(1);
3231 if (MinBlocksExpr->isTypeDependent() ||
3232 MinBlocksExpr->isValueDependent() ||
3233 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3235 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3236 return;
3237 }
3238 }
3239
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003240 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003241 MaxThreads.getZExtValue(),
3242 MinBlocks.getZExtValue()));
3243 } else {
3244 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3245 }
3246}
3247
Chris Lattner0744e5f2008-06-29 00:23:49 +00003248//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003249// Checker-specific attribute handlers.
3250//===----------------------------------------------------------------------===//
3251
John McCallc7ad3812011-01-25 03:31:58 +00003252static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003253 return type->isDependentType() ||
3254 type->isObjCObjectPointerType() ||
3255 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003256}
3257static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003258 return type->isDependentType() ||
3259 type->isPointerType() ||
3260 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003261}
3262
Chandler Carruth1b03c872011-07-02 00:01:44 +00003263static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003264 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003265 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003266 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003267 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003268 return;
3269 }
3270
3271 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003272 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003273 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3274 cf = false;
3275 } else {
3276 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3277 cf = true;
3278 }
3279
3280 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003281 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003282 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003283 return;
3284 }
3285
3286 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003287 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003288 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003289 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003290}
3291
Chandler Carruth1b03c872011-07-02 00:01:44 +00003292static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3293 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003294 if (!isa<ObjCMethodDecl>(D)) {
3295 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003296 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003297 return;
3298 }
3299
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003300 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003301}
3302
Chandler Carruth1b03c872011-07-02 00:01:44 +00003303static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3304 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003305
John McCallc7ad3812011-01-25 03:31:58 +00003306 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003307
Chandler Carruth87c44602011-07-01 23:49:12 +00003308 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003309 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003310 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003311 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003312 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruth87c44602011-07-01 23:49:12 +00003313 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003314 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003315 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003316 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003317 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003318 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003319 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003320 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003321 return;
3322 }
Mike Stumpbf916502009-07-24 19:02:52 +00003323
John McCallc7ad3812011-01-25 03:31:58 +00003324 bool typeOK;
3325 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003326 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003327 default: llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003328 case AttributeList::AT_ns_returns_autoreleased:
3329 case AttributeList::AT_ns_returns_retained:
3330 case AttributeList::AT_ns_returns_not_retained:
3331 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3332 cf = false;
3333 break;
3334
3335 case AttributeList::AT_cf_returns_retained:
3336 case AttributeList::AT_cf_returns_not_retained:
3337 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3338 cf = true;
3339 break;
3340 }
3341
3342 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003343 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003344 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003345 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003346 }
Mike Stumpbf916502009-07-24 19:02:52 +00003347
Chandler Carruth87c44602011-07-01 23:49:12 +00003348 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003349 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003350 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003351 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003352 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003353 S.Context));
3354 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003355 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003356 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003357 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003358 return;
3359 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003360 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003361 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003362 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003363 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003364 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003365 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003366 return;
3367 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003368 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003369 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003370 return;
3371 };
3372}
3373
John McCalldc7c5ad2011-07-22 08:53:00 +00003374static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3375 const AttributeList &attr) {
3376 SourceLocation loc = attr.getLoc();
3377
3378 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3379
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003380 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003381 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003382 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003383 return;
3384 }
3385
3386 // Check that the method returns a normal pointer.
3387 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003388
3389 if (!resultType->isReferenceType() &&
3390 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003391 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3392 << SourceRange(loc)
3393 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3394
3395 // Drop the attribute.
3396 return;
3397 }
3398
3399 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003400 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003401}
3402
John McCall8dfac0b2011-09-30 05:12:12 +00003403/// Handle cf_audited_transfer and cf_unknown_transfer.
3404static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3405 if (!isa<FunctionDecl>(D)) {
3406 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003407 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003408 return;
3409 }
3410
3411 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3412
3413 // Check whether there's a conflicting attribute already present.
3414 Attr *Existing;
3415 if (IsAudited) {
3416 Existing = D->getAttr<CFUnknownTransferAttr>();
3417 } else {
3418 Existing = D->getAttr<CFAuditedTransferAttr>();
3419 }
3420 if (Existing) {
3421 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3422 << A.getName()
3423 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3424 << A.getRange() << Existing->getRange();
3425 return;
3426 }
3427
3428 // All clear; add the attribute.
3429 if (IsAudited) {
3430 D->addAttr(
3431 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3432 } else {
3433 D->addAttr(
3434 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3435 }
3436}
3437
John McCallfe98da02011-09-29 07:17:38 +00003438static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3439 const AttributeList &Attr) {
3440 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3441 if (!RD || RD->isUnion()) {
3442 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003443 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003444 }
3445
3446 IdentifierInfo *ParmName = Attr.getParameterName();
3447
3448 // In Objective-C, verify that the type names an Objective-C type.
3449 // We don't want to check this outside of ObjC because people sometimes
3450 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003451 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003452 // Check for an existing type with this name.
3453 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3454 Sema::LookupOrdinaryName);
3455 if (S.LookupName(R, Sc)) {
3456 NamedDecl *Target = R.getFoundDecl();
3457 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3458 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3459 S.Diag(Target->getLocStart(), diag::note_declared_at);
3460 }
3461 }
3462 }
3463
3464 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3465 ParmName));
3466}
3467
Chandler Carruth1b03c872011-07-02 00:01:44 +00003468static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3469 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003470 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003471
Chandler Carruth87c44602011-07-01 23:49:12 +00003472 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003473 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003474}
3475
Chandler Carruth1b03c872011-07-02 00:01:44 +00003476static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3477 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003478 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003479 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003480 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003481 return;
3482 }
3483
Chandler Carruth87c44602011-07-01 23:49:12 +00003484 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003485 QualType type = vd->getType();
3486
3487 if (!type->isDependentType() &&
3488 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003489 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003490 << type;
3491 return;
3492 }
3493
3494 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3495
3496 // If we have no lifetime yet, check the lifetime we're presumably
3497 // going to infer.
3498 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3499 lifetime = type->getObjCARCImplicitLifetime();
3500
3501 switch (lifetime) {
3502 case Qualifiers::OCL_None:
3503 assert(type->isDependentType() &&
3504 "didn't infer lifetime for non-dependent type?");
3505 break;
3506
3507 case Qualifiers::OCL_Weak: // meaningful
3508 case Qualifiers::OCL_Strong: // meaningful
3509 break;
3510
3511 case Qualifiers::OCL_ExplicitNone:
3512 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003513 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003514 << (lifetime == Qualifiers::OCL_Autoreleasing);
3515 break;
3516 }
3517
Chandler Carruth87c44602011-07-01 23:49:12 +00003518 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003519 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003520}
3521
Charles Davisf0122fe2010-02-16 18:27:26 +00003522static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman94287722012-02-23 22:46:33 +00003523 switch (Attr.getKind()) {
3524 default:
3525 return false;
3526 case AttributeList::AT_dllimport:
3527 case AttributeList::AT_dllexport:
3528 case AttributeList::AT_uuid:
3529 case AttributeList::AT_deprecated:
3530 case AttributeList::AT_noreturn:
3531 case AttributeList::AT_nothrow:
3532 case AttributeList::AT_naked:
3533 case AttributeList::AT_noinline:
3534 return true;
3535 }
Francois Pichet11542142010-12-19 06:50:37 +00003536}
3537
3538//===----------------------------------------------------------------------===//
3539// Microsoft specific attribute handlers.
3540//===----------------------------------------------------------------------===//
3541
Chandler Carruth1b03c872011-07-02 00:01:44 +00003542static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003543 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003544 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003545 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003546 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003547
Francois Pichet11542142010-12-19 06:50:37 +00003548 Expr *Arg = Attr.getArg(0);
3549 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003550 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3552 << "uuid" << 1;
3553 return;
3554 }
3555
Chris Lattner5f9e2722011-07-23 10:55:15 +00003556 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003557
3558 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3559 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003560
Francois Pichetd3d3be92010-12-20 01:41:49 +00003561 // Validate GUID length.
3562 if (IsCurly && StrRef.size() != 38) {
3563 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3564 return;
3565 }
3566 if (!IsCurly && StrRef.size() != 36) {
3567 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3568 return;
3569 }
3570
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003571 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003572 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003573 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003574 if (IsCurly) // Skip the optional '{'
3575 ++I;
3576
3577 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003578 if (i == 8 || i == 13 || i == 18 || i == 23) {
3579 if (*I != '-') {
3580 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3581 return;
3582 }
3583 } else if (!isxdigit(*I)) {
3584 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3585 return;
3586 }
3587 I++;
3588 }
Francois Pichet11542142010-12-19 06:50:37 +00003589
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003590 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003591 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003592 } else
Francois Pichet11542142010-12-19 06:50:37 +00003593 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003594}
3595
Ted Kremenekb71368d2009-05-09 02:44:38 +00003596//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003597// Top Level Sema Entry Points
3598//===----------------------------------------------------------------------===//
3599
Chandler Carruth1b03c872011-07-02 00:01:44 +00003600static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3601 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003602 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003603 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3604 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3605 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003606 default:
3607 break;
3608 }
3609}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003610
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3612 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003613 switch (Attr.getKind()) {
Michael Hane53ac8a2012-03-07 00:12:16 +00003614 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3615 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3616 case AttributeList::AT_iboutletcollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003617 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003618 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003619 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003620 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003621 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003622 case AttributeList::AT_neon_vector_type:
3623 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003624 // Ignore these, these are type attributes, handled by
3625 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003626 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003627 case AttributeList::AT_device:
3628 case AttributeList::AT_host:
3629 case AttributeList::AT_overloadable:
3630 // Ignore, this is a non-inheritable attribute, handled
3631 // by ProcessNonInheritableDeclAttr.
3632 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003633 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3634 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003635 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003636 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003637 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003638 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3639 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3640 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003641 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003642 handleDependencyAttr (S, D, Attr); break;
3643 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3644 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3645 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3646 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3647 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003648 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003649 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003650 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003651 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3652 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3653 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3654 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003655 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003656 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003657 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003658 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3659 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3660 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3661 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3662 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003663 case AttributeList::AT_ownership_returns:
3664 case AttributeList::AT_ownership_takes:
3665 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003666 handleOwnershipAttr (S, D, Attr); break;
3667 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3668 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3669 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3670 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3671 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003672
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003673 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003674 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003675 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003676 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003677
John McCalldc7c5ad2011-07-22 08:53:00 +00003678 case AttributeList::AT_objc_returns_inner_pointer:
3679 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3680
John McCallfe98da02011-09-29 07:17:38 +00003681 case AttributeList::AT_ns_bridged:
3682 handleNSBridgedAttr(S, scope, D, Attr); break;
3683
John McCall8dfac0b2011-09-30 05:12:12 +00003684 case AttributeList::AT_cf_audited_transfer:
3685 case AttributeList::AT_cf_unknown_transfer:
3686 handleCFTransferAttr(S, D, Attr); break;
3687
Ted Kremenekb71368d2009-05-09 02:44:38 +00003688 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003689 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003690 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003691 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003692 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003693
3694 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003695 case AttributeList::AT_ns_returns_not_retained:
3696 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003697 case AttributeList::AT_ns_returns_retained:
3698 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003699 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003700
Michael Hane53ac8a2012-03-07 00:12:16 +00003701 case AttributeList::AT_reqd_work_group_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003702 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003703
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003704 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003705 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003706
Chandler Carruth1b03c872011-07-02 00:01:44 +00003707 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003708 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003709 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3710 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003711 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003712 handleArcWeakrefUnavailableAttr (S, D, Attr);
3713 break;
Patrick Beardb2f68202012-04-06 18:12:22 +00003714 case AttributeList::AT_objc_root_class:
3715 handleObjCRootClassAttr(S, D, Attr);
3716 break;
Ted Kremenek71207fc2012-01-05 22:47:47 +00003717 case AttributeList::AT_objc_requires_property_definitions:
3718 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00003719 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003720 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00003721 case AttributeList::AT_returns_twice:
3722 handleReturnsTwiceAttr(S, D, Attr);
3723 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003724 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3725 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3726 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003727 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003728 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3729 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3730 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003731 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003732 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003733 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003734 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003735 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003736 break;
John McCalld5313b02011-03-02 11:33:24 +00003737 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003738 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003739 break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003740 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003741 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3742 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3743 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3744 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3745 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3746 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3747 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3748 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003749 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003750 // Just ignore
3751 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003752 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003753 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003754 break;
John McCall04a67a62010-02-05 21:31:56 +00003755 case AttributeList::AT_stdcall:
3756 case AttributeList::AT_cdecl:
3757 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003758 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003759 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003760 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003761 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003762 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003763 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003764 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003765 break;
Francois Pichet11542142010-12-19 06:50:37 +00003766 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003767 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003768 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003769
3770 // Thread safety attributes:
3771 case AttributeList::AT_guarded_var:
3772 handleGuardedVarAttr(S, D, Attr);
3773 break;
3774 case AttributeList::AT_pt_guarded_var:
3775 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3776 break;
3777 case AttributeList::AT_scoped_lockable:
3778 handleLockableAttr(S, D, Attr, /*scoped = */true);
3779 break;
Kostya Serebryany71efba02012-01-24 19:25:38 +00003780 case AttributeList::AT_no_address_safety_analysis:
3781 handleNoAddressSafetyAttr(S, D, Attr);
3782 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003783 case AttributeList::AT_no_thread_safety_analysis:
3784 handleNoThreadSafetyAttr(S, D, Attr);
3785 break;
3786 case AttributeList::AT_lockable:
3787 handleLockableAttr(S, D, Attr);
3788 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003789 case AttributeList::AT_guarded_by:
3790 handleGuardedByAttr(S, D, Attr);
3791 break;
3792 case AttributeList::AT_pt_guarded_by:
3793 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3794 break;
3795 case AttributeList::AT_exclusive_lock_function:
3796 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3797 break;
3798 case AttributeList::AT_exclusive_locks_required:
3799 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3800 break;
3801 case AttributeList::AT_exclusive_trylock_function:
3802 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3803 break;
3804 case AttributeList::AT_lock_returned:
3805 handleLockReturnedAttr(S, D, Attr);
3806 break;
3807 case AttributeList::AT_locks_excluded:
3808 handleLocksExcludedAttr(S, D, Attr);
3809 break;
3810 case AttributeList::AT_shared_lock_function:
3811 handleLockFunAttr(S, D, Attr);
3812 break;
3813 case AttributeList::AT_shared_locks_required:
3814 handleLocksRequiredAttr(S, D, Attr);
3815 break;
3816 case AttributeList::AT_shared_trylock_function:
3817 handleTrylockFunAttr(S, D, Attr);
3818 break;
3819 case AttributeList::AT_unlock_function:
3820 handleUnlockFunAttr(S, D, Attr);
3821 break;
3822 case AttributeList::AT_acquired_before:
3823 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3824 break;
3825 case AttributeList::AT_acquired_after:
3826 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3827 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003828
Chris Lattner803d0802008-06-29 00:43:07 +00003829 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003830 // Ask target about the attribute.
3831 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3832 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003833 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3834 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003835 break;
3836 }
3837}
3838
Peter Collingbourne60700392011-01-21 02:08:45 +00003839/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3840/// the attribute applies to decls. If the attribute is a type attribute, just
3841/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3842/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003843static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3844 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003845 bool NonInheritable, bool Inheritable) {
3846 if (Attr.isInvalid())
3847 return;
3848
3849 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3850 // FIXME: Try to deal with other __declspec attributes!
3851 return;
3852
3853 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003854 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003855
3856 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003857 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003858}
3859
Chris Lattner803d0802008-06-29 00:43:07 +00003860/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3861/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003862void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003863 const AttributeList *AttrList,
3864 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003865 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003866 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003867 }
3868
3869 // GCC accepts
3870 // static int a9 __attribute__((weakref));
3871 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003872 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003873 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003874 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003875 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003876 }
3877}
3878
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003879// Annotation attributes are the only attributes allowed after an access
3880// specifier.
3881bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3882 const AttributeList *AttrList) {
3883 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3884 if (l->getKind() == AttributeList::AT_annotate) {
3885 handleAnnotateAttr(*this, ASDecl, *l);
3886 } else {
3887 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3888 return true;
3889 }
3890 }
3891
3892 return false;
3893}
3894
John McCalle82247a2011-10-01 05:17:03 +00003895/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3896/// contains any decl attributes that we should warn about.
3897static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3898 for ( ; A; A = A->getNext()) {
3899 // Only warn if the attribute is an unignored, non-type attribute.
3900 if (A->isUsedAsTypeAttr()) continue;
3901 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3902
3903 if (A->getKind() == AttributeList::UnknownAttribute) {
3904 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3905 << A->getName() << A->getRange();
3906 } else {
3907 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3908 << A->getName() << A->getRange();
3909 }
3910 }
3911}
3912
3913/// checkUnusedDeclAttributes - Given a declarator which is not being
3914/// used to build a declaration, complain about any decl attributes
3915/// which might be lying around on it.
3916void Sema::checkUnusedDeclAttributes(Declarator &D) {
3917 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3918 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3919 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3920 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3921}
3922
Ryan Flynne25ff832009-07-30 03:15:39 +00003923/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3924/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003925NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3926 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003927 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003928 NamedDecl *NewD = 0;
3929 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003930 FunctionDecl *NewFD;
3931 // FIXME: Missing call to CheckFunctionDeclaration().
3932 // FIXME: Mangling?
3933 // FIXME: Is the qualifier info correct?
3934 // FIXME: Is the DeclContext correct?
3935 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3936 Loc, Loc, DeclarationName(II),
3937 FD->getType(), FD->getTypeSourceInfo(),
3938 SC_None, SC_None,
3939 false/*isInlineSpecified*/,
3940 FD->hasPrototype(),
3941 false/*isConstexprSpecified*/);
3942 NewD = NewFD;
3943
3944 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003945 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003946
3947 // Fake up parameter variables; they are declared as if this were
3948 // a typedef.
3949 QualType FDTy = FD->getType();
3950 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3951 SmallVector<ParmVarDecl*, 16> Params;
3952 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3953 AE = FT->arg_type_end(); AI != AE; ++AI) {
3954 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3955 Param->setScopeInfo(0, Params.size());
3956 Params.push_back(Param);
3957 }
David Blaikie4278c652011-09-21 18:16:56 +00003958 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003959 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003960 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3961 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003962 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003963 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003964 VD->getStorageClass(),
3965 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003966 if (VD->getQualifier()) {
3967 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003968 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003969 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003970 }
3971 return NewD;
3972}
3973
3974/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3975/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003976void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003977 if (W.getUsed()) return; // only do this once
3978 W.setUsed(true);
3979 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3980 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003981 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003982 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3983 NDId->getName()));
3984 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003985 WeakTopLevelDecl.push_back(NewD);
3986 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3987 // to insert Decl at TU scope, sorry.
3988 DeclContext *SavedContext = CurContext;
3989 CurContext = Context.getTranslationUnitDecl();
3990 PushOnScopeChains(NewD, S);
3991 CurContext = SavedContext;
3992 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003993 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003994 }
3995}
3996
Chris Lattner0744e5f2008-06-29 00:23:49 +00003997/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3998/// it, apply them to D. This is a bit tricky because PD can have attributes
3999/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004000void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4001 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004002 // It's valid to "forward-declare" #pragma weak, in which case we
4003 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004004 if (Inheritable) {
4005 LoadExternalWeakUndeclaredIdentifiers();
4006 if (!WeakUndeclaredIdentifiers.empty()) {
4007 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4008 if (IdentifierInfo *Id = ND->getIdentifier()) {
4009 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4010 = WeakUndeclaredIdentifiers.find(Id);
4011 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4012 WeakInfo W = I->second;
4013 DeclApplyPragmaWeak(S, ND, W);
4014 WeakUndeclaredIdentifiers[Id] = W;
4015 }
John McCalld4aff0e2010-10-27 00:59:00 +00004016 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004017 }
4018 }
4019 }
4020
Chris Lattner0744e5f2008-06-29 00:23:49 +00004021 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004022 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004023 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004024
Chris Lattner0744e5f2008-06-29 00:23:49 +00004025 // Walk the declarator structure, applying decl attributes that were in a type
4026 // position to the decl itself. This handles cases like:
4027 // int *__attr__(x)** D;
4028 // when X is a decl attribute.
4029 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4030 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004031 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004032
Chris Lattner0744e5f2008-06-29 00:23:49 +00004033 // Finally, apply any attributes on the decl itself.
4034 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004035 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004036}
John McCall54abf7d2009-11-04 02:18:39 +00004037
John McCallf85e1932011-06-15 23:02:42 +00004038/// Is the given declaration allowed to use a forbidden type?
4039static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4040 // Private ivars are always okay. Unfortunately, people don't
4041 // always properly make their ivars private, even in system headers.
4042 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004043 // Function declarations in sys headers will be marked unavailable.
4044 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4045 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004046 return false;
4047
4048 // Require it to be declared in a system header.
4049 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4050}
4051
4052/// Handle a delayed forbidden-type diagnostic.
4053static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4054 Decl *decl) {
4055 if (decl && isForbiddenTypeAllowed(S, decl)) {
4056 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4057 "this system declaration uses an unsupported type"));
4058 return;
4059 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004060 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004061 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4062 // FIXME. we may want to supress diagnostics for all
4063 // kind of forbidden type messages on unavailable functions.
4064 if (FD->hasAttr<UnavailableAttr>() &&
4065 diag.getForbiddenTypeDiagnostic() ==
4066 diag::err_arc_array_param_no_ownership) {
4067 diag.Triggered = true;
4068 return;
4069 }
4070 }
John McCallf85e1932011-06-15 23:02:42 +00004071
4072 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4073 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4074 diag.Triggered = true;
4075}
4076
John McCalleee1d542011-02-14 07:13:47 +00004077// This duplicates a vector push_back but hides the need to know the
4078// size of the type.
4079void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4080 assert(StackSize <= StackCapacity);
4081
4082 // Grow the stack if necessary.
4083 if (StackSize == StackCapacity) {
4084 unsigned newCapacity = 2 * StackCapacity + 2;
4085 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4086 const char *oldBuffer = (const char*) Stack;
4087
4088 if (StackCapacity)
4089 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4090
4091 delete[] oldBuffer;
4092 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4093 StackCapacity = newCapacity;
4094 }
4095
4096 assert(StackSize < StackCapacity);
4097 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00004098}
4099
John McCalleee1d542011-02-14 07:13:47 +00004100void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4101 Decl *decl) {
4102 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00004103
John McCalleee1d542011-02-14 07:13:47 +00004104 // Check the invariants.
4105 assert(DD.StackSize >= state.SavedStackSize);
4106 assert(state.SavedStackSize >= DD.ActiveStackBase);
4107 assert(DD.ParsingDepth > 0);
4108
4109 // Drop the parsing depth.
4110 DD.ParsingDepth--;
4111
4112 // If there are no active diagnostics, we're done.
4113 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00004114 return;
4115
John McCall2f514482010-01-27 03:50:35 +00004116 // We only want to actually emit delayed diagnostics when we
4117 // successfully parsed a decl.
John McCalle8c904f2012-01-26 20:04:03 +00004118 if (decl) {
John McCalleee1d542011-02-14 07:13:47 +00004119 // We emit all the active diagnostics, not just those starting
4120 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00004121 // decl spec and another for each declarator; in a decl group like:
4122 // deprecated_typedef foo, *bar, baz();
4123 // only the declarator pops will be passed decls. This is correct;
4124 // we really do need to consider delayed diagnostics from the decl spec
4125 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00004126 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4127 DelayedDiagnostic &diag = DD.Stack[i];
4128 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004129 continue;
4130
John McCalleee1d542011-02-14 07:13:47 +00004131 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004132 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004133 // Don't bother giving deprecation diagnostics if the decl is invalid.
4134 if (!decl->isInvalidDecl())
4135 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004136 break;
4137
4138 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00004139 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004140 break;
John McCallf85e1932011-06-15 23:02:42 +00004141
4142 case DelayedDiagnostic::ForbiddenType:
4143 handleDelayedForbiddenType(S, diag, decl);
4144 break;
John McCall2f514482010-01-27 03:50:35 +00004145 }
4146 }
4147 }
4148
John McCall58e6f342010-03-16 05:22:47 +00004149 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00004150 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00004151 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00004152
John McCalleee1d542011-02-14 07:13:47 +00004153 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00004154}
4155
4156static bool isDeclDeprecated(Decl *D) {
4157 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004158 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004159 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004160 // A category implicitly has the availability of the interface.
4161 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4162 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004163 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4164 return false;
4165}
4166
John McCall9c3087b2010-08-26 02:13:20 +00004167void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004168 Decl *Ctx) {
4169 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004170 return;
4171
John McCall2f514482010-01-27 03:50:35 +00004172 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004173 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004174 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004175 << DD.getDeprecationDecl()->getDeclName()
4176 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004177 else if (DD.getUnknownObjCClass()) {
4178 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4179 << DD.getDeprecationDecl()->getDeclName();
4180 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4181 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004182 else
4183 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004184 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004185}
4186
Chris Lattner5f9e2722011-07-23 10:55:15 +00004187void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004188 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004189 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004190 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004191 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004192 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4193 UnknownObjCClass,
4194 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004195 return;
4196 }
4197
4198 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004199 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004200 return;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004201 if (!Message.empty()) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004202 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4203 << Message;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004204 Diag(D->getLocation(),
4205 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4206 : diag::note_previous_decl) << D->getDeclName();
4207 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004208 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00004209 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004210 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004211 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004212 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004213 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4214 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004215 }
John McCall54abf7d2009-11-04 02:18:39 +00004216}