blob: 13bf0aa4875cf6dc624b4ff856495f7e0d026310 [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 Hutchinsf26efd72012-05-02 17:38:37 +0000684 SmallVector<Expr*, 1> Args;
685 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
686 unsigned Size = Args.size();
687 if (Size == 0)
688 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000689
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000690 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
691 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000692}
693
694static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000695 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000696 assert(!Attr.isInvalid());
697
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000699 return;
700
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000701 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000702 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
703 << Attr.getName() << ExpectedFunctionOrMethod;
704 return;
705 }
706
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000707 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000708 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000709 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000710 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000711 if (Size == 0)
712 return;
713 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000714
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000715 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000716 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000717}
718
719
Chandler Carruth1b03c872011-07-02 00:01:44 +0000720static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
721 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000722 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000723 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000724 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000725 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000726 }
Mike Stumpbf916502009-07-24 19:02:52 +0000727
Chris Lattner6b6b5372008-06-26 18:38:35 +0000728 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000729
730 Expr *sizeExpr;
731
732 // Special case where the argument is a template id.
733 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000734 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000735 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000736 UnqualifiedId id;
737 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000738
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000739 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
740 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000741 if (Size.isInvalid())
742 return;
743
744 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000745 } else {
746 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000747 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000748 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000749
Peter Collingbourne7a730022010-11-23 20:45:58 +0000750 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000751 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000752
753 // Instantiate/Install the vector type, and let Sema build the type for us.
754 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000755 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000756 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000757 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000758 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000759
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000760 // Remember this typedef decl, we will need it later for diagnostics.
761 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000762 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000763}
764
Chandler Carruth1b03c872011-07-02 00:01:44 +0000765static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000766 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000767 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000768 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000769
Chandler Carruth87c44602011-07-01 23:49:12 +0000770 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000771 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000772 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000773 // If the alignment is less than or equal to 8 bits, the packed attribute
774 // has no effect.
775 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000776 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000777 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000778 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000779 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000780 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000781 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000782 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000783}
784
Chandler Carruth1b03c872011-07-02 00:01:44 +0000785static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000786 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000787 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000788 else
789 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
790}
791
Chandler Carruth1b03c872011-07-02 00:01:44 +0000792static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000793 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000794 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000795 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000796
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000797 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000798 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000799 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000800 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000801 return;
802 }
803
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000804 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000805}
806
Ted Kremenek2f041d02011-09-29 07:02:25 +0000807static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
808 // The IBOutlet/IBOutletCollection attributes only apply to instance
809 // variables or properties of Objective-C classes. The outlet must also
810 // have an object reference type.
811 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
812 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000813 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000814 << Attr.getName() << VD->getType() << 0;
815 return false;
816 }
817 }
818 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
819 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000820 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000821 << Attr.getName() << PD->getType() << 1;
822 return false;
823 }
824 }
825 else {
826 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
827 return false;
828 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000829
Ted Kremenek2f041d02011-09-29 07:02:25 +0000830 return true;
831}
832
Chandler Carruth1b03c872011-07-02 00:01:44 +0000833static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000834 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000835 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000836 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000837
838 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000839 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000840
Ted Kremenek2f041d02011-09-29 07:02:25 +0000841 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000842}
843
Chandler Carruth1b03c872011-07-02 00:01:44 +0000844static void handleIBOutletCollection(Sema &S, Decl *D,
845 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000846
847 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000848 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
850 return;
851 }
852
Ted Kremenek2f041d02011-09-29 07:02:25 +0000853 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000854 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000855
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000856 IdentifierInfo *II = Attr.getParameterName();
857 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000858 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000859
John McCallb3d87482010-08-24 05:47:05 +0000860 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000861 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000862 if (!TypeRep) {
863 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
864 return;
865 }
John McCallb3d87482010-08-24 05:47:05 +0000866 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000867 // Diagnose use of non-object type in iboutletcollection attribute.
868 // FIXME. Gnu attribute extension ignores use of builtin types in
869 // attributes. So, __attribute__((iboutletcollection(char))) will be
870 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000871 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000872 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
873 return;
874 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000875 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
876 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000877}
878
Chandler Carruthd309c812011-07-01 23:49:16 +0000879static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000880 if (const RecordType *UT = T->getAsUnionType())
881 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
882 RecordDecl *UD = UT->getDecl();
883 for (RecordDecl::field_iterator it = UD->field_begin(),
884 itend = UD->field_end(); it != itend; ++it) {
885 QualType QT = it->getType();
886 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
887 T = QT;
888 return;
889 }
890 }
891 }
892}
893
Chandler Carruth1b03c872011-07-02 00:01:44 +0000894static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000895 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
896 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000897 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000898 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000899 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000900 return;
901 }
Mike Stumpbf916502009-07-24 19:02:52 +0000902
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000903 // In C++ the implicit 'this' function parameter also counts, and they are
904 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000905 bool HasImplicitThisParam = isInstanceMethod(D);
906 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000907
908 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000909 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000910
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000911 for (AttributeList::arg_iterator I=Attr.arg_begin(),
912 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000913
914
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000915 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000916 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000917 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000918 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
919 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000920 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
921 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000922 return;
923 }
Mike Stumpbf916502009-07-24 19:02:52 +0000924
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000925 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000926
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000927 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000928 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000929 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000930 return;
931 }
Mike Stumpbf916502009-07-24 19:02:52 +0000932
Ted Kremenek465172f2008-07-21 22:09:15 +0000933 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000934 if (HasImplicitThisParam) {
935 if (x == 0) {
936 S.Diag(Attr.getLoc(),
937 diag::err_attribute_invalid_implicit_this_argument)
938 << "nonnull" << Ex->getSourceRange();
939 return;
940 }
941 --x;
942 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000943
944 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000945 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000946 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000947
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000948 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000949 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000950 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000951 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000952 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000953 }
Mike Stumpbf916502009-07-24 19:02:52 +0000954
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000955 NonNullArgs.push_back(x);
956 }
Mike Stumpbf916502009-07-24 19:02:52 +0000957
958 // If no arguments were specified to __attribute__((nonnull)) then all pointer
959 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000960 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000961 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
962 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000963 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000964 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000965 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000966 }
Mike Stumpbf916502009-07-24 19:02:52 +0000967
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000968 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000969 if (NonNullArgs.empty()) {
970 // Warn the trivial case only if attribute is not coming from a
971 // macro instantiation.
972 if (Attr.getLoc().isFileID())
973 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000974 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000975 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000976 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000977
978 unsigned* start = &NonNullArgs[0];
979 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000980 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000981 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000982 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000983}
984
Chandler Carruth1b03c872011-07-02 00:01:44 +0000985static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000986 // This attribute must be applied to a function declaration.
987 // The first argument to the attribute must be a string,
988 // the name of the resource, for example "malloc".
989 // The following arguments must be argument indexes, the arguments must be
990 // of integer type for Returns, otherwise of pointer type.
991 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000992 // after being held. free() should be __attribute((ownership_takes)), whereas
993 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000994
995 if (!AL.getParameterName()) {
996 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
997 << AL.getName()->getName() << 1;
998 return;
999 }
1000 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001001 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001002 switch (AL.getKind()) {
1003 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001004 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001005 if (AL.getNumArgs() < 1) {
1006 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1007 return;
1008 }
Jordy Rose2a479922010-08-12 08:54:03 +00001009 break;
1010 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001011 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001012 if (AL.getNumArgs() < 1) {
1013 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1014 return;
1015 }
Jordy Rose2a479922010-08-12 08:54:03 +00001016 break;
1017 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001018 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001019 if (AL.getNumArgs() > 1) {
1020 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1021 << AL.getNumArgs() + 1;
1022 return;
1023 }
Jordy Rose2a479922010-08-12 08:54:03 +00001024 break;
1025 default:
1026 // This should never happen given how we are called.
1027 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001028 }
1029
Chandler Carruth87c44602011-07-01 23:49:12 +00001030 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001031 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1032 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001033 return;
1034 }
1035
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001036 // In C++ the implicit 'this' function parameter also counts, and they are
1037 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001038 bool HasImplicitThisParam = isInstanceMethod(D);
1039 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001040
Chris Lattner5f9e2722011-07-23 10:55:15 +00001041 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001042
1043 // Normalize the argument, __foo__ becomes foo.
1044 if (Module.startswith("__") && Module.endswith("__"))
1045 Module = Module.substr(2, Module.size() - 4);
1046
Chris Lattner5f9e2722011-07-23 10:55:15 +00001047 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001048
Jordy Rose2a479922010-08-12 08:54:03 +00001049 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1050 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001051
Peter Collingbourne7a730022010-11-23 20:45:58 +00001052 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001053 llvm::APSInt ArgNum(32);
1054 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1055 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1056 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1057 << AL.getName()->getName() << IdxExpr->getSourceRange();
1058 continue;
1059 }
1060
1061 unsigned x = (unsigned) ArgNum.getZExtValue();
1062
1063 if (x > NumArgs || x < 1) {
1064 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1065 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1066 continue;
1067 }
1068 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001069 if (HasImplicitThisParam) {
1070 if (x == 0) {
1071 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1072 << "ownership" << IdxExpr->getSourceRange();
1073 return;
1074 }
1075 --x;
1076 }
1077
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001078 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001079 case OwnershipAttr::Takes:
1080 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001081 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001082 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001083 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1084 // FIXME: Should also highlight argument in decl.
1085 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001086 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001087 << "pointer"
1088 << IdxExpr->getSourceRange();
1089 continue;
1090 }
1091 break;
1092 }
Sean Huntcf807c42010-08-18 23:23:40 +00001093 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001094 if (AL.getNumArgs() > 1) {
1095 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001096 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001097 llvm::APSInt ArgNum(32);
1098 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1099 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1100 S.Diag(AL.getLoc(), diag::err_ownership_type)
1101 << "ownership_returns" << "integer"
1102 << IdxExpr->getSourceRange();
1103 return;
1104 }
1105 }
1106 break;
1107 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001108 } // switch
1109
1110 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001111 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001112 i = D->specific_attr_begin<OwnershipAttr>(),
1113 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001114 i != e; ++i) {
1115 if ((*i)->getOwnKind() != K) {
1116 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1117 I!=E; ++I) {
1118 if (x == *I) {
1119 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1120 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001121 }
1122 }
1123 }
1124 }
1125 OwnershipArgs.push_back(x);
1126 }
1127
1128 unsigned* start = OwnershipArgs.data();
1129 unsigned size = OwnershipArgs.size();
1130 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001131
1132 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1133 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1134 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001135 }
Sean Huntcf807c42010-08-18 23:23:40 +00001136
Chandler Carruth87c44602011-07-01 23:49:12 +00001137 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001138 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001139}
1140
John McCall332bb2a2011-02-08 22:35:49 +00001141/// Whether this declaration has internal linkage for the purposes of
1142/// things that want to complain about things not have internal linkage.
1143static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1144 switch (D->getLinkage()) {
1145 case NoLinkage:
1146 case InternalLinkage:
1147 return true;
1148
1149 // Template instantiations that go from external to unique-external
1150 // shouldn't get diagnosed.
1151 case UniqueExternalLinkage:
1152 return true;
1153
1154 case ExternalLinkage:
1155 return false;
1156 }
1157 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001158}
1159
Chandler Carruth1b03c872011-07-02 00:01:44 +00001160static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001161 // Check the attribute arguments.
1162 if (Attr.getNumArgs() > 1) {
1163 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1164 return;
1165 }
1166
Chandler Carruth87c44602011-07-01 23:49:12 +00001167 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001169 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001170 return;
1171 }
1172
Chandler Carruth87c44602011-07-01 23:49:12 +00001173 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001174
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001175 // gcc rejects
1176 // class c {
1177 // static int a __attribute__((weakref ("v2")));
1178 // static int b() __attribute__((weakref ("f3")));
1179 // };
1180 // and ignores the attributes of
1181 // void f(void) {
1182 // static int a __attribute__((weakref ("v2")));
1183 // }
1184 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001185 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001186 if (!Ctx->isFileContext()) {
1187 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001188 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001189 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001190 }
1191
1192 // The GCC manual says
1193 //
1194 // At present, a declaration to which `weakref' is attached can only
1195 // be `static'.
1196 //
1197 // It also says
1198 //
1199 // Without a TARGET,
1200 // given as an argument to `weakref' or to `alias', `weakref' is
1201 // equivalent to `weak'.
1202 //
1203 // gcc 4.4.1 will accept
1204 // int a7 __attribute__((weakref));
1205 // as
1206 // int a7 __attribute__((weak));
1207 // This looks like a bug in gcc. We reject that for now. We should revisit
1208 // it if this behaviour is actually used.
1209
John McCall332bb2a2011-02-08 22:35:49 +00001210 if (!hasEffectivelyInternalLinkage(nd)) {
1211 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001212 return;
1213 }
1214
1215 // GCC rejects
1216 // static ((alias ("y"), weakref)).
1217 // Should we? How to check that weakref is before or after alias?
1218
1219 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001220 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001221 Arg = Arg->IgnoreParenCasts();
1222 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1223
Douglas Gregor5cee1192011-07-27 05:40:30 +00001224 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1226 << "weakref" << 1;
1227 return;
1228 }
1229 // GCC will accept anything as the argument of weakref. Should we
1230 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001231 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001232 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001233 }
1234
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001235 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001236}
1237
Chandler Carruth1b03c872011-07-02 00:01:44 +00001238static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001239 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001240 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001242 return;
1243 }
Mike Stumpbf916502009-07-24 19:02:52 +00001244
Peter Collingbourne7a730022010-11-23 20:45:58 +00001245 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001246 Arg = Arg->IgnoreParenCasts();
1247 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001248
Douglas Gregor5cee1192011-07-27 05:40:30 +00001249 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001251 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 return;
1253 }
Mike Stumpbf916502009-07-24 19:02:52 +00001254
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001255 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001256 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1257 return;
1258 }
1259
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001261
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001262 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001263 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001264}
1265
Chandler Carruth1b03c872011-07-02 00:01:44 +00001266static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001267 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001268 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001269 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001270
Chandler Carruth87c44602011-07-01 23:49:12 +00001271 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001272 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001273 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001274 return;
1275 }
1276
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001277 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001278}
1279
Chandler Carruth1b03c872011-07-02 00:01:44 +00001280static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1281 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001282 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001283 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1285 return;
1286 }
1287
Chandler Carruth87c44602011-07-01 23:49:12 +00001288 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001289 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001290 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001291 return;
1292 }
Mike Stumpbf916502009-07-24 19:02:52 +00001293
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001294 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001295}
1296
Chandler Carruth1b03c872011-07-02 00:01:44 +00001297static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001298 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001299 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001300 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1301 return;
1302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chandler Carruth87c44602011-07-01 23:49:12 +00001304 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001305 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001306 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001307 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001308 return;
1309 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001310 }
1311
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001312 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001313}
1314
Chandler Carruth1b03c872011-07-02 00:01:44 +00001315static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001316 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001317 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001318 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001319
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001320 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001321}
1322
Chandler Carruth1b03c872011-07-02 00:01:44 +00001323static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001324 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001325 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001326 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001327 else
1328 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001329 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001330}
1331
Chandler Carruth1b03c872011-07-02 00:01:44 +00001332static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001333 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001334 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001335 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001336 else
1337 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001338 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001339}
1340
Chandler Carruth1b03c872011-07-02 00:01:44 +00001341static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001342 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001343
1344 if (S.CheckNoReturnAttr(attr)) return;
1345
Chandler Carruth87c44602011-07-01 23:49:12 +00001346 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001347 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001348 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001349 return;
1350 }
1351
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001352 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001353}
1354
1355bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001356 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001357 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1358 attr.setInvalid();
1359 return true;
1360 }
1361
1362 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001363}
1364
Chandler Carruth1b03c872011-07-02 00:01:44 +00001365static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1366 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001367
1368 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1369 // because 'analyzer_noreturn' does not impact the type.
1370
Chandler Carruth1731e202011-07-11 23:30:35 +00001371 if(!checkAttributeNumArgs(S, Attr, 0))
1372 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001373
Chandler Carruth87c44602011-07-01 23:49:12 +00001374 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1375 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001376 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1377 && !VD->getType()->isFunctionPointerType())) {
1378 S.Diag(Attr.getLoc(),
1379 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1380 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001381 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001382 return;
1383 }
1384 }
1385
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001386 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001387}
1388
John Thompson35cc9622010-08-09 21:53:52 +00001389// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001390static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001391/*
1392 Returning a Vector Class in Registers
1393
Eric Christopherf48f3672010-12-01 22:13:54 +00001394 According to the PPU ABI specifications, a class with a single member of
1395 vector type is returned in memory when used as the return value of a function.
1396 This results in inefficient code when implementing vector classes. To return
1397 the value in a single vector register, add the vecreturn attribute to the
1398 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001399
1400 Example:
1401
1402 struct Vector
1403 {
1404 __vector float xyzw;
1405 } __attribute__((vecreturn));
1406
1407 Vector Add(Vector lhs, Vector rhs)
1408 {
1409 Vector result;
1410 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1411 return result; // This will be returned in a register
1412 }
1413*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001414 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001416 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001417 return;
1418 }
1419
Chandler Carruth87c44602011-07-01 23:49:12 +00001420 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001421 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1422 return;
1423 }
1424
Chandler Carruth87c44602011-07-01 23:49:12 +00001425 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001426 int count = 0;
1427
1428 if (!isa<CXXRecordDecl>(record)) {
1429 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1430 return;
1431 }
1432
1433 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1434 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1435 return;
1436 }
1437
Eric Christopherf48f3672010-12-01 22:13:54 +00001438 for (RecordDecl::field_iterator iter = record->field_begin();
1439 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001440 if ((count == 1) || !iter->getType()->isVectorType()) {
1441 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1442 return;
1443 }
1444 count++;
1445 }
1446
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001447 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001448}
1449
Chandler Carruth1b03c872011-07-02 00:01:44 +00001450static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001451 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001452 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001453 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001454 return;
1455 }
1456 // FIXME: Actually store the attribute on the declaration
1457}
1458
Chandler Carruth1b03c872011-07-02 00:01:44 +00001459static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001460 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001461 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001462 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001463 return;
1464 }
Mike Stumpbf916502009-07-24 19:02:52 +00001465
Chandler Carruth87c44602011-07-01 23:49:12 +00001466 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1467 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001468 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001469 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001470 return;
1471 }
Mike Stumpbf916502009-07-24 19:02:52 +00001472
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001473 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001474}
1475
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001476static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1477 const AttributeList &Attr) {
1478 // check the attribute arguments.
1479 if (Attr.hasParameterOrArguments()) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1481 return;
1482 }
1483
1484 if (!isa<FunctionDecl>(D)) {
1485 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1486 << Attr.getName() << ExpectedFunction;
1487 return;
1488 }
1489
1490 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1491}
1492
Chandler Carruth1b03c872011-07-02 00:01:44 +00001493static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001494 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001495 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1497 return;
1498 }
Mike Stumpbf916502009-07-24 19:02:52 +00001499
Chandler Carruth87c44602011-07-01 23:49:12 +00001500 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001501 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001502 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1503 return;
1504 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001505 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001506 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001507 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001508 return;
1509 }
Mike Stumpbf916502009-07-24 19:02:52 +00001510
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001511 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001512}
1513
Chandler Carruth1b03c872011-07-02 00:01:44 +00001514static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001515 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001516 if (Attr.getNumArgs() > 1) {
1517 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001518 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001519 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001520
1521 int priority = 65535; // FIXME: Do not hardcode such constants.
1522 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001523 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001524 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001525 if (E->isTypeDependent() || E->isValueDependent() ||
1526 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001527 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001528 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001529 return;
1530 }
1531 priority = Idx.getZExtValue();
1532 }
Mike Stumpbf916502009-07-24 19:02:52 +00001533
Chandler Carruth87c44602011-07-01 23:49:12 +00001534 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001535 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001536 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001537 return;
1538 }
1539
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001540 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001541 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001542}
1543
Chandler Carruth1b03c872011-07-02 00:01:44 +00001544static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001545 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001546 if (Attr.getNumArgs() > 1) {
1547 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001548 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001549 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001550
1551 int priority = 65535; // FIXME: Do not hardcode such constants.
1552 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001553 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001554 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001555 if (E->isTypeDependent() || E->isValueDependent() ||
1556 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001557 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001558 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001559 return;
1560 }
1561 priority = Idx.getZExtValue();
1562 }
Mike Stumpbf916502009-07-24 19:02:52 +00001563
Chandler Carruth87c44602011-07-01 23:49:12 +00001564 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001565 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001566 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001567 return;
1568 }
1569
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001570 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001571 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001572}
1573
Chandler Carruth1b03c872011-07-02 00:01:44 +00001574static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001575 unsigned NumArgs = Attr.getNumArgs();
1576 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001577 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001578 return;
1579 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001580
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001581 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001582 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001583 if (NumArgs == 1) {
1584 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001585 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001586 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1587 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001588 return;
1589 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001590 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001591 }
Mike Stumpbf916502009-07-24 19:02:52 +00001592
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001593 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001594}
1595
Chandler Carruth1b03c872011-07-02 00:01:44 +00001596static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001597 unsigned NumArgs = Attr.getNumArgs();
1598 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001599 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001600 return;
1601 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001602
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001603 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001604 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001605 if (NumArgs == 1) {
1606 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001607 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001608 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001609 diag::err_attribute_not_string) << "unavailable";
1610 return;
1611 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001612 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001613 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001614 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001615}
1616
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001617static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1618 const AttributeList &Attr) {
1619 unsigned NumArgs = Attr.getNumArgs();
1620 if (NumArgs > 0) {
1621 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1622 return;
1623 }
1624
1625 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001626 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001627}
1628
Patrick Beardb2f68202012-04-06 18:12:22 +00001629static void handleObjCRootClassAttr(Sema &S, Decl *D,
1630 const AttributeList &Attr) {
1631 if (!isa<ObjCInterfaceDecl>(D)) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1633 return;
1634 }
1635
1636 unsigned NumArgs = Attr.getNumArgs();
1637 if (NumArgs > 0) {
1638 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1639 return;
1640 }
1641
1642 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1643}
1644
Ted Kremenek71207fc2012-01-05 22:47:47 +00001645static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001646 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001647 if (!isa<ObjCInterfaceDecl>(D)) {
1648 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1649 return;
1650 }
1651
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001652 unsigned NumArgs = Attr.getNumArgs();
1653 if (NumArgs > 0) {
1654 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1655 return;
1656 }
1657
Ted Kremenek71207fc2012-01-05 22:47:47 +00001658 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001659 Attr.getRange(), S.Context));
1660}
1661
Chandler Carruth1b03c872011-07-02 00:01:44 +00001662static void handleAvailabilityAttr(Sema &S, Decl *D,
1663 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001664 IdentifierInfo *Platform = Attr.getParameterName();
1665 SourceLocation PlatformLoc = Attr.getParameterLoc();
1666
Chris Lattner5f9e2722011-07-23 10:55:15 +00001667 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001668 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1669 if (PlatformName.empty()) {
1670 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1671 << Platform;
1672
1673 PlatformName = Platform->getName();
1674 }
1675
1676 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1677 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1678 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001679 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001680
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001681 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001682 // of these steps are needed).
1683 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001684 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001685 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1686 << 1 << PlatformName << Deprecated.Version.getAsString()
1687 << 0 << Introduced.Version.getAsString();
1688 return;
1689 }
1690
1691 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001692 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001693 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1694 << 2 << PlatformName << Obsoleted.Version.getAsString()
1695 << 0 << Introduced.Version.getAsString();
1696 return;
1697 }
1698
1699 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001700 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001701 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1702 << 2 << PlatformName << Obsoleted.Version.getAsString()
1703 << 1 << Deprecated.Version.getAsString();
1704 return;
1705 }
1706
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001707 StringRef Str;
1708 const StringLiteral *SE =
1709 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1710 if (SE)
1711 Str = SE->getString();
1712
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001713 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001714 Platform,
1715 Introduced.Version,
1716 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001717 Obsoleted.Version,
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001718 IsUnavailable,
1719 Str));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001720}
1721
Chandler Carruth1b03c872011-07-02 00:01:44 +00001722static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001723 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001724 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001725 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001726
Peter Collingbourne7a730022010-11-23 20:45:58 +00001727 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001728 Arg = Arg->IgnoreParenCasts();
1729 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001730
Douglas Gregor5cee1192011-07-27 05:40:30 +00001731 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001732 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001733 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001734 return;
1735 }
Mike Stumpbf916502009-07-24 19:02:52 +00001736
Chris Lattner5f9e2722011-07-23 10:55:15 +00001737 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001738 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001739
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001740 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001741 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001742 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001743 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001744 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001745 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00001746 else if (TypeStr == "protected") {
1747 // Complain about attempts to use protected visibility on targets
1748 // (like Darwin) that don't support it.
1749 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1750 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1751 type = VisibilityAttr::Default;
1752 } else {
1753 type = VisibilityAttr::Protected;
1754 }
1755 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00001756 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001757 return;
1758 }
Mike Stumpbf916502009-07-24 19:02:52 +00001759
Rafael Espindola4e31b4d2012-05-01 20:58:29 +00001760 Decl *PrevDecl;
1761 if (isa<FunctionDecl>(D))
1762 PrevDecl = D->getMostRecentDecl()->getPreviousDecl();
1763 else
1764 PrevDecl = D->getCanonicalDecl();
1765
1766 VisibilityAttr *PrevAttr = PrevDecl ? PrevDecl->getAttr<VisibilityAttr>() : 0;
Rafael Espindola45a0b262012-04-26 01:26:03 +00001767 if (PrevAttr) {
1768 VisibilityAttr::VisibilityType PrevVisibility = PrevAttr->getVisibility();
1769 if (PrevVisibility != type) {
1770 S.Diag(Attr.getLoc(), diag::err_mismatched_visibilit);
1771 S.Diag(PrevAttr->getLocation(), diag::note_previous_attribute);
1772 return;
1773 }
1774 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001775 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001776}
1777
Chandler Carruth1b03c872011-07-02 00:01:44 +00001778static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1779 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001780 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1781 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001783 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001784 return;
1785 }
1786
Chandler Carruth87c44602011-07-01 23:49:12 +00001787 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1788 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1789 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001790 << "objc_method_family" << 1;
1791 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001792 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001793 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001794 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001795 return;
1796 }
1797
Chris Lattner5f9e2722011-07-23 10:55:15 +00001798 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001799 ObjCMethodFamilyAttr::FamilyKind family;
1800 if (param == "none")
1801 family = ObjCMethodFamilyAttr::OMF_None;
1802 else if (param == "alloc")
1803 family = ObjCMethodFamilyAttr::OMF_alloc;
1804 else if (param == "copy")
1805 family = ObjCMethodFamilyAttr::OMF_copy;
1806 else if (param == "init")
1807 family = ObjCMethodFamilyAttr::OMF_init;
1808 else if (param == "mutableCopy")
1809 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1810 else if (param == "new")
1811 family = ObjCMethodFamilyAttr::OMF_new;
1812 else {
1813 // Just warn and ignore it. This is future-proof against new
1814 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001815 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001816 return;
1817 }
1818
John McCallf85e1932011-06-15 23:02:42 +00001819 if (family == ObjCMethodFamilyAttr::OMF_init &&
1820 !method->getResultType()->isObjCObjectPointerType()) {
1821 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1822 << method->getResultType();
1823 // Ignore the attribute.
1824 return;
1825 }
1826
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001827 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001828 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001829}
1830
Chandler Carruth1b03c872011-07-02 00:01:44 +00001831static void handleObjCExceptionAttr(Sema &S, Decl *D,
1832 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001833 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001834 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001835
Chris Lattner0db29ec2009-02-14 08:09:34 +00001836 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1837 if (OCI == 0) {
1838 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1839 return;
1840 }
Mike Stumpbf916502009-07-24 19:02:52 +00001841
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001842 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001843}
1844
Chandler Carruth1b03c872011-07-02 00:01:44 +00001845static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001846 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001848 return;
1849 }
Richard Smith162e1c12011-04-15 14:24:37 +00001850 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001851 QualType T = TD->getUnderlyingType();
1852 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001853 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001854 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1855 return;
1856 }
1857 }
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001858 else if (!isa<ObjCPropertyDecl>(D)) {
1859 // It is okay to include this attribute on properties, e.g.:
1860 //
1861 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1862 //
1863 // In this case it follows tradition and suppresses an error in the above
1864 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00001865 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001866 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001867 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001868}
1869
Mike Stumpbf916502009-07-24 19:02:52 +00001870static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001871handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001872 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001874 return;
1875 }
1876
1877 if (!isa<FunctionDecl>(D)) {
1878 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1879 return;
1880 }
1881
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001882 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001883}
1884
Chandler Carruth1b03c872011-07-02 00:01:44 +00001885static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001886 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001887 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001888 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001889 return;
1890 }
Mike Stumpbf916502009-07-24 19:02:52 +00001891
Steve Naroff9eae5762008-09-18 16:44:58 +00001892 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001893 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001894 return;
1895 }
Mike Stumpbf916502009-07-24 19:02:52 +00001896
Sean Huntcf807c42010-08-18 23:23:40 +00001897 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001898 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001899 type = BlocksAttr::ByRef;
1900 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001901 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001902 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001903 return;
1904 }
Mike Stumpbf916502009-07-24 19:02:52 +00001905
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001906 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001907}
1908
Chandler Carruth1b03c872011-07-02 00:01:44 +00001909static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001910 // check the attribute arguments.
1911 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001912 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001913 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001914 }
1915
John McCall3323fad2011-09-09 07:56:05 +00001916 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001917 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001918 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001919 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001920 if (E->isTypeDependent() || E->isValueDependent() ||
1921 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001923 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001924 return;
1925 }
Mike Stumpbf916502009-07-24 19:02:52 +00001926
John McCall3323fad2011-09-09 07:56:05 +00001927 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001928 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1929 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001930 return;
1931 }
John McCall3323fad2011-09-09 07:56:05 +00001932
1933 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001934 }
1935
John McCall3323fad2011-09-09 07:56:05 +00001936 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001937 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001938 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001939 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001940 if (E->isTypeDependent() || E->isValueDependent() ||
1941 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001943 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001944 return;
1945 }
1946 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001947
John McCall3323fad2011-09-09 07:56:05 +00001948 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001949 // FIXME: This error message could be improved, it would be nice
1950 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001951 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1952 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001953 return;
1954 }
1955 }
1956
Chandler Carruth87c44602011-07-01 23:49:12 +00001957 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001958 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001959 if (isa<FunctionNoProtoType>(FT)) {
1960 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1961 return;
1962 }
Mike Stumpbf916502009-07-24 19:02:52 +00001963
Chris Lattner897cd902009-03-17 23:03:47 +00001964 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001966 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001967 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001968 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001969 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001970 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001971 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001972 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00001973 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1974 if (!BD->isVariadic()) {
1975 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1976 return;
1977 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001978 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001979 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001980 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001981 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001982 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001983 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001984 int m = Ty->isFunctionPointerType() ? 0 : 1;
1985 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001986 return;
1987 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001988 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001989 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001990 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001991 return;
1992 }
Anders Carlsson77091822008-10-05 18:05:59 +00001993 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001994 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001995 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001996 return;
1997 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001998 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001999 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002000}
2001
Chandler Carruth1b03c872011-07-02 00:01:44 +00002002static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002003 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002004 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002005 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002006
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002007 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002008 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002009 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002010 return;
2011 }
Mike Stumpbf916502009-07-24 19:02:52 +00002012
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002013 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2014 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2015 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002016 return;
2017 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002018 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2019 if (MD->getResultType()->isVoidType()) {
2020 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2021 << Attr.getName() << 1;
2022 return;
2023 }
2024
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002025 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002026}
2027
Chandler Carruth1b03c872011-07-02 00:01:44 +00002028static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002029 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002030 if (Attr.hasParameterOrArguments()) {
2031 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002032 return;
2033 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002034
Chandler Carruth87c44602011-07-01 23:49:12 +00002035 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002036 if (isa<CXXRecordDecl>(D)) {
2037 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2038 return;
2039 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002040 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2041 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002042 return;
2043 }
2044
Chandler Carruth87c44602011-07-01 23:49:12 +00002045 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002046
2047 // 'weak' only applies to declarations with external linkage.
2048 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002049 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002050 return;
2051 }
Mike Stumpbf916502009-07-24 19:02:52 +00002052
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002053 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002054}
2055
Chandler Carruth1b03c872011-07-02 00:01:44 +00002056static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002057 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002058 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002059 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002060
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002061
2062 // weak_import only applies to variable & function declarations.
2063 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002064 if (!D->canBeWeakImported(isDef)) {
2065 if (isDef)
2066 S.Diag(Attr.getLoc(),
2067 diag::warn_attribute_weak_import_invalid_on_definition)
2068 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002069 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002070 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002071 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002072 // Nothing to warn about here.
2073 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002074 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002075 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002076
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002077 return;
2078 }
2079
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002080 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002081}
2082
Chandler Carruth1b03c872011-07-02 00:01:44 +00002083static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2084 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002085 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002086 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002087 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002088
2089 unsigned WGSize[3];
2090 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002091 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002092 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002093 if (E->isTypeDependent() || E->isValueDependent() ||
2094 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002095 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2096 << "reqd_work_group_size" << E->getSourceRange();
2097 return;
2098 }
2099 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2100 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002101 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002102 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002103 WGSize[2]));
2104}
2105
Chandler Carruth1b03c872011-07-02 00:01:44 +00002106static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002107 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002108 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002109 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002110
2111 // Make sure that there is a string literal as the sections's single
2112 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002113 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002114 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002115 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002116 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002117 return;
2118 }
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Chris Lattner797c3c42009-08-10 19:03:04 +00002120 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002121 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002122 if (!Error.empty()) {
2123 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2124 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002125 return;
2126 }
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002128 // This attribute cannot be applied to local variables.
2129 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2130 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2131 return;
2132 }
2133
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002134 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002135 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002136}
2137
Chris Lattner6b6b5372008-06-26 18:38:35 +00002138
Chandler Carruth1b03c872011-07-02 00:01:44 +00002139static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002140 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002141 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002142 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002143 return;
2144 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002145
Chandler Carruth87c44602011-07-01 23:49:12 +00002146 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002147 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002148 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002149 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002150 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002151 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002152}
2153
Chandler Carruth1b03c872011-07-02 00:01:44 +00002154static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002155 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002156 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002157 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002158 return;
2159 }
Mike Stumpbf916502009-07-24 19:02:52 +00002160
Chandler Carruth87c44602011-07-01 23:49:12 +00002161 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002162 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002163 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002164 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002165 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002166 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002167}
2168
Chandler Carruth1b03c872011-07-02 00:01:44 +00002169static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002170 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002171 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002172 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002173
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002174 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002175}
2176
Chandler Carruth1b03c872011-07-02 00:01:44 +00002177static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002178 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2180 return;
2181 }
Mike Stumpbf916502009-07-24 19:02:52 +00002182
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002183 if (Attr.getNumArgs() != 0) {
2184 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2185 return;
2186 }
Mike Stumpbf916502009-07-24 19:02:52 +00002187
Chandler Carruth87c44602011-07-01 23:49:12 +00002188 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002189
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002190 if (!VD || !VD->hasLocalStorage()) {
2191 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2192 return;
2193 }
Mike Stumpbf916502009-07-24 19:02:52 +00002194
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002195 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002196 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002197 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002198 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2199 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002200 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002201 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002202 Attr.getParameterName();
2203 return;
2204 }
Mike Stumpbf916502009-07-24 19:02:52 +00002205
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002206 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2207 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002208 S.Diag(Attr.getParameterLoc(),
2209 diag::err_attribute_cleanup_arg_not_function)
2210 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002211 return;
2212 }
2213
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002214 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002215 S.Diag(Attr.getParameterLoc(),
2216 diag::err_attribute_cleanup_func_must_take_one_arg)
2217 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002218 return;
2219 }
Mike Stumpbf916502009-07-24 19:02:52 +00002220
Anders Carlsson89941c12009-02-07 23:16:50 +00002221 // We're currently more strict than GCC about what function types we accept.
2222 // If this ever proves to be a problem it should be easy to fix.
2223 QualType Ty = S.Context.getPointerType(VD->getType());
2224 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002225 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2226 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002227 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002228 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2229 Attr.getParameterName() << ParamTy << Ty;
2230 return;
2231 }
Mike Stumpbf916502009-07-24 19:02:52 +00002232
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002233 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002234 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002235}
2236
Mike Stumpbf916502009-07-24 19:02:52 +00002237/// Handle __attribute__((format_arg((idx)))) attribute based on
2238/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002239static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002240 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002241 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002242
Chandler Carruth87c44602011-07-01 23:49:12 +00002243 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002244 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002245 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002246 return;
2247 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002248
2249 // In C++ the implicit 'this' function parameter also counts, and they are
2250 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002251 bool HasImplicitThisParam = isInstanceMethod(D);
2252 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002253 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002254
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002255 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002256 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002257 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002258 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2259 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2261 << "format" << 2 << IdxExpr->getSourceRange();
2262 return;
2263 }
Mike Stumpbf916502009-07-24 19:02:52 +00002264
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002265 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2266 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2267 << "format" << 2 << IdxExpr->getSourceRange();
2268 return;
2269 }
Mike Stumpbf916502009-07-24 19:02:52 +00002270
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002271 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002272
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002273 if (HasImplicitThisParam) {
2274 if (ArgIdx == 0) {
2275 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2276 << "format_arg" << IdxExpr->getSourceRange();
2277 return;
2278 }
2279 ArgIdx--;
2280 }
2281
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002282 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002283 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002284
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002285 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2286 if (not_nsstring_type &&
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_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002292 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002293 << IdxExpr->getSourceRange();
2294 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002295 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002296 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002297 if (!isNSStringType(Ty, S.Context) &&
2298 !isCFStringType(Ty, S.Context) &&
2299 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002300 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002301 // FIXME: Should highlight the actual expression that has the wrong type.
2302 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002303 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002304 << IdxExpr->getSourceRange();
2305 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002306 }
2307
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002308 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002309 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002310}
2311
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002312enum FormatAttrKind {
2313 CFStringFormat,
2314 NSStringFormat,
2315 StrftimeFormat,
2316 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002317 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002318 InvalidFormat
2319};
2320
2321/// getFormatAttrKind - Map from format attribute names to supported format
2322/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002323static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002324 // Check for formats that get handled specially.
2325 if (Format == "NSString")
2326 return NSStringFormat;
2327 if (Format == "CFString")
2328 return CFStringFormat;
2329 if (Format == "strftime")
2330 return StrftimeFormat;
2331
2332 // Otherwise, check for supported formats.
2333 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupas69d53842012-01-27 09:14:17 +00002334 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002335 Format == "zcmn_err" ||
2336 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002337 return SupportedFormat;
2338
Duncan Sandsbc525952010-03-23 14:44:19 +00002339 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2340 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002341 return IgnoredFormat;
2342
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002343 return InvalidFormat;
2344}
2345
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002346/// Handle __attribute__((init_priority(priority))) attributes based on
2347/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002348static void handleInitPriorityAttr(Sema &S, Decl *D,
2349 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002350 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002351 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2352 return;
2353 }
2354
Chandler Carruth87c44602011-07-01 23:49:12 +00002355 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002356 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2357 Attr.setInvalid();
2358 return;
2359 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002360 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002361 if (S.Context.getAsArrayType(T))
2362 T = S.Context.getBaseElementType(T);
2363 if (!T->getAs<RecordType>()) {
2364 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2365 Attr.setInvalid();
2366 return;
2367 }
2368
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002369 if (Attr.getNumArgs() != 1) {
2370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2371 Attr.setInvalid();
2372 return;
2373 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002374 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002375
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002376 llvm::APSInt priority(32);
2377 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2378 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2379 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2380 << "init_priority" << priorityExpr->getSourceRange();
2381 Attr.setInvalid();
2382 return;
2383 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002384 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002385 if (prioritynum < 101 || prioritynum > 65535) {
2386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2387 << priorityExpr->getSourceRange();
2388 Attr.setInvalid();
2389 return;
2390 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002391 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002392 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002393}
2394
Mike Stumpbf916502009-07-24 19:02:52 +00002395/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2396/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002397static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002398
Chris Lattner545dd342008-06-28 23:36:30 +00002399 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002401 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002402 return;
2403 }
2404
Chris Lattner545dd342008-06-28 23:36:30 +00002405 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002407 return;
2408 }
2409
Chandler Carruth87c44602011-07-01 23:49:12 +00002410 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002411 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002412 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002413 return;
2414 }
2415
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002416 // In C++ the implicit 'this' function parameter also counts, and they are
2417 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002418 bool HasImplicitThisParam = isInstanceMethod(D);
2419 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002420 unsigned FirstIdx = 1;
2421
Chris Lattner5f9e2722011-07-23 10:55:15 +00002422 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002423
2424 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002425 if (Format.startswith("__") && Format.endswith("__"))
2426 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002427
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002428 // Check for supported formats.
2429 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002430
2431 if (Kind == IgnoredFormat)
2432 return;
2433
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002434 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002435 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002436 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002437 return;
2438 }
2439
2440 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002441 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002442 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002443 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2444 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002445 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002446 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002447 return;
2448 }
2449
2450 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002451 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002452 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002453 return;
2454 }
2455
2456 // FIXME: Do we need to bounds check?
2457 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002458
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002459 if (HasImplicitThisParam) {
2460 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002461 S.Diag(Attr.getLoc(),
2462 diag::err_format_attribute_implicit_this_format_string)
2463 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002464 return;
2465 }
2466 ArgIdx--;
2467 }
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Chris Lattner6b6b5372008-06-26 18:38:35 +00002469 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002470 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002471
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002472 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002473 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002474 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2475 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002476 return;
2477 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002478 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002479 // FIXME: do we need to check if the type is NSString*? What are the
2480 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002481 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002482 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002483 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2484 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002485 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002486 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002487 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002488 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002489 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002490 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2491 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002492 return;
2493 }
2494
2495 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002496 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002497 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002498 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2499 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002501 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002502 return;
2503 }
2504
2505 // check if the function is variadic if the 3rd argument non-zero
2506 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002507 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002508 ++NumArgs; // +1 for ...
2509 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002510 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002511 return;
2512 }
2513 }
2514
Chris Lattner3c73c412008-11-19 08:23:25 +00002515 // strftime requires FirstArg to be 0 because it doesn't read from any
2516 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002517 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002518 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002519 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2520 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002521 return;
2522 }
2523 // if 0 it disables parameter checking (to use with e.g. va_list)
2524 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002526 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002527 return;
2528 }
2529
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002530 // Check whether we already have an equivalent format attribute.
2531 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002532 i = D->specific_attr_begin<FormatAttr>(),
2533 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002534 i != e ; ++i) {
2535 FormatAttr *f = *i;
2536 if (f->getType() == Format &&
2537 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2538 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2539 // If we don't have a valid location for this attribute, adopt the
2540 // location.
2541 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002542 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002543 return;
2544 }
2545 }
2546
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002547 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002548 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002549 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002550}
2551
Chandler Carruth1b03c872011-07-02 00:01:44 +00002552static void handleTransparentUnionAttr(Sema &S, Decl *D,
2553 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002554 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002555 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002556 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002557
Chris Lattner6b6b5372008-06-26 18:38:35 +00002558
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002559 // Try to find the underlying union declaration.
2560 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002561 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002562 if (TD && TD->getUnderlyingType()->isUnionType())
2563 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2564 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002565 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002566
2567 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002568 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002569 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002570 return;
2571 }
2572
John McCall5e1cdac2011-10-07 06:10:15 +00002573 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002574 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002575 diag::warn_transparent_union_attribute_not_definition);
2576 return;
2577 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002578
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002579 RecordDecl::field_iterator Field = RD->field_begin(),
2580 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002581 if (Field == FieldEnd) {
2582 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2583 return;
2584 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002585
David Blaikie262bc182012-04-30 02:36:29 +00002586 FieldDecl *FirstField = &*Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002587 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002588 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002589 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002590 diag::warn_transparent_union_attribute_floating)
2591 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002592 return;
2593 }
2594
2595 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2596 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2597 for (; Field != FieldEnd; ++Field) {
2598 QualType FieldType = Field->getType();
2599 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2600 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2601 // Warn if we drop the attribute.
2602 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002603 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002604 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002605 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002606 diag::warn_transparent_union_attribute_field_size_align)
2607 << isSize << Field->getDeclName() << FieldBits;
2608 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002609 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002610 diag::note_transparent_union_first_field_size_align)
2611 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002612 return;
2613 }
2614 }
2615
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002616 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002617}
2618
Chandler Carruth1b03c872011-07-02 00:01:44 +00002619static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002620 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002621 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002622 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002623
Peter Collingbourne7a730022010-11-23 20:45:58 +00002624 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002625 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002626
Chris Lattner6b6b5372008-06-26 18:38:35 +00002627 // Make sure that there is a string literal as the annotation's single
2628 // argument.
2629 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002630 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002631 return;
2632 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002633
2634 // Don't duplicate annotations that are already set.
2635 for (specific_attr_iterator<AnnotateAttr>
2636 i = D->specific_attr_begin<AnnotateAttr>(),
2637 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2638 if ((*i)->getAnnotation() == SE->getString())
2639 return;
2640 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002641 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002642 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002643}
2644
Chandler Carruth1b03c872011-07-02 00:01:44 +00002645static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002646 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002647 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002649 return;
2650 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002651
2652 //FIXME: The C++0x version of this attribute has more limited applicabilty
2653 // than GNU's, and should error out when it is used to specify a
2654 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002655
Chris Lattner545dd342008-06-28 23:36:30 +00002656 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002657 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002658 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002659 }
Mike Stumpbf916502009-07-24 19:02:52 +00002660
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002661 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002662}
2663
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002664void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002665 // FIXME: Handle pack-expansions here.
2666 if (DiagnoseUnexpandedParameterPack(E))
2667 return;
2668
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002669 if (E->isTypeDependent() || E->isValueDependent()) {
2670 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002671 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002672 return;
2673 }
2674
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002675 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002676 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002677 llvm::APSInt Alignment(32);
Richard Smith282e7e62012-02-04 09:53:13 +00002678 ExprResult ICE =
2679 VerifyIntegerConstantExpression(E, &Alignment,
2680 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2681 /*AllowFold*/ false);
2682 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002683 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002684 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002685 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2686 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002687 return;
2688 }
2689
Richard Smith282e7e62012-02-04 09:53:13 +00002690 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Sean Huntcf807c42010-08-18 23:23:40 +00002691}
2692
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002693void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002694 // FIXME: Cache the number on the Attr object if non-dependent?
2695 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002696 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002697 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002698}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002699
Chandler Carruthd309c812011-07-01 23:49:16 +00002700/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002701/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002702///
Mike Stumpbf916502009-07-24 19:02:52 +00002703/// Despite what would be logical, the mode attribute is a decl attribute, not a
2704/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2705/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002706static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002707 // This attribute isn't documented, but glibc uses it. It changes
2708 // the width of an int or unsigned int to the specified size.
2709
2710 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002711 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002712 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002713
Chris Lattnerfbf13472008-06-27 22:18:37 +00002714
2715 IdentifierInfo *Name = Attr.getParameterName();
2716 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002717 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002718 return;
2719 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002720
Chris Lattner5f9e2722011-07-23 10:55:15 +00002721 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002722
2723 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002724 if (Str.startswith("__") && Str.endswith("__"))
2725 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002726
2727 unsigned DestWidth = 0;
2728 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002729 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002730 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002731 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002732 switch (Str[0]) {
2733 case 'Q': DestWidth = 8; break;
2734 case 'H': DestWidth = 16; break;
2735 case 'S': DestWidth = 32; break;
2736 case 'D': DestWidth = 64; break;
2737 case 'X': DestWidth = 96; break;
2738 case 'T': DestWidth = 128; break;
2739 }
2740 if (Str[1] == 'F') {
2741 IntegerMode = false;
2742 } else if (Str[1] == 'C') {
2743 IntegerMode = false;
2744 ComplexMode = true;
2745 } else if (Str[1] != 'I') {
2746 DestWidth = 0;
2747 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002748 break;
2749 case 4:
2750 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2751 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002752 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002753 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002754 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002755 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002756 break;
2757 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002758 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002759 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002760 break;
2761 }
2762
2763 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002764 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002765 OldTy = TD->getUnderlyingType();
2766 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2767 OldTy = VD->getType();
2768 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002769 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002770 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002771 return;
2772 }
Eli Friedman73397492009-03-03 06:41:03 +00002773
John McCall183700f2009-09-21 23:43:11 +00002774 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002775 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2776 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002777 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002778 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2779 } else if (ComplexMode) {
2780 if (!OldTy->isComplexType())
2781 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2782 } else {
2783 if (!OldTy->isFloatingType())
2784 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2785 }
2786
Mike Stump390b4cc2009-05-16 07:39:55 +00002787 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2788 // and friends, at least with glibc.
2789 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2790 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002791 // FIXME: Make sure floating-point mappings are accurate
2792 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002793 QualType NewTy;
2794 switch (DestWidth) {
2795 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002796 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002797 return;
2798 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002799 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002800 return;
2801 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002802 if (!IntegerMode) {
2803 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2804 return;
2805 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002806 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002807 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002808 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002809 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002810 break;
2811 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002812 if (!IntegerMode) {
2813 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2814 return;
2815 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002816 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002817 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002818 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002819 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002820 break;
2821 case 32:
2822 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002823 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002824 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002825 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002826 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002827 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002828 break;
2829 case 64:
2830 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002831 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002832 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002833 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002834 NewTy = S.Context.LongTy;
2835 else
2836 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002837 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002838 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002839 NewTy = S.Context.UnsignedLongTy;
2840 else
2841 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002842 break;
Eli Friedman73397492009-03-03 06:41:03 +00002843 case 96:
2844 NewTy = S.Context.LongDoubleTy;
2845 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002846 case 128:
2847 if (!IntegerMode) {
2848 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2849 return;
2850 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002851 if (OldTy->isSignedIntegerType())
2852 NewTy = S.Context.Int128Ty;
2853 else
2854 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002855 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002856 }
2857
Eli Friedman73397492009-03-03 06:41:03 +00002858 if (ComplexMode) {
2859 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002860 }
2861
2862 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002863 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002864 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002865 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002866 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002867 cast<ValueDecl>(D)->setType(NewTy);
2868}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002869
Chandler Carruth1b03c872011-07-02 00:01:44 +00002870static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002871 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002872 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002873 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002874
Chandler Carruth87c44602011-07-01 23:49:12 +00002875 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002877 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002878 return;
2879 }
Mike Stumpbf916502009-07-24 19:02:52 +00002880
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002881 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002882}
2883
Chandler Carruth1b03c872011-07-02 00:01:44 +00002884static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002885 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002886 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002887 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002888
Mike Stumpbf916502009-07-24 19:02:52 +00002889
Chandler Carruth87c44602011-07-01 23:49:12 +00002890 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002891 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002892 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002893 return;
2894 }
Mike Stumpbf916502009-07-24 19:02:52 +00002895
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002896 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002897}
2898
Chandler Carruth1b03c872011-07-02 00:01:44 +00002899static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2900 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002901 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002902 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002903 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002904
Chris Lattner7255a2d2010-06-22 00:03:40 +00002905
Chandler Carruth87c44602011-07-01 23:49:12 +00002906 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002908 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002909 return;
2910 }
2911
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002912 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002913 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002914}
2915
Chandler Carruth1b03c872011-07-02 00:01:44 +00002916static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002917 if (S.LangOpts.CUDA) {
2918 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002919 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2921 return;
2922 }
2923
Chandler Carruth87c44602011-07-01 23:49:12 +00002924 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002926 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002927 return;
2928 }
2929
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002930 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002931 } else {
2932 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2933 }
2934}
2935
Chandler Carruth1b03c872011-07-02 00:01:44 +00002936static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002937 if (S.LangOpts.CUDA) {
2938 // check the attribute arguments.
2939 if (Attr.getNumArgs() != 0) {
2940 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2941 return;
2942 }
2943
Chandler Carruth87c44602011-07-01 23:49:12 +00002944 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002946 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002947 return;
2948 }
2949
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002950 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002951 } else {
2952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2953 }
2954}
2955
Chandler Carruth1b03c872011-07-02 00:01:44 +00002956static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002957 if (S.LangOpts.CUDA) {
2958 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002959 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002960 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002961
Chandler Carruth87c44602011-07-01 23:49:12 +00002962 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002963 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002964 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002965 return;
2966 }
2967
Chandler Carruth87c44602011-07-01 23:49:12 +00002968 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002969 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002970 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002971 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2972 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2973 << FD->getType()
2974 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2975 "void");
2976 } else {
2977 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2978 << FD->getType();
2979 }
2980 return;
2981 }
2982
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002983 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002984 } else {
2985 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2986 }
2987}
2988
Chandler Carruth1b03c872011-07-02 00:01:44 +00002989static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002990 if (S.LangOpts.CUDA) {
2991 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002992 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002993 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002994
Peter Collingbourneced76712010-12-01 03:15:31 +00002995
Chandler Carruth87c44602011-07-01 23:49:12 +00002996 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002998 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002999 return;
3000 }
3001
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003002 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003003 } else {
3004 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3005 }
3006}
3007
Chandler Carruth1b03c872011-07-02 00:01:44 +00003008static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003009 if (S.LangOpts.CUDA) {
3010 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003011 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003012 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003013
Peter Collingbourneced76712010-12-01 03:15:31 +00003014
Chandler Carruth87c44602011-07-01 23:49:12 +00003015 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003016 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003017 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003018 return;
3019 }
3020
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003021 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003022 } else {
3023 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3024 }
3025}
3026
Chandler Carruth1b03c872011-07-02 00:01:44 +00003027static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003028 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003029 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003030 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003031
Chandler Carruth87c44602011-07-01 23:49:12 +00003032 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003033 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003034 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003035 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003036 return;
3037 }
Mike Stumpbf916502009-07-24 19:02:52 +00003038
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003039 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003040 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003041 return;
3042 }
Mike Stumpbf916502009-07-24 19:02:52 +00003043
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003044 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003045}
3046
Chandler Carruth1b03c872011-07-02 00:01:44 +00003047static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003048 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003049
Chandler Carruth87c44602011-07-01 23:49:12 +00003050 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003051 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3052 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003053 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003054 return;
3055
Chandler Carruth87c44602011-07-01 23:49:12 +00003056 if (!isa<ObjCMethodDecl>(D)) {
3057 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3058 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003059 return;
3060 }
3061
Chandler Carruth87c44602011-07-01 23:49:12 +00003062 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00003063 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003064 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003065 return;
3066 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003067 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003068 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003069 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003070 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003071 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003072 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003073 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003074 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003075 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003076 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003077 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003078 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003079 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003080 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003081 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003082 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003083 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003084 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003085 return;
3086 }
3087
Chris Lattner5f9e2722011-07-23 10:55:15 +00003088 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003089 PcsAttr::PCSType PCS;
3090 if (StrRef == "aapcs")
3091 PCS = PcsAttr::AAPCS;
3092 else if (StrRef == "aapcs-vfp")
3093 PCS = PcsAttr::AAPCS_VFP;
3094 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003095 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3096 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003097 return;
3098 }
3099
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003100 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003101 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003102 default:
3103 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003104 }
3105}
3106
Chandler Carruth1b03c872011-07-02 00:01:44 +00003107static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003108 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003109 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003110}
3111
John McCall711c52b2011-01-05 12:14:39 +00003112bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3113 if (attr.isInvalid())
3114 return true;
3115
Ted Kremenek831efae2011-04-15 05:49:29 +00003116 if ((attr.getNumArgs() != 0 &&
3117 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3118 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003119 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3120 attr.setInvalid();
3121 return true;
3122 }
3123
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003124 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3125 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003126 switch (attr.getKind()) {
3127 case AttributeList::AT_cdecl: CC = CC_C; break;
3128 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3129 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3130 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3131 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003132 case AttributeList::AT_pcs: {
3133 Expr *Arg = attr.getArg(0);
3134 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003135 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003136 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3137 << "pcs" << 1;
3138 attr.setInvalid();
3139 return true;
3140 }
3141
Chris Lattner5f9e2722011-07-23 10:55:15 +00003142 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003143 if (StrRef == "aapcs") {
3144 CC = CC_AAPCS;
3145 break;
3146 } else if (StrRef == "aapcs-vfp") {
3147 CC = CC_AAPCS_VFP;
3148 break;
3149 }
3150 // FALLS THROUGH
3151 }
David Blaikie7530c032012-01-17 06:56:22 +00003152 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003153 }
3154
3155 return false;
3156}
3157
Chandler Carruth1b03c872011-07-02 00:01:44 +00003158static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003159 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003160
3161 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003162 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003163 return;
3164
Chandler Carruth87c44602011-07-01 23:49:12 +00003165 if (!isa<ObjCMethodDecl>(D)) {
3166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3167 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003168 return;
3169 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003170
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003171 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003172}
3173
3174/// Checks a regparm attribute, returning true if it is ill-formed and
3175/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003176bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3177 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003178 return true;
3179
Chandler Carruth87c44602011-07-01 23:49:12 +00003180 if (Attr.getNumArgs() != 1) {
3181 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3182 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003183 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003184 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003185
Chandler Carruth87c44602011-07-01 23:49:12 +00003186 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003187 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003188 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003189 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003190 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003191 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003192 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003193 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003194 }
3195
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003196 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003197 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003198 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003199 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003200 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003201 }
3202
John McCall711c52b2011-01-05 12:14:39 +00003203 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003204 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003205 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003206 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003207 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003208 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003209 }
3210
John McCall711c52b2011-01-05 12:14:39 +00003211 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003212}
3213
Chandler Carruth1b03c872011-07-02 00:01:44 +00003214static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003215 if (S.LangOpts.CUDA) {
3216 // check the attribute arguments.
3217 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003218 // FIXME: 0 is not okay.
3219 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003220 return;
3221 }
3222
Chandler Carruth87c44602011-07-01 23:49:12 +00003223 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003225 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003226 return;
3227 }
3228
3229 Expr *MaxThreadsExpr = Attr.getArg(0);
3230 llvm::APSInt MaxThreads(32);
3231 if (MaxThreadsExpr->isTypeDependent() ||
3232 MaxThreadsExpr->isValueDependent() ||
3233 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3235 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3236 return;
3237 }
3238
3239 llvm::APSInt MinBlocks(32);
3240 if (Attr.getNumArgs() > 1) {
3241 Expr *MinBlocksExpr = Attr.getArg(1);
3242 if (MinBlocksExpr->isTypeDependent() ||
3243 MinBlocksExpr->isValueDependent() ||
3244 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3246 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3247 return;
3248 }
3249 }
3250
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003251 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003252 MaxThreads.getZExtValue(),
3253 MinBlocks.getZExtValue()));
3254 } else {
3255 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3256 }
3257}
3258
Chris Lattner0744e5f2008-06-29 00:23:49 +00003259//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003260// Checker-specific attribute handlers.
3261//===----------------------------------------------------------------------===//
3262
John McCallc7ad3812011-01-25 03:31:58 +00003263static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003264 return type->isDependentType() ||
3265 type->isObjCObjectPointerType() ||
3266 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003267}
3268static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003269 return type->isDependentType() ||
3270 type->isPointerType() ||
3271 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003272}
3273
Chandler Carruth1b03c872011-07-02 00:01:44 +00003274static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003275 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003276 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003277 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003278 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003279 return;
3280 }
3281
3282 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003283 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003284 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3285 cf = false;
3286 } else {
3287 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3288 cf = true;
3289 }
3290
3291 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003292 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003293 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003294 return;
3295 }
3296
3297 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003298 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003299 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003300 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003301}
3302
Chandler Carruth1b03c872011-07-02 00:01:44 +00003303static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3304 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003305 if (!isa<ObjCMethodDecl>(D)) {
3306 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003307 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003308 return;
3309 }
3310
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003311 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003312}
3313
Chandler Carruth1b03c872011-07-02 00:01:44 +00003314static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3315 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003316
John McCallc7ad3812011-01-25 03:31:58 +00003317 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003318
Chandler Carruth87c44602011-07-01 23:49:12 +00003319 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003320 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003321 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003322 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003323 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruth87c44602011-07-01 23:49:12 +00003324 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003325 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003326 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003327 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003328 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003329 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003330 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003331 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003332 return;
3333 }
Mike Stumpbf916502009-07-24 19:02:52 +00003334
John McCallc7ad3812011-01-25 03:31:58 +00003335 bool typeOK;
3336 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003337 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003338 default: llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003339 case AttributeList::AT_ns_returns_autoreleased:
3340 case AttributeList::AT_ns_returns_retained:
3341 case AttributeList::AT_ns_returns_not_retained:
3342 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3343 cf = false;
3344 break;
3345
3346 case AttributeList::AT_cf_returns_retained:
3347 case AttributeList::AT_cf_returns_not_retained:
3348 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3349 cf = true;
3350 break;
3351 }
3352
3353 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003354 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003355 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003356 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003357 }
Mike Stumpbf916502009-07-24 19:02:52 +00003358
Chandler Carruth87c44602011-07-01 23:49:12 +00003359 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003360 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003361 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003362 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003363 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003364 S.Context));
3365 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003366 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003367 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003368 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003369 return;
3370 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003371 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003372 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003373 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003374 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003375 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003376 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003377 return;
3378 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003379 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003380 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003381 return;
3382 };
3383}
3384
John McCalldc7c5ad2011-07-22 08:53:00 +00003385static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3386 const AttributeList &attr) {
3387 SourceLocation loc = attr.getLoc();
3388
3389 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3390
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003391 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003392 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003393 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003394 return;
3395 }
3396
3397 // Check that the method returns a normal pointer.
3398 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003399
3400 if (!resultType->isReferenceType() &&
3401 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003402 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3403 << SourceRange(loc)
3404 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3405
3406 // Drop the attribute.
3407 return;
3408 }
3409
3410 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003411 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003412}
3413
John McCall8dfac0b2011-09-30 05:12:12 +00003414/// Handle cf_audited_transfer and cf_unknown_transfer.
3415static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3416 if (!isa<FunctionDecl>(D)) {
3417 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003418 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003419 return;
3420 }
3421
3422 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3423
3424 // Check whether there's a conflicting attribute already present.
3425 Attr *Existing;
3426 if (IsAudited) {
3427 Existing = D->getAttr<CFUnknownTransferAttr>();
3428 } else {
3429 Existing = D->getAttr<CFAuditedTransferAttr>();
3430 }
3431 if (Existing) {
3432 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3433 << A.getName()
3434 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3435 << A.getRange() << Existing->getRange();
3436 return;
3437 }
3438
3439 // All clear; add the attribute.
3440 if (IsAudited) {
3441 D->addAttr(
3442 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3443 } else {
3444 D->addAttr(
3445 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3446 }
3447}
3448
John McCallfe98da02011-09-29 07:17:38 +00003449static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3450 const AttributeList &Attr) {
3451 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3452 if (!RD || RD->isUnion()) {
3453 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003454 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003455 }
3456
3457 IdentifierInfo *ParmName = Attr.getParameterName();
3458
3459 // In Objective-C, verify that the type names an Objective-C type.
3460 // We don't want to check this outside of ObjC because people sometimes
3461 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003462 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003463 // Check for an existing type with this name.
3464 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3465 Sema::LookupOrdinaryName);
3466 if (S.LookupName(R, Sc)) {
3467 NamedDecl *Target = R.getFoundDecl();
3468 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3469 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3470 S.Diag(Target->getLocStart(), diag::note_declared_at);
3471 }
3472 }
3473 }
3474
3475 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3476 ParmName));
3477}
3478
Chandler Carruth1b03c872011-07-02 00:01:44 +00003479static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3480 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003481 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003482
Chandler Carruth87c44602011-07-01 23:49:12 +00003483 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003484 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003485}
3486
Chandler Carruth1b03c872011-07-02 00:01:44 +00003487static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3488 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003489 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003490 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003491 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003492 return;
3493 }
3494
Chandler Carruth87c44602011-07-01 23:49:12 +00003495 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003496 QualType type = vd->getType();
3497
3498 if (!type->isDependentType() &&
3499 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003500 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003501 << type;
3502 return;
3503 }
3504
3505 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3506
3507 // If we have no lifetime yet, check the lifetime we're presumably
3508 // going to infer.
3509 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3510 lifetime = type->getObjCARCImplicitLifetime();
3511
3512 switch (lifetime) {
3513 case Qualifiers::OCL_None:
3514 assert(type->isDependentType() &&
3515 "didn't infer lifetime for non-dependent type?");
3516 break;
3517
3518 case Qualifiers::OCL_Weak: // meaningful
3519 case Qualifiers::OCL_Strong: // meaningful
3520 break;
3521
3522 case Qualifiers::OCL_ExplicitNone:
3523 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003524 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003525 << (lifetime == Qualifiers::OCL_Autoreleasing);
3526 break;
3527 }
3528
Chandler Carruth87c44602011-07-01 23:49:12 +00003529 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003530 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003531}
3532
Charles Davisf0122fe2010-02-16 18:27:26 +00003533static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman94287722012-02-23 22:46:33 +00003534 switch (Attr.getKind()) {
3535 default:
3536 return false;
3537 case AttributeList::AT_dllimport:
3538 case AttributeList::AT_dllexport:
3539 case AttributeList::AT_uuid:
3540 case AttributeList::AT_deprecated:
3541 case AttributeList::AT_noreturn:
3542 case AttributeList::AT_nothrow:
3543 case AttributeList::AT_naked:
3544 case AttributeList::AT_noinline:
3545 return true;
3546 }
Francois Pichet11542142010-12-19 06:50:37 +00003547}
3548
3549//===----------------------------------------------------------------------===//
3550// Microsoft specific attribute handlers.
3551//===----------------------------------------------------------------------===//
3552
Chandler Carruth1b03c872011-07-02 00:01:44 +00003553static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003554 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003555 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003556 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003557 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003558
Francois Pichet11542142010-12-19 06:50:37 +00003559 Expr *Arg = Attr.getArg(0);
3560 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003561 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3563 << "uuid" << 1;
3564 return;
3565 }
3566
Chris Lattner5f9e2722011-07-23 10:55:15 +00003567 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003568
3569 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3570 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003571
Francois Pichetd3d3be92010-12-20 01:41:49 +00003572 // Validate GUID length.
3573 if (IsCurly && StrRef.size() != 38) {
3574 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3575 return;
3576 }
3577 if (!IsCurly && StrRef.size() != 36) {
3578 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3579 return;
3580 }
3581
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003582 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003583 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003584 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003585 if (IsCurly) // Skip the optional '{'
3586 ++I;
3587
3588 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003589 if (i == 8 || i == 13 || i == 18 || i == 23) {
3590 if (*I != '-') {
3591 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3592 return;
3593 }
3594 } else if (!isxdigit(*I)) {
3595 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3596 return;
3597 }
3598 I++;
3599 }
Francois Pichet11542142010-12-19 06:50:37 +00003600
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003601 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003602 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003603 } else
Francois Pichet11542142010-12-19 06:50:37 +00003604 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003605}
3606
Ted Kremenekb71368d2009-05-09 02:44:38 +00003607//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003608// Top Level Sema Entry Points
3609//===----------------------------------------------------------------------===//
3610
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3612 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003613 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003614 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3615 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3616 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003617 default:
3618 break;
3619 }
3620}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003621
Chandler Carruth1b03c872011-07-02 00:01:44 +00003622static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3623 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003624 switch (Attr.getKind()) {
Michael Hane53ac8a2012-03-07 00:12:16 +00003625 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3626 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3627 case AttributeList::AT_iboutletcollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003628 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003629 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003630 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003631 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003632 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003633 case AttributeList::AT_neon_vector_type:
3634 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003635 // Ignore these, these are type attributes, handled by
3636 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003637 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003638 case AttributeList::AT_device:
3639 case AttributeList::AT_host:
3640 case AttributeList::AT_overloadable:
3641 // Ignore, this is a non-inheritable attribute, handled
3642 // by ProcessNonInheritableDeclAttr.
3643 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003644 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3645 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003646 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003647 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003648 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003649 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3650 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3651 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003652 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003653 handleDependencyAttr (S, D, Attr); break;
3654 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3655 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3656 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3657 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3658 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003659 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003660 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003661 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003662 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3663 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3664 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3665 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003666 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003667 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003668 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003669 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3670 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3671 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3672 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3673 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003674 case AttributeList::AT_ownership_returns:
3675 case AttributeList::AT_ownership_takes:
3676 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003677 handleOwnershipAttr (S, D, Attr); break;
3678 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3679 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3680 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3681 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3682 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003683
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003684 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003685 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003686 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003687 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003688
John McCalldc7c5ad2011-07-22 08:53:00 +00003689 case AttributeList::AT_objc_returns_inner_pointer:
3690 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3691
John McCallfe98da02011-09-29 07:17:38 +00003692 case AttributeList::AT_ns_bridged:
3693 handleNSBridgedAttr(S, scope, D, Attr); break;
3694
John McCall8dfac0b2011-09-30 05:12:12 +00003695 case AttributeList::AT_cf_audited_transfer:
3696 case AttributeList::AT_cf_unknown_transfer:
3697 handleCFTransferAttr(S, D, Attr); break;
3698
Ted Kremenekb71368d2009-05-09 02:44:38 +00003699 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003700 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003701 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003702 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003703 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003704
3705 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003706 case AttributeList::AT_ns_returns_not_retained:
3707 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003708 case AttributeList::AT_ns_returns_retained:
3709 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003710 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003711
Michael Hane53ac8a2012-03-07 00:12:16 +00003712 case AttributeList::AT_reqd_work_group_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003713 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003714
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003715 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003716 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003717
Chandler Carruth1b03c872011-07-02 00:01:44 +00003718 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003719 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003720 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3721 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003722 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003723 handleArcWeakrefUnavailableAttr (S, D, Attr);
3724 break;
Patrick Beardb2f68202012-04-06 18:12:22 +00003725 case AttributeList::AT_objc_root_class:
3726 handleObjCRootClassAttr(S, D, Attr);
3727 break;
Ted Kremenek71207fc2012-01-05 22:47:47 +00003728 case AttributeList::AT_objc_requires_property_definitions:
3729 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00003730 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003731 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00003732 case AttributeList::AT_returns_twice:
3733 handleReturnsTwiceAttr(S, D, Attr);
3734 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003735 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3736 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3737 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003738 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003739 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3740 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3741 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003742 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003743 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003744 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003745 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003746 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003747 break;
John McCalld5313b02011-03-02 11:33:24 +00003748 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003749 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003750 break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003751 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003752 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3753 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3754 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3755 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3756 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3757 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3758 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3759 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003760 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003761 // Just ignore
3762 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003763 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003764 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003765 break;
John McCall04a67a62010-02-05 21:31:56 +00003766 case AttributeList::AT_stdcall:
3767 case AttributeList::AT_cdecl:
3768 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003769 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003770 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003771 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003772 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003773 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003774 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003775 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003776 break;
Francois Pichet11542142010-12-19 06:50:37 +00003777 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003778 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003779 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003780
3781 // Thread safety attributes:
3782 case AttributeList::AT_guarded_var:
3783 handleGuardedVarAttr(S, D, Attr);
3784 break;
3785 case AttributeList::AT_pt_guarded_var:
3786 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3787 break;
3788 case AttributeList::AT_scoped_lockable:
3789 handleLockableAttr(S, D, Attr, /*scoped = */true);
3790 break;
Kostya Serebryany71efba02012-01-24 19:25:38 +00003791 case AttributeList::AT_no_address_safety_analysis:
3792 handleNoAddressSafetyAttr(S, D, Attr);
3793 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003794 case AttributeList::AT_no_thread_safety_analysis:
3795 handleNoThreadSafetyAttr(S, D, Attr);
3796 break;
3797 case AttributeList::AT_lockable:
3798 handleLockableAttr(S, D, Attr);
3799 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003800 case AttributeList::AT_guarded_by:
3801 handleGuardedByAttr(S, D, Attr);
3802 break;
3803 case AttributeList::AT_pt_guarded_by:
3804 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3805 break;
3806 case AttributeList::AT_exclusive_lock_function:
3807 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3808 break;
3809 case AttributeList::AT_exclusive_locks_required:
3810 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3811 break;
3812 case AttributeList::AT_exclusive_trylock_function:
3813 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3814 break;
3815 case AttributeList::AT_lock_returned:
3816 handleLockReturnedAttr(S, D, Attr);
3817 break;
3818 case AttributeList::AT_locks_excluded:
3819 handleLocksExcludedAttr(S, D, Attr);
3820 break;
3821 case AttributeList::AT_shared_lock_function:
3822 handleLockFunAttr(S, D, Attr);
3823 break;
3824 case AttributeList::AT_shared_locks_required:
3825 handleLocksRequiredAttr(S, D, Attr);
3826 break;
3827 case AttributeList::AT_shared_trylock_function:
3828 handleTrylockFunAttr(S, D, Attr);
3829 break;
3830 case AttributeList::AT_unlock_function:
3831 handleUnlockFunAttr(S, D, Attr);
3832 break;
3833 case AttributeList::AT_acquired_before:
3834 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3835 break;
3836 case AttributeList::AT_acquired_after:
3837 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3838 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003839
Chris Lattner803d0802008-06-29 00:43:07 +00003840 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003841 // Ask target about the attribute.
3842 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3843 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003844 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3845 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003846 break;
3847 }
3848}
3849
Peter Collingbourne60700392011-01-21 02:08:45 +00003850/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3851/// the attribute applies to decls. If the attribute is a type attribute, just
3852/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3853/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003854static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3855 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003856 bool NonInheritable, bool Inheritable) {
3857 if (Attr.isInvalid())
3858 return;
3859
3860 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3861 // FIXME: Try to deal with other __declspec attributes!
3862 return;
3863
3864 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003865 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003866
3867 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003868 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003869}
3870
Chris Lattner803d0802008-06-29 00:43:07 +00003871/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3872/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003873void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003874 const AttributeList *AttrList,
3875 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003876 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003877 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003878 }
3879
3880 // GCC accepts
3881 // static int a9 __attribute__((weakref));
3882 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003883 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003884 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003885 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003886 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003887 }
3888}
3889
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003890// Annotation attributes are the only attributes allowed after an access
3891// specifier.
3892bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3893 const AttributeList *AttrList) {
3894 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3895 if (l->getKind() == AttributeList::AT_annotate) {
3896 handleAnnotateAttr(*this, ASDecl, *l);
3897 } else {
3898 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3899 return true;
3900 }
3901 }
3902
3903 return false;
3904}
3905
John McCalle82247a2011-10-01 05:17:03 +00003906/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3907/// contains any decl attributes that we should warn about.
3908static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3909 for ( ; A; A = A->getNext()) {
3910 // Only warn if the attribute is an unignored, non-type attribute.
3911 if (A->isUsedAsTypeAttr()) continue;
3912 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3913
3914 if (A->getKind() == AttributeList::UnknownAttribute) {
3915 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3916 << A->getName() << A->getRange();
3917 } else {
3918 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3919 << A->getName() << A->getRange();
3920 }
3921 }
3922}
3923
3924/// checkUnusedDeclAttributes - Given a declarator which is not being
3925/// used to build a declaration, complain about any decl attributes
3926/// which might be lying around on it.
3927void Sema::checkUnusedDeclAttributes(Declarator &D) {
3928 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3929 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3930 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3931 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3932}
3933
Ryan Flynne25ff832009-07-30 03:15:39 +00003934/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3935/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003936NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3937 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003938 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003939 NamedDecl *NewD = 0;
3940 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003941 FunctionDecl *NewFD;
3942 // FIXME: Missing call to CheckFunctionDeclaration().
3943 // FIXME: Mangling?
3944 // FIXME: Is the qualifier info correct?
3945 // FIXME: Is the DeclContext correct?
3946 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3947 Loc, Loc, DeclarationName(II),
3948 FD->getType(), FD->getTypeSourceInfo(),
3949 SC_None, SC_None,
3950 false/*isInlineSpecified*/,
3951 FD->hasPrototype(),
3952 false/*isConstexprSpecified*/);
3953 NewD = NewFD;
3954
3955 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003956 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003957
3958 // Fake up parameter variables; they are declared as if this were
3959 // a typedef.
3960 QualType FDTy = FD->getType();
3961 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3962 SmallVector<ParmVarDecl*, 16> Params;
3963 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3964 AE = FT->arg_type_end(); AI != AE; ++AI) {
3965 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3966 Param->setScopeInfo(0, Params.size());
3967 Params.push_back(Param);
3968 }
David Blaikie4278c652011-09-21 18:16:56 +00003969 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003970 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003971 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3972 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003973 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003974 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003975 VD->getStorageClass(),
3976 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003977 if (VD->getQualifier()) {
3978 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003979 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003980 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003981 }
3982 return NewD;
3983}
3984
3985/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3986/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003987void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003988 if (W.getUsed()) return; // only do this once
3989 W.setUsed(true);
3990 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3991 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003992 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003993 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3994 NDId->getName()));
3995 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003996 WeakTopLevelDecl.push_back(NewD);
3997 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3998 // to insert Decl at TU scope, sorry.
3999 DeclContext *SavedContext = CurContext;
4000 CurContext = Context.getTranslationUnitDecl();
4001 PushOnScopeChains(NewD, S);
4002 CurContext = SavedContext;
4003 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004004 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004005 }
4006}
4007
Chris Lattner0744e5f2008-06-29 00:23:49 +00004008/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4009/// it, apply them to D. This is a bit tricky because PD can have attributes
4010/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004011void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4012 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004013 // It's valid to "forward-declare" #pragma weak, in which case we
4014 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004015 if (Inheritable) {
4016 LoadExternalWeakUndeclaredIdentifiers();
4017 if (!WeakUndeclaredIdentifiers.empty()) {
4018 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4019 if (IdentifierInfo *Id = ND->getIdentifier()) {
4020 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4021 = WeakUndeclaredIdentifiers.find(Id);
4022 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4023 WeakInfo W = I->second;
4024 DeclApplyPragmaWeak(S, ND, W);
4025 WeakUndeclaredIdentifiers[Id] = W;
4026 }
John McCalld4aff0e2010-10-27 00:59:00 +00004027 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004028 }
4029 }
4030 }
4031
Chris Lattner0744e5f2008-06-29 00:23:49 +00004032 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004033 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004034 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004035
Chris Lattner0744e5f2008-06-29 00:23:49 +00004036 // Walk the declarator structure, applying decl attributes that were in a type
4037 // position to the decl itself. This handles cases like:
4038 // int *__attr__(x)** D;
4039 // when X is a decl attribute.
4040 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4041 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004042 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004043
Chris Lattner0744e5f2008-06-29 00:23:49 +00004044 // Finally, apply any attributes on the decl itself.
4045 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004046 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004047}
John McCall54abf7d2009-11-04 02:18:39 +00004048
John McCallf85e1932011-06-15 23:02:42 +00004049/// Is the given declaration allowed to use a forbidden type?
4050static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4051 // Private ivars are always okay. Unfortunately, people don't
4052 // always properly make their ivars private, even in system headers.
4053 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004054 // Function declarations in sys headers will be marked unavailable.
4055 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4056 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004057 return false;
4058
4059 // Require it to be declared in a system header.
4060 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4061}
4062
4063/// Handle a delayed forbidden-type diagnostic.
4064static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4065 Decl *decl) {
4066 if (decl && isForbiddenTypeAllowed(S, decl)) {
4067 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4068 "this system declaration uses an unsupported type"));
4069 return;
4070 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004071 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004072 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4073 // FIXME. we may want to supress diagnostics for all
4074 // kind of forbidden type messages on unavailable functions.
4075 if (FD->hasAttr<UnavailableAttr>() &&
4076 diag.getForbiddenTypeDiagnostic() ==
4077 diag::err_arc_array_param_no_ownership) {
4078 diag.Triggered = true;
4079 return;
4080 }
4081 }
John McCallf85e1932011-06-15 23:02:42 +00004082
4083 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4084 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4085 diag.Triggered = true;
4086}
4087
John McCalleee1d542011-02-14 07:13:47 +00004088// This duplicates a vector push_back but hides the need to know the
4089// size of the type.
4090void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4091 assert(StackSize <= StackCapacity);
4092
4093 // Grow the stack if necessary.
4094 if (StackSize == StackCapacity) {
4095 unsigned newCapacity = 2 * StackCapacity + 2;
4096 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4097 const char *oldBuffer = (const char*) Stack;
4098
4099 if (StackCapacity)
4100 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4101
4102 delete[] oldBuffer;
4103 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4104 StackCapacity = newCapacity;
4105 }
4106
4107 assert(StackSize < StackCapacity);
4108 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00004109}
4110
John McCalleee1d542011-02-14 07:13:47 +00004111void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4112 Decl *decl) {
4113 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00004114
John McCalleee1d542011-02-14 07:13:47 +00004115 // Check the invariants.
4116 assert(DD.StackSize >= state.SavedStackSize);
4117 assert(state.SavedStackSize >= DD.ActiveStackBase);
4118 assert(DD.ParsingDepth > 0);
4119
4120 // Drop the parsing depth.
4121 DD.ParsingDepth--;
4122
4123 // If there are no active diagnostics, we're done.
4124 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00004125 return;
4126
John McCall2f514482010-01-27 03:50:35 +00004127 // We only want to actually emit delayed diagnostics when we
4128 // successfully parsed a decl.
John McCalle8c904f2012-01-26 20:04:03 +00004129 if (decl) {
John McCalleee1d542011-02-14 07:13:47 +00004130 // We emit all the active diagnostics, not just those starting
4131 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00004132 // decl spec and another for each declarator; in a decl group like:
4133 // deprecated_typedef foo, *bar, baz();
4134 // only the declarator pops will be passed decls. This is correct;
4135 // we really do need to consider delayed diagnostics from the decl spec
4136 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00004137 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4138 DelayedDiagnostic &diag = DD.Stack[i];
4139 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004140 continue;
4141
John McCalleee1d542011-02-14 07:13:47 +00004142 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004143 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004144 // Don't bother giving deprecation diagnostics if the decl is invalid.
4145 if (!decl->isInvalidDecl())
4146 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004147 break;
4148
4149 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00004150 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004151 break;
John McCallf85e1932011-06-15 23:02:42 +00004152
4153 case DelayedDiagnostic::ForbiddenType:
4154 handleDelayedForbiddenType(S, diag, decl);
4155 break;
John McCall2f514482010-01-27 03:50:35 +00004156 }
4157 }
4158 }
4159
John McCall58e6f342010-03-16 05:22:47 +00004160 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00004161 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00004162 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00004163
John McCalleee1d542011-02-14 07:13:47 +00004164 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00004165}
4166
4167static bool isDeclDeprecated(Decl *D) {
4168 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004169 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004170 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004171 // A category implicitly has the availability of the interface.
4172 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4173 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004174 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4175 return false;
4176}
4177
John McCall9c3087b2010-08-26 02:13:20 +00004178void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004179 Decl *Ctx) {
4180 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004181 return;
4182
John McCall2f514482010-01-27 03:50:35 +00004183 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004184 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004185 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004186 << DD.getDeprecationDecl()->getDeclName()
4187 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004188 else if (DD.getUnknownObjCClass()) {
4189 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4190 << DD.getDeprecationDecl()->getDeclName();
4191 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4192 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004193 else
4194 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004195 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004196}
4197
Chris Lattner5f9e2722011-07-23 10:55:15 +00004198void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004199 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004200 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004201 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004202 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004203 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4204 UnknownObjCClass,
4205 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004206 return;
4207 }
4208
4209 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004210 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004211 return;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004212 if (!Message.empty()) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004213 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4214 << Message;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004215 Diag(D->getLocation(),
4216 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4217 : diag::note_previous_decl) << D->getDeclName();
4218 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004219 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00004220 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004221 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004222 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004223 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004224 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4225 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004226 }
John McCall54abf7d2009-11-04 02:18:39 +00004227}