blob: 435507f9d78f944bd4ace759f4c9c54423384a94 [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"
Chris Lattner797c3c42009-08-10 19:03:04 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000026using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000027using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000028
John McCall883cc2c2011-03-02 12:29:23 +000029/// These constants match the enumerated choices of
30/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000031enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000032 ExpectedFunction,
33 ExpectedUnion,
34 ExpectedVariableOrFunction,
35 ExpectedFunctionOrMethod,
36 ExpectedParameter,
37 ExpectedParameterOrMethod,
38 ExpectedFunctionMethodOrBlock,
39 ExpectedClassOrVirtualMethod,
40 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
42 ExpectedVirtualMethod,
43 ExpectedClassMember,
44 ExpectedVariable,
45 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000046 ExpectedVariableFunctionOrLabel,
47 ExpectedFieldOrGlobalVar
John McCall883cc2c2011-03-02 12:29:23 +000048};
49
Chris Lattnere5c5ee12008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruth87c44602011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000065
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070
John McCall183700f2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000072}
73
Daniel Dunbar35682492008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopesd20254f2009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
87 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000088}
89
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000102}
103
John McCall711c52b2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000110}
111
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000118 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000133}
134
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148}
149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000156 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000175
John McCall506b57e2010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000211
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221 return true;
222}
223
224///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000225/// \brief Check if passed in Decl is a field or potentially shared global var
226/// \return true if the Decl is a field or potentially shared global variable
227///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000228static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000229 if (isa<FieldDecl>(D))
230 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000231 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000232 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
233
234 return false;
235}
236
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000237/// \brief Check if the passed-in expression is of type int or bool.
238static bool isIntOrBool(Expr *Exp) {
239 QualType QT = Exp->getType();
240 return QT->isBooleanType() || QT->isIntegerType();
241}
242
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000243///
244/// \brief Check if passed in Decl is a pointer type.
245/// Note that this function may produce an error message.
246/// \return true if the Decl is a pointer type; false otherwise
247///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000248static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
249 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000250 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000251 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000252 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000253 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
254 << Attr.getName()->getName() << QT;
255 } else {
256 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
257 << Attr.getName();
258 }
259 return false;
260}
261
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000262/// \brief Checks that the passed in QualType either is of RecordType or points
263/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000264static const RecordType *getRecordType(QualType QT) {
265 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000266 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000267
268 // Now check if we point to record type.
269 if (const PointerType *PT = QT->getAs<PointerType>())
270 return PT->getPointeeType()->getAs<RecordType>();
271
272 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000273}
274
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000275/// \brief Thread Safety Analysis: Checks that the passed in RecordType
276/// resolves to a lockable object. May flag an error.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000277static bool checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
278 const RecordType *RT) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000279 // Flag error if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000280 if (!RT) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_class)
282 << Attr.getName();
283 return false;
284 }
285 // Flag error if the type is not lockable.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000286 if (!RT->getDecl()->getAttr<LockableAttr>()) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000287 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_lockable)
288 << Attr.getName();
289 return false;
290 }
291 return true;
292}
293
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000294/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
295/// from Sidx, resolve to a lockable object. May flag an error.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000296/// \param Sidx The attribute argument index to start checking with.
297/// \param ParamIdxOk Whether an argument can be indexing into a function
298/// parameter list.
299static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
300 const AttributeList &Attr,
301 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000302 int Sidx = 0,
303 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000304 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000305 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000306
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000307 if (ArgExp->isTypeDependent()) {
308 // FIXME -- need to processs this again on template instantiation
309 Args.push_back(ArgExp);
310 continue;
311 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000312
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000313 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000314
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000315 // First see if we can just cast to record type, or point to record type.
316 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000317
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000318 // Now check if we index into a record type function param.
319 if(!RT && ParamIdxOk) {
320 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000321 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
322 if(FD && IL) {
323 unsigned int NumParams = FD->getNumParams();
324 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000325 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
326 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
327 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000328 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
329 << Attr.getName() << Idx + 1 << NumParams;
330 return false;
331 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000332 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
333 RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000334 }
335 }
336
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000337 if (!checkForLockableRecord(S, D, Attr, RT))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000338 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000339
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000340 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000341 }
342 return true;
343}
344
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000345//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000346// Attribute Implementations
347//===----------------------------------------------------------------------===//
348
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000349// FIXME: All this manual attribute parsing code is gross. At the
350// least add some helper functions to check most argument patterns (#
351// and types of args).
352
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000353static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
354 bool pointer = false) {
355 assert(!Attr.isInvalid());
356
357 if (!checkAttributeNumArgs(S, Attr, 0))
358 return;
359
360 // D must be either a member field or global (potentially shared) variable.
361 if (!mayBeSharedVariable(D)) {
362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000363 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000364 return;
365 }
366
367 if (pointer && !checkIsPointer(S, D, Attr))
368 return;
369
370 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000371 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000372 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000373 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000374}
375
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000376static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000377 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000378 assert(!Attr.isInvalid());
379
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000380 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000381 return;
382
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000383 Expr *Arg = Attr.getArg(0);
384
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000385 // D must be either a member field or global (potentially shared) variable.
386 if (!mayBeSharedVariable(D)) {
387 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000388 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000389 return;
390 }
391
392 if (pointer && !checkIsPointer(S, D, Attr))
393 return;
394
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000395 if (Arg->isTypeDependent())
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000396 // FIXME: handle attributes with dependent types
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000397 return;
398
399 // check that the argument is lockable object
400 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000401 return;
402
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000403 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000404 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000405 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000406 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000407 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000408}
409
410
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000411static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
412 bool scoped = false) {
413 assert(!Attr.isInvalid());
414
415 if (!checkAttributeNumArgs(S, Attr, 0))
416 return;
417
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000418 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000419 if (!isa<CXXRecordDecl>(D)) {
420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
421 << Attr.getName() << ExpectedClass;
422 return;
423 }
424
425 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000426 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000427 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000428 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000429}
430
431static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
432 const AttributeList &Attr) {
433 assert(!Attr.isInvalid());
434
435 if (!checkAttributeNumArgs(S, Attr, 0))
436 return;
437
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000438 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
440 << Attr.getName() << ExpectedFunctionOrMethod;
441 return;
442 }
443
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000444 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000445 S.Context));
446}
447
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000448static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
449 bool before) {
450 assert(!Attr.isInvalid());
451
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000452 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000453 return;
454
455 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000456 ValueDecl *VD = dyn_cast<ValueDecl>(D);
457 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000458 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000460 return;
461 }
462
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000463 // Check that this attribute only applies to lockable types
464 QualType QT = VD->getType();
465 if (!QT->isDependentType()) {
466 const RecordType *RT = getRecordType(QT);
467 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
468 S.Diag(Attr.getLoc(), diag::err_attribute_decl_not_lockable)
469 << Attr.getName();
470 return;
471 }
472 }
473
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000474 SmallVector<Expr*, 1> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000475 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000476 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000477 return;
478
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000479 unsigned Size = Args.size();
480 assert(Size == Attr.getNumArgs());
481 Expr **StartArg = Size == 0 ? 0 : &Args[0];
482
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000483 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000484 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000485 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000486 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000487 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000488 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000489}
490
491static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000492 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000493 assert(!Attr.isInvalid());
494
495 // zero or more arguments ok
496
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000497 // check that the attribute is applied to a function
498 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000499 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
500 << Attr.getName() << ExpectedFunctionOrMethod;
501 return;
502 }
503
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000504 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000505 SmallVector<Expr*, 1> Args;
506 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000507 return;
508
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000509 unsigned Size = Args.size();
510 assert(Size == Attr.getNumArgs());
511 Expr **StartArg = Size == 0 ? 0 : &Args[0];
512
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000513 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000514 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000515 S.Context, StartArg,
516 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000517 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000518 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000519 S.Context, StartArg,
520 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000521}
522
523static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000524 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000525 assert(!Attr.isInvalid());
526
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000527 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000528 return;
529
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000530
531 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
533 << Attr.getName() << ExpectedFunctionOrMethod;
534 return;
535 }
536
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000537 if (!isIntOrBool(Attr.getArg(0))) {
538 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
539 << Attr.getName();
540 return;
541 }
542
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000543 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000544 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000545 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000546 return;
547
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000548 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000549 Expr **StartArg = Size == 0 ? 0 : &Args[0];
550
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000551 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000552 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000553 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000554 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000555 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000556 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000557 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000558 S.Context,
559 Attr.getArg(0),
560 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000561}
562
563static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000564 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000565 assert(!Attr.isInvalid());
566
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000567 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000568 return;
569
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000570 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000571 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
572 << Attr.getName() << ExpectedFunctionOrMethod;
573 return;
574 }
575
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000576 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000577 SmallVector<Expr*, 1> Args;
578 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000579 return;
580
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000581 unsigned Size = Args.size();
582 assert(Size == Attr.getNumArgs());
583 Expr **StartArg = Size == 0 ? 0 : &Args[0];
584
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000585 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000586 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000587 S.Context, StartArg,
588 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000589 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000590 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000591 S.Context, StartArg,
592 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000593}
594
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000595static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000596 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000597 assert(!Attr.isInvalid());
598
599 // zero or more arguments ok
600
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000601 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000602 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
603 << Attr.getName() << ExpectedFunctionOrMethod;
604 return;
605 }
606
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000607 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000608 SmallVector<Expr*, 1> Args;
609 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000610 return;
611
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000612 unsigned Size = Args.size();
613 assert(Size == Attr.getNumArgs());
614 Expr **StartArg = Size == 0 ? 0 : &Args[0];
615
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000616 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000617 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000618}
619
620static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000621 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000622 assert(!Attr.isInvalid());
623
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000624 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000625 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000626 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000627
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000628 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
630 << Attr.getName() << ExpectedFunctionOrMethod;
631 return;
632 }
633
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000634 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000635 return;
636
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000637 // check that the argument is lockable object
638 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
639 return;
640
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000641 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000642}
643
644static void handleLocksExcludedAttr(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
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000648 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000649 return;
650
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000651 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
653 << Attr.getName() << ExpectedFunctionOrMethod;
654 return;
655 }
656
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000657 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000658 SmallVector<Expr*, 1> Args;
659 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000660 return;
661
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000662 unsigned Size = Args.size();
663 assert(Size == Attr.getNumArgs());
664 Expr **StartArg = Size == 0 ? 0 : &Args[0];
665
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000666 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000667 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000668}
669
670
Chandler Carruth1b03c872011-07-02 00:01:44 +0000671static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
672 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000673 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000674 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000675 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000676 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000677 }
Mike Stumpbf916502009-07-24 19:02:52 +0000678
Chris Lattner6b6b5372008-06-26 18:38:35 +0000679 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000680
681 Expr *sizeExpr;
682
683 // Special case where the argument is a template id.
684 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000685 CXXScopeSpec SS;
686 UnqualifiedId id;
687 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000688
689 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
690 if (Size.isInvalid())
691 return;
692
693 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000694 } else {
695 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000696 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000697 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000698
Peter Collingbourne7a730022010-11-23 20:45:58 +0000699 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000700 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000701
702 // Instantiate/Install the vector type, and let Sema build the type for us.
703 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000704 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000705 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000706 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000707 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000708
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000709 // Remember this typedef decl, we will need it later for diagnostics.
710 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000711 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000712}
713
Chandler Carruth1b03c872011-07-02 00:01:44 +0000714static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000716 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000717 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000718
Chandler Carruth87c44602011-07-01 23:49:12 +0000719 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000720 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000721 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000722 // If the alignment is less than or equal to 8 bits, the packed attribute
723 // has no effect.
724 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000725 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000726 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000727 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000728 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000729 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000730 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000731 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000732}
733
Chandler Carruth1b03c872011-07-02 00:01:44 +0000734static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000735 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000736 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000737 else
738 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
739}
740
Chandler Carruth1b03c872011-07-02 00:01:44 +0000741static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000742 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000743 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000744 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000745
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000746 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000747 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000748 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000749 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000750 return;
751 }
752
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000753 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000754}
755
Ted Kremenek2f041d02011-09-29 07:02:25 +0000756static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
757 // The IBOutlet/IBOutletCollection attributes only apply to instance
758 // variables or properties of Objective-C classes. The outlet must also
759 // have an object reference type.
760 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
761 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
762 S.Diag(Attr.getLoc(), diag::err_iboutlet_object_type)
763 << Attr.getName() << VD->getType() << 0;
764 return false;
765 }
766 }
767 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
768 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
769 S.Diag(Attr.getLoc(), diag::err_iboutlet_object_type)
770 << Attr.getName() << PD->getType() << 1;
771 return false;
772 }
773 }
774 else {
775 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
776 return false;
777 }
778
779 return true;
780}
781
Chandler Carruth1b03c872011-07-02 00:01:44 +0000782static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000783 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000784 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000785 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000786
787 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000788 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000789
Ted Kremenek2f041d02011-09-29 07:02:25 +0000790 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000791}
792
Chandler Carruth1b03c872011-07-02 00:01:44 +0000793static void handleIBOutletCollection(Sema &S, Decl *D,
794 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000795
796 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000797 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
799 return;
800 }
801
Ted Kremenek2f041d02011-09-29 07:02:25 +0000802 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000803 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000804
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000805 IdentifierInfo *II = Attr.getParameterName();
806 if (!II)
807 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000808
John McCallb3d87482010-08-24 05:47:05 +0000809 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000810 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000811 if (!TypeRep) {
812 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
813 return;
814 }
John McCallb3d87482010-08-24 05:47:05 +0000815 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000816 // Diagnose use of non-object type in iboutletcollection attribute.
817 // FIXME. Gnu attribute extension ignores use of builtin types in
818 // attributes. So, __attribute__((iboutletcollection(char))) will be
819 // treated as __attribute__((iboutletcollection())).
820 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
821 !QT->isObjCObjectType()) {
822 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
823 return;
824 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000825 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
826 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000827}
828
Chandler Carruthd309c812011-07-01 23:49:16 +0000829static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000830 if (const RecordType *UT = T->getAsUnionType())
831 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
832 RecordDecl *UD = UT->getDecl();
833 for (RecordDecl::field_iterator it = UD->field_begin(),
834 itend = UD->field_end(); it != itend; ++it) {
835 QualType QT = it->getType();
836 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
837 T = QT;
838 return;
839 }
840 }
841 }
842}
843
Chandler Carruth1b03c872011-07-02 00:01:44 +0000844static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000845 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
846 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000847 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000848 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000849 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000850 return;
851 }
Mike Stumpbf916502009-07-24 19:02:52 +0000852
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000853 // In C++ the implicit 'this' function parameter also counts, and they are
854 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000855 bool HasImplicitThisParam = isInstanceMethod(D);
856 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000857
858 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000859 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000860
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000861 for (AttributeList::arg_iterator I=Attr.arg_begin(),
862 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000863
864
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000865 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000866 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000867 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000868 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
869 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
871 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000872 return;
873 }
Mike Stumpbf916502009-07-24 19:02:52 +0000874
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000875 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000876
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000877 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000879 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000880 return;
881 }
Mike Stumpbf916502009-07-24 19:02:52 +0000882
Ted Kremenek465172f2008-07-21 22:09:15 +0000883 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000884 if (HasImplicitThisParam) {
885 if (x == 0) {
886 S.Diag(Attr.getLoc(),
887 diag::err_attribute_invalid_implicit_this_argument)
888 << "nonnull" << Ex->getSourceRange();
889 return;
890 }
891 --x;
892 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000893
894 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000895 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000896 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000897
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000898 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000899 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000900 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000901 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000902 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000903 }
Mike Stumpbf916502009-07-24 19:02:52 +0000904
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000905 NonNullArgs.push_back(x);
906 }
Mike Stumpbf916502009-07-24 19:02:52 +0000907
908 // If no arguments were specified to __attribute__((nonnull)) then all pointer
909 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000910 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000911 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
912 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000913 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000914 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000915 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000916 }
Mike Stumpbf916502009-07-24 19:02:52 +0000917
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000918 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000919 if (NonNullArgs.empty()) {
920 // Warn the trivial case only if attribute is not coming from a
921 // macro instantiation.
922 if (Attr.getLoc().isFileID())
923 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000924 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000925 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000926 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000927
928 unsigned* start = &NonNullArgs[0];
929 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000930 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000931 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000932 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000933}
934
Chandler Carruth1b03c872011-07-02 00:01:44 +0000935static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000936 // This attribute must be applied to a function declaration.
937 // The first argument to the attribute must be a string,
938 // the name of the resource, for example "malloc".
939 // The following arguments must be argument indexes, the arguments must be
940 // of integer type for Returns, otherwise of pointer type.
941 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000942 // after being held. free() should be __attribute((ownership_takes)), whereas
943 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000944
945 if (!AL.getParameterName()) {
946 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
947 << AL.getName()->getName() << 1;
948 return;
949 }
950 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000951 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000952 switch (AL.getKind()) {
953 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000954 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000955 if (AL.getNumArgs() < 1) {
956 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
957 return;
958 }
Jordy Rose2a479922010-08-12 08:54:03 +0000959 break;
960 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000961 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000962 if (AL.getNumArgs() < 1) {
963 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
964 return;
965 }
Jordy Rose2a479922010-08-12 08:54:03 +0000966 break;
967 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000968 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000969 if (AL.getNumArgs() > 1) {
970 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
971 << AL.getNumArgs() + 1;
972 return;
973 }
Jordy Rose2a479922010-08-12 08:54:03 +0000974 break;
975 default:
976 // This should never happen given how we are called.
977 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000978 }
979
Chandler Carruth87c44602011-07-01 23:49:12 +0000980 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000981 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
982 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000983 return;
984 }
985
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000986 // In C++ the implicit 'this' function parameter also counts, and they are
987 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000988 bool HasImplicitThisParam = isInstanceMethod(D);
989 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000990
Chris Lattner5f9e2722011-07-23 10:55:15 +0000991 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000992
993 // Normalize the argument, __foo__ becomes foo.
994 if (Module.startswith("__") && Module.endswith("__"))
995 Module = Module.substr(2, Module.size() - 4);
996
Chris Lattner5f9e2722011-07-23 10:55:15 +0000997 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000998
Jordy Rose2a479922010-08-12 08:54:03 +0000999 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1000 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001001
Peter Collingbourne7a730022010-11-23 20:45:58 +00001002 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001003 llvm::APSInt ArgNum(32);
1004 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1005 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1006 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1007 << AL.getName()->getName() << IdxExpr->getSourceRange();
1008 continue;
1009 }
1010
1011 unsigned x = (unsigned) ArgNum.getZExtValue();
1012
1013 if (x > NumArgs || x < 1) {
1014 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1015 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1016 continue;
1017 }
1018 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001019 if (HasImplicitThisParam) {
1020 if (x == 0) {
1021 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1022 << "ownership" << IdxExpr->getSourceRange();
1023 return;
1024 }
1025 --x;
1026 }
1027
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001028 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001029 case OwnershipAttr::Takes:
1030 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001031 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001032 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001033 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1034 // FIXME: Should also highlight argument in decl.
1035 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001036 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001037 << "pointer"
1038 << IdxExpr->getSourceRange();
1039 continue;
1040 }
1041 break;
1042 }
Sean Huntcf807c42010-08-18 23:23:40 +00001043 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001044 if (AL.getNumArgs() > 1) {
1045 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001046 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001047 llvm::APSInt ArgNum(32);
1048 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1049 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1050 S.Diag(AL.getLoc(), diag::err_ownership_type)
1051 << "ownership_returns" << "integer"
1052 << IdxExpr->getSourceRange();
1053 return;
1054 }
1055 }
1056 break;
1057 }
Jordy Rose2a479922010-08-12 08:54:03 +00001058 default:
1059 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001060 } // switch
1061
1062 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001063 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001064 i = D->specific_attr_begin<OwnershipAttr>(),
1065 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001066 i != e; ++i) {
1067 if ((*i)->getOwnKind() != K) {
1068 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1069 I!=E; ++I) {
1070 if (x == *I) {
1071 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1072 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001073 }
1074 }
1075 }
1076 }
1077 OwnershipArgs.push_back(x);
1078 }
1079
1080 unsigned* start = OwnershipArgs.data();
1081 unsigned size = OwnershipArgs.size();
1082 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001083
1084 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1085 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1086 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001087 }
Sean Huntcf807c42010-08-18 23:23:40 +00001088
Chandler Carruth87c44602011-07-01 23:49:12 +00001089 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001090 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001091}
1092
John McCall332bb2a2011-02-08 22:35:49 +00001093/// Whether this declaration has internal linkage for the purposes of
1094/// things that want to complain about things not have internal linkage.
1095static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1096 switch (D->getLinkage()) {
1097 case NoLinkage:
1098 case InternalLinkage:
1099 return true;
1100
1101 // Template instantiations that go from external to unique-external
1102 // shouldn't get diagnosed.
1103 case UniqueExternalLinkage:
1104 return true;
1105
1106 case ExternalLinkage:
1107 return false;
1108 }
1109 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001110 return false;
1111}
1112
Chandler Carruth1b03c872011-07-02 00:01:44 +00001113static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001114 // Check the attribute arguments.
1115 if (Attr.getNumArgs() > 1) {
1116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1117 return;
1118 }
1119
Chandler Carruth87c44602011-07-01 23:49:12 +00001120 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001122 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001123 return;
1124 }
1125
Chandler Carruth87c44602011-07-01 23:49:12 +00001126 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001127
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001128 // gcc rejects
1129 // class c {
1130 // static int a __attribute__((weakref ("v2")));
1131 // static int b() __attribute__((weakref ("f3")));
1132 // };
1133 // and ignores the attributes of
1134 // void f(void) {
1135 // static int a __attribute__((weakref ("v2")));
1136 // }
1137 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001138 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001139 if (!Ctx->isFileContext()) {
1140 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001141 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001142 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001143 }
1144
1145 // The GCC manual says
1146 //
1147 // At present, a declaration to which `weakref' is attached can only
1148 // be `static'.
1149 //
1150 // It also says
1151 //
1152 // Without a TARGET,
1153 // given as an argument to `weakref' or to `alias', `weakref' is
1154 // equivalent to `weak'.
1155 //
1156 // gcc 4.4.1 will accept
1157 // int a7 __attribute__((weakref));
1158 // as
1159 // int a7 __attribute__((weak));
1160 // This looks like a bug in gcc. We reject that for now. We should revisit
1161 // it if this behaviour is actually used.
1162
John McCall332bb2a2011-02-08 22:35:49 +00001163 if (!hasEffectivelyInternalLinkage(nd)) {
1164 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001165 return;
1166 }
1167
1168 // GCC rejects
1169 // static ((alias ("y"), weakref)).
1170 // Should we? How to check that weakref is before or after alias?
1171
1172 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001173 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001174 Arg = Arg->IgnoreParenCasts();
1175 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1176
Douglas Gregor5cee1192011-07-27 05:40:30 +00001177 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001178 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1179 << "weakref" << 1;
1180 return;
1181 }
1182 // GCC will accept anything as the argument of weakref. Should we
1183 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001184 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001185 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001186 }
1187
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001188 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001189}
1190
Chandler Carruth1b03c872011-07-02 00:01:44 +00001191static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001192 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001193 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001194 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001195 return;
1196 }
Mike Stumpbf916502009-07-24 19:02:52 +00001197
Peter Collingbourne7a730022010-11-23 20:45:58 +00001198 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001199 Arg = Arg->IgnoreParenCasts();
1200 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001201
Douglas Gregor5cee1192011-07-27 05:40:30 +00001202 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001203 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001204 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001205 return;
1206 }
Mike Stumpbf916502009-07-24 19:02:52 +00001207
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001208 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001209 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1210 return;
1211 }
1212
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001214
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001215 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001216 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001217}
1218
Chandler Carruth1b03c872011-07-02 00:01:44 +00001219static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001220 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001221 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001222 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001223
Chandler Carruth87c44602011-07-01 23:49:12 +00001224 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001226 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001227 return;
1228 }
1229
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001230 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001231}
1232
Chandler Carruth1b03c872011-07-02 00:01:44 +00001233static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1234 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001235 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001236 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001237 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1238 return;
1239 }
1240
Chandler Carruth87c44602011-07-01 23:49:12 +00001241 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001242 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001243 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001244 return;
1245 }
Mike Stumpbf916502009-07-24 19:02:52 +00001246
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001247 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001248}
1249
Chandler Carruth1b03c872011-07-02 00:01:44 +00001250static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001251 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001252 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1254 return;
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Chandler Carruth87c44602011-07-01 23:49:12 +00001257 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001258 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001259 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001260 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001261 return;
1262 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001263 }
1264
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001265 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001266}
1267
Chandler Carruth1b03c872011-07-02 00:01:44 +00001268static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001269 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001270 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001271 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001272
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001273 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001274}
1275
Chandler Carruth1b03c872011-07-02 00:01:44 +00001276static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001277 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001278 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001279 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001280 else
1281 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001282 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001283}
1284
Chandler Carruth1b03c872011-07-02 00:01:44 +00001285static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001286 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001287 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001288 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001289 else
1290 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001291 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001292}
1293
Chandler Carruth1b03c872011-07-02 00:01:44 +00001294static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001295 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001296
1297 if (S.CheckNoReturnAttr(attr)) return;
1298
Chandler Carruth87c44602011-07-01 23:49:12 +00001299 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001300 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001301 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001302 return;
1303 }
1304
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001305 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001306}
1307
1308bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001309 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001310 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1311 attr.setInvalid();
1312 return true;
1313 }
1314
1315 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001316}
1317
Chandler Carruth1b03c872011-07-02 00:01:44 +00001318static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1319 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001320
1321 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1322 // because 'analyzer_noreturn' does not impact the type.
1323
Chandler Carruth1731e202011-07-11 23:30:35 +00001324 if(!checkAttributeNumArgs(S, Attr, 0))
1325 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001326
Chandler Carruth87c44602011-07-01 23:49:12 +00001327 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1328 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001329 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1330 && !VD->getType()->isFunctionPointerType())) {
1331 S.Diag(Attr.getLoc(),
1332 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1333 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001334 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001335 return;
1336 }
1337 }
1338
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001339 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001340}
1341
John Thompson35cc9622010-08-09 21:53:52 +00001342// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001343static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001344/*
1345 Returning a Vector Class in Registers
1346
Eric Christopherf48f3672010-12-01 22:13:54 +00001347 According to the PPU ABI specifications, a class with a single member of
1348 vector type is returned in memory when used as the return value of a function.
1349 This results in inefficient code when implementing vector classes. To return
1350 the value in a single vector register, add the vecreturn attribute to the
1351 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001352
1353 Example:
1354
1355 struct Vector
1356 {
1357 __vector float xyzw;
1358 } __attribute__((vecreturn));
1359
1360 Vector Add(Vector lhs, Vector rhs)
1361 {
1362 Vector result;
1363 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1364 return result; // This will be returned in a register
1365 }
1366*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001367 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001368 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001369 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001370 return;
1371 }
1372
Chandler Carruth87c44602011-07-01 23:49:12 +00001373 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001374 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1375 return;
1376 }
1377
Chandler Carruth87c44602011-07-01 23:49:12 +00001378 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001379 int count = 0;
1380
1381 if (!isa<CXXRecordDecl>(record)) {
1382 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1383 return;
1384 }
1385
1386 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1387 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1388 return;
1389 }
1390
Eric Christopherf48f3672010-12-01 22:13:54 +00001391 for (RecordDecl::field_iterator iter = record->field_begin();
1392 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001393 if ((count == 1) || !iter->getType()->isVectorType()) {
1394 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1395 return;
1396 }
1397 count++;
1398 }
1399
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001400 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001401}
1402
Chandler Carruth1b03c872011-07-02 00:01:44 +00001403static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001404 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001406 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001407 return;
1408 }
1409 // FIXME: Actually store the attribute on the declaration
1410}
1411
Chandler Carruth1b03c872011-07-02 00:01:44 +00001412static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001413 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001414 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001416 return;
1417 }
Mike Stumpbf916502009-07-24 19:02:52 +00001418
Chandler Carruth87c44602011-07-01 23:49:12 +00001419 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1420 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001422 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001423 return;
1424 }
Mike Stumpbf916502009-07-24 19:02:52 +00001425
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001426 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001427}
1428
Chandler Carruth1b03c872011-07-02 00:01:44 +00001429static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001430 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001431 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1433 return;
1434 }
Mike Stumpbf916502009-07-24 19:02:52 +00001435
Chandler Carruth87c44602011-07-01 23:49:12 +00001436 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001437 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001438 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1439 return;
1440 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001441 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001443 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001444 return;
1445 }
Mike Stumpbf916502009-07-24 19:02:52 +00001446
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001447 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001448}
1449
Chandler Carruth1b03c872011-07-02 00:01:44 +00001450static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001451 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001452 if (Attr.getNumArgs() > 1) {
1453 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001454 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001455 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001456
1457 int priority = 65535; // FIXME: Do not hardcode such constants.
1458 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001459 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001460 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001461 if (E->isTypeDependent() || E->isValueDependent() ||
1462 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001464 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001465 return;
1466 }
1467 priority = Idx.getZExtValue();
1468 }
Mike Stumpbf916502009-07-24 19:02:52 +00001469
Chandler Carruth87c44602011-07-01 23:49:12 +00001470 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001472 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001473 return;
1474 }
1475
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001476 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001477 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001478}
1479
Chandler Carruth1b03c872011-07-02 00:01:44 +00001480static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001481 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001482 if (Attr.getNumArgs() > 1) {
1483 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001484 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001485 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001486
1487 int priority = 65535; // FIXME: Do not hardcode such constants.
1488 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001489 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001490 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001491 if (E->isTypeDependent() || E->isValueDependent() ||
1492 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001493 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001494 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001495 return;
1496 }
1497 priority = Idx.getZExtValue();
1498 }
Mike Stumpbf916502009-07-24 19:02:52 +00001499
Chandler Carruth87c44602011-07-01 23:49:12 +00001500 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001502 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001503 return;
1504 }
1505
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001506 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001507 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001508}
1509
Chandler Carruth1b03c872011-07-02 00:01:44 +00001510static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001511 unsigned NumArgs = Attr.getNumArgs();
1512 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001514 return;
1515 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001516
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001517 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001518 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001519 if (NumArgs == 1) {
1520 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001521 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001522 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1523 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001524 return;
1525 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001526 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001527 }
Mike Stumpbf916502009-07-24 19:02:52 +00001528
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001529 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001530}
1531
Chandler Carruth1b03c872011-07-02 00:01:44 +00001532static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001533 unsigned NumArgs = Attr.getNumArgs();
1534 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001535 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001536 return;
1537 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001538
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001539 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001540 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001541 if (NumArgs == 1) {
1542 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001543 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001544 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001545 diag::err_attribute_not_string) << "unavailable";
1546 return;
1547 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001548 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001549 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001550 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001551}
1552
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001553static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1554 const AttributeList &Attr) {
1555 unsigned NumArgs = Attr.getNumArgs();
1556 if (NumArgs > 0) {
1557 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1558 return;
1559 }
1560
1561 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001562 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001563}
1564
Chandler Carruth1b03c872011-07-02 00:01:44 +00001565static void handleAvailabilityAttr(Sema &S, Decl *D,
1566 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001567 IdentifierInfo *Platform = Attr.getParameterName();
1568 SourceLocation PlatformLoc = Attr.getParameterLoc();
1569
Chris Lattner5f9e2722011-07-23 10:55:15 +00001570 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001571 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1572 if (PlatformName.empty()) {
1573 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1574 << Platform;
1575
1576 PlatformName = Platform->getName();
1577 }
1578
1579 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1580 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1581 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001582 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001583
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001584 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001585 // of these steps are needed).
1586 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001587 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001588 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1589 << 1 << PlatformName << Deprecated.Version.getAsString()
1590 << 0 << Introduced.Version.getAsString();
1591 return;
1592 }
1593
1594 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001595 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001596 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1597 << 2 << PlatformName << Obsoleted.Version.getAsString()
1598 << 0 << Introduced.Version.getAsString();
1599 return;
1600 }
1601
1602 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001603 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001604 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1605 << 2 << PlatformName << Obsoleted.Version.getAsString()
1606 << 1 << Deprecated.Version.getAsString();
1607 return;
1608 }
1609
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001610 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001611 Platform,
1612 Introduced.Version,
1613 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001614 Obsoleted.Version,
1615 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001616}
1617
Chandler Carruth1b03c872011-07-02 00:01:44 +00001618static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001619 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001620 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001621 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001622
Peter Collingbourne7a730022010-11-23 20:45:58 +00001623 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001624 Arg = Arg->IgnoreParenCasts();
1625 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001626
Douglas Gregor5cee1192011-07-27 05:40:30 +00001627 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001628 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001629 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001630 return;
1631 }
Mike Stumpbf916502009-07-24 19:02:52 +00001632
Chris Lattner5f9e2722011-07-23 10:55:15 +00001633 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001634 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001635
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001636 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001637 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001638 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001639 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001640 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001641 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001642 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001643 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001644 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001645 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001646 return;
1647 }
Mike Stumpbf916502009-07-24 19:02:52 +00001648
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001649 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001650}
1651
Chandler Carruth1b03c872011-07-02 00:01:44 +00001652static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1653 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001654 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1655 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001657 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001658 return;
1659 }
1660
Chandler Carruth87c44602011-07-01 23:49:12 +00001661 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1662 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1663 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001664 << "objc_method_family" << 1;
1665 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001667 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001668 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001669 return;
1670 }
1671
Chris Lattner5f9e2722011-07-23 10:55:15 +00001672 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001673 ObjCMethodFamilyAttr::FamilyKind family;
1674 if (param == "none")
1675 family = ObjCMethodFamilyAttr::OMF_None;
1676 else if (param == "alloc")
1677 family = ObjCMethodFamilyAttr::OMF_alloc;
1678 else if (param == "copy")
1679 family = ObjCMethodFamilyAttr::OMF_copy;
1680 else if (param == "init")
1681 family = ObjCMethodFamilyAttr::OMF_init;
1682 else if (param == "mutableCopy")
1683 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1684 else if (param == "new")
1685 family = ObjCMethodFamilyAttr::OMF_new;
1686 else {
1687 // Just warn and ignore it. This is future-proof against new
1688 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001689 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001690 return;
1691 }
1692
John McCallf85e1932011-06-15 23:02:42 +00001693 if (family == ObjCMethodFamilyAttr::OMF_init &&
1694 !method->getResultType()->isObjCObjectPointerType()) {
1695 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1696 << method->getResultType();
1697 // Ignore the attribute.
1698 return;
1699 }
1700
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001701 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001702 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001703}
1704
Chandler Carruth1b03c872011-07-02 00:01:44 +00001705static void handleObjCExceptionAttr(Sema &S, Decl *D,
1706 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001707 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001708 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001709
Chris Lattner0db29ec2009-02-14 08:09:34 +00001710 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1711 if (OCI == 0) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1713 return;
1714 }
Mike Stumpbf916502009-07-24 19:02:52 +00001715
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001716 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001717}
1718
Chandler Carruth1b03c872011-07-02 00:01:44 +00001719static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001720 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001722 return;
1723 }
Richard Smith162e1c12011-04-15 14:24:37 +00001724 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001725 QualType T = TD->getUnderlyingType();
1726 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001727 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001728 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1729 return;
1730 }
1731 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001732 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001733}
1734
Mike Stumpbf916502009-07-24 19:02:52 +00001735static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001737 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001738 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001739 return;
1740 }
1741
1742 if (!isa<FunctionDecl>(D)) {
1743 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1744 return;
1745 }
1746
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001747 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001748}
1749
Chandler Carruth1b03c872011-07-02 00:01:44 +00001750static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001751 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001752 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001753 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001754 return;
1755 }
Mike Stumpbf916502009-07-24 19:02:52 +00001756
Steve Naroff9eae5762008-09-18 16:44:58 +00001757 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001759 return;
1760 }
Mike Stumpbf916502009-07-24 19:02:52 +00001761
Sean Huntcf807c42010-08-18 23:23:40 +00001762 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001763 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001764 type = BlocksAttr::ByRef;
1765 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001766 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001767 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001768 return;
1769 }
Mike Stumpbf916502009-07-24 19:02:52 +00001770
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001771 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001772}
1773
Chandler Carruth1b03c872011-07-02 00:01:44 +00001774static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001775 // check the attribute arguments.
1776 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001777 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001778 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001779 }
1780
John McCall3323fad2011-09-09 07:56:05 +00001781 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001782 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001783 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001784 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001785 if (E->isTypeDependent() || E->isValueDependent() ||
1786 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001787 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001788 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001789 return;
1790 }
Mike Stumpbf916502009-07-24 19:02:52 +00001791
John McCall3323fad2011-09-09 07:56:05 +00001792 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001793 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1794 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001795 return;
1796 }
John McCall3323fad2011-09-09 07:56:05 +00001797
1798 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001799 }
1800
John McCall3323fad2011-09-09 07:56:05 +00001801 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001802 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001803 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001804 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001805 if (E->isTypeDependent() || E->isValueDependent() ||
1806 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001807 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001808 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001809 return;
1810 }
1811 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001812
John McCall3323fad2011-09-09 07:56:05 +00001813 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001814 // FIXME: This error message could be improved, it would be nice
1815 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001816 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1817 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001818 return;
1819 }
1820 }
1821
Chandler Carruth87c44602011-07-01 23:49:12 +00001822 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001823 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001824 if (isa<FunctionNoProtoType>(FT)) {
1825 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1826 return;
1827 }
Mike Stumpbf916502009-07-24 19:02:52 +00001828
Chris Lattner897cd902009-03-17 23:03:47 +00001829 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001830 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001831 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001832 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001833 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001834 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001835 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001836 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001837 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001838 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001839 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1840 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001841 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001842 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001843 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001844 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001845 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001846 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001847 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001848 int m = Ty->isFunctionPointerType() ? 0 : 1;
1849 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001850 return;
1851 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001852 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001854 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001855 return;
1856 }
Anders Carlsson77091822008-10-05 18:05:59 +00001857 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001859 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001860 return;
1861 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001862 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001863 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001864}
1865
Chandler Carruth1b03c872011-07-02 00:01:44 +00001866static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001867 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001868 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001869 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001870
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001871 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001873 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001874 return;
1875 }
Mike Stumpbf916502009-07-24 19:02:52 +00001876
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001877 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1878 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1879 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001880 return;
1881 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001882 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1883 if (MD->getResultType()->isVoidType()) {
1884 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1885 << Attr.getName() << 1;
1886 return;
1887 }
1888
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001889 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001890}
1891
Chandler Carruth1b03c872011-07-02 00:01:44 +00001892static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001893 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001894 if (Attr.hasParameterOrArguments()) {
1895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001896 return;
1897 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001898
Chandler Carruth87c44602011-07-01 23:49:12 +00001899 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1901 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001902 return;
1903 }
1904
Chandler Carruth87c44602011-07-01 23:49:12 +00001905 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001906
1907 // 'weak' only applies to declarations with external linkage.
1908 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001909 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001910 return;
1911 }
Mike Stumpbf916502009-07-24 19:02:52 +00001912
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001913 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001914}
1915
Chandler Carruth1b03c872011-07-02 00:01:44 +00001916static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001917 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001918 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001919 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001920
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001921
1922 // weak_import only applies to variable & function declarations.
1923 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001924 if (!D->canBeWeakImported(isDef)) {
1925 if (isDef)
1926 S.Diag(Attr.getLoc(),
1927 diag::warn_attribute_weak_import_invalid_on_definition)
1928 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001929 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001930 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001931 isa<ObjCInterfaceDecl>(D))) {
1932 // Nothing to warn about here.
1933 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001935 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001936
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001937 return;
1938 }
1939
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001940 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001941}
1942
Chandler Carruth1b03c872011-07-02 00:01:44 +00001943static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1944 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001945 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001946 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001947 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001948
1949 unsigned WGSize[3];
1950 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001951 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001952 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001953 if (E->isTypeDependent() || E->isValueDependent() ||
1954 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001955 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1956 << "reqd_work_group_size" << E->getSourceRange();
1957 return;
1958 }
1959 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1960 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001961 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00001962 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001963 WGSize[2]));
1964}
1965
Chandler Carruth1b03c872011-07-02 00:01:44 +00001966static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001967 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001968 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001969 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001970
1971 // Make sure that there is a string literal as the sections's single
1972 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001973 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001974 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001975 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001976 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001977 return;
1978 }
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Chris Lattner797c3c42009-08-10 19:03:04 +00001980 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001981 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001982 if (!Error.empty()) {
1983 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1984 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001985 return;
1986 }
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001988 // This attribute cannot be applied to local variables.
1989 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1990 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1991 return;
1992 }
1993
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001994 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001995 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001996}
1997
Chris Lattner6b6b5372008-06-26 18:38:35 +00001998
Chandler Carruth1b03c872011-07-02 00:01:44 +00001999static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002000 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002001 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002002 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002003 return;
2004 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002005
Chandler Carruth87c44602011-07-01 23:49:12 +00002006 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002007 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002008 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002009 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002010 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002011 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002012}
2013
Chandler Carruth1b03c872011-07-02 00:01:44 +00002014static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002015 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002016 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002017 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002018 return;
2019 }
Mike Stumpbf916502009-07-24 19:02:52 +00002020
Chandler Carruth87c44602011-07-01 23:49:12 +00002021 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002022 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002023 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002024 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002025 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002026 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002027}
2028
Chandler Carruth1b03c872011-07-02 00:01:44 +00002029static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002030 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002031 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002032 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002033
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002034 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002035}
2036
Chandler Carruth1b03c872011-07-02 00:01:44 +00002037static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002038 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2040 return;
2041 }
Mike Stumpbf916502009-07-24 19:02:52 +00002042
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002043 if (Attr.getNumArgs() != 0) {
2044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2045 return;
2046 }
Mike Stumpbf916502009-07-24 19:02:52 +00002047
Chandler Carruth87c44602011-07-01 23:49:12 +00002048 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002049
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002050 if (!VD || !VD->hasLocalStorage()) {
2051 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2052 return;
2053 }
Mike Stumpbf916502009-07-24 19:02:52 +00002054
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002055 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002056 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002057 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002058 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2059 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002060 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002061 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002062 Attr.getParameterName();
2063 return;
2064 }
Mike Stumpbf916502009-07-24 19:02:52 +00002065
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002066 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2067 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002068 S.Diag(Attr.getParameterLoc(),
2069 diag::err_attribute_cleanup_arg_not_function)
2070 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002071 return;
2072 }
2073
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002074 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002075 S.Diag(Attr.getParameterLoc(),
2076 diag::err_attribute_cleanup_func_must_take_one_arg)
2077 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002078 return;
2079 }
Mike Stumpbf916502009-07-24 19:02:52 +00002080
Anders Carlsson89941c12009-02-07 23:16:50 +00002081 // We're currently more strict than GCC about what function types we accept.
2082 // If this ever proves to be a problem it should be easy to fix.
2083 QualType Ty = S.Context.getPointerType(VD->getType());
2084 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002085 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2086 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002087 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002088 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2089 Attr.getParameterName() << ParamTy << Ty;
2090 return;
2091 }
Mike Stumpbf916502009-07-24 19:02:52 +00002092
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002093 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00002094 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002095}
2096
Mike Stumpbf916502009-07-24 19:02:52 +00002097/// Handle __attribute__((format_arg((idx)))) attribute based on
2098/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002099static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002100 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002101 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002102
Chandler Carruth87c44602011-07-01 23:49:12 +00002103 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002104 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002105 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002106 return;
2107 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002108
2109 // In C++ the implicit 'this' function parameter also counts, and they are
2110 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002111 bool HasImplicitThisParam = isInstanceMethod(D);
2112 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002113 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002114
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002115 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002116 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002117 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002118 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2119 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2121 << "format" << 2 << IdxExpr->getSourceRange();
2122 return;
2123 }
Mike Stumpbf916502009-07-24 19:02:52 +00002124
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002125 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2126 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2127 << "format" << 2 << IdxExpr->getSourceRange();
2128 return;
2129 }
Mike Stumpbf916502009-07-24 19:02:52 +00002130
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002131 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002132
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002133 if (HasImplicitThisParam) {
2134 if (ArgIdx == 0) {
2135 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2136 << "format_arg" << IdxExpr->getSourceRange();
2137 return;
2138 }
2139 ArgIdx--;
2140 }
2141
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002142 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002143 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002144
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002145 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2146 if (not_nsstring_type &&
2147 !isCFStringType(Ty, S.Context) &&
2148 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002149 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002150 // FIXME: Should highlight the actual expression that has the wrong type.
2151 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002152 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002153 << IdxExpr->getSourceRange();
2154 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002155 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002156 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002157 if (!isNSStringType(Ty, S.Context) &&
2158 !isCFStringType(Ty, S.Context) &&
2159 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002160 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002161 // FIXME: Should highlight the actual expression that has the wrong type.
2162 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002163 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002164 << IdxExpr->getSourceRange();
2165 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002166 }
2167
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002168 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002169 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002170}
2171
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002172enum FormatAttrKind {
2173 CFStringFormat,
2174 NSStringFormat,
2175 StrftimeFormat,
2176 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002177 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002178 InvalidFormat
2179};
2180
2181/// getFormatAttrKind - Map from format attribute names to supported format
2182/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002183static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002184 // Check for formats that get handled specially.
2185 if (Format == "NSString")
2186 return NSStringFormat;
2187 if (Format == "CFString")
2188 return CFStringFormat;
2189 if (Format == "strftime")
2190 return StrftimeFormat;
2191
2192 // Otherwise, check for supported formats.
2193 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2194 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
2195 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002196 Format == "zcmn_err" ||
2197 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002198 return SupportedFormat;
2199
Duncan Sandsbc525952010-03-23 14:44:19 +00002200 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2201 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002202 return IgnoredFormat;
2203
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002204 return InvalidFormat;
2205}
2206
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002207/// Handle __attribute__((init_priority(priority))) attributes based on
2208/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002209static void handleInitPriorityAttr(Sema &S, Decl *D,
2210 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002211 if (!S.getLangOptions().CPlusPlus) {
2212 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2213 return;
2214 }
2215
Chandler Carruth87c44602011-07-01 23:49:12 +00002216 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002217 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2218 Attr.setInvalid();
2219 return;
2220 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002221 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002222 if (S.Context.getAsArrayType(T))
2223 T = S.Context.getBaseElementType(T);
2224 if (!T->getAs<RecordType>()) {
2225 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2226 Attr.setInvalid();
2227 return;
2228 }
2229
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002230 if (Attr.getNumArgs() != 1) {
2231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2232 Attr.setInvalid();
2233 return;
2234 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002235 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002236
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002237 llvm::APSInt priority(32);
2238 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2239 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2241 << "init_priority" << priorityExpr->getSourceRange();
2242 Attr.setInvalid();
2243 return;
2244 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002245 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002246 if (prioritynum < 101 || prioritynum > 65535) {
2247 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2248 << priorityExpr->getSourceRange();
2249 Attr.setInvalid();
2250 return;
2251 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002252 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002253 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002254}
2255
Mike Stumpbf916502009-07-24 19:02:52 +00002256/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2257/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002258static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002259
Chris Lattner545dd342008-06-28 23:36:30 +00002260 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002262 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002263 return;
2264 }
2265
Chris Lattner545dd342008-06-28 23:36:30 +00002266 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002268 return;
2269 }
2270
Chandler Carruth87c44602011-07-01 23:49:12 +00002271 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002272 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002273 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002274 return;
2275 }
2276
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002277 // In C++ the implicit 'this' function parameter also counts, and they are
2278 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002279 bool HasImplicitThisParam = isInstanceMethod(D);
2280 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002281 unsigned FirstIdx = 1;
2282
Chris Lattner5f9e2722011-07-23 10:55:15 +00002283 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002284
2285 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002286 if (Format.startswith("__") && Format.endswith("__"))
2287 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002288
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002289 // Check for supported formats.
2290 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002291
2292 if (Kind == IgnoredFormat)
2293 return;
2294
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002295 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002296 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002297 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002298 return;
2299 }
2300
2301 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002302 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002303 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002304 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2305 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002306 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002307 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002308 return;
2309 }
2310
2311 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002313 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002314 return;
2315 }
2316
2317 // FIXME: Do we need to bounds check?
2318 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002319
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002320 if (HasImplicitThisParam) {
2321 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002322 S.Diag(Attr.getLoc(),
2323 diag::err_format_attribute_implicit_this_format_string)
2324 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002325 return;
2326 }
2327 ArgIdx--;
2328 }
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Chris Lattner6b6b5372008-06-26 18:38:35 +00002330 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002331 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002332
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002333 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002334 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002335 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2336 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002337 return;
2338 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002339 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002340 // FIXME: do we need to check if the type is NSString*? What are the
2341 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002342 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002343 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002344 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2345 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002346 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002347 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002348 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002349 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002350 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002351 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2352 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002353 return;
2354 }
2355
2356 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002357 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002358 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002359 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2360 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002361 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002362 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002363 return;
2364 }
2365
2366 // check if the function is variadic if the 3rd argument non-zero
2367 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002368 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002369 ++NumArgs; // +1 for ...
2370 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002371 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002372 return;
2373 }
2374 }
2375
Chris Lattner3c73c412008-11-19 08:23:25 +00002376 // strftime requires FirstArg to be 0 because it doesn't read from any
2377 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002378 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002379 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002380 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2381 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002382 return;
2383 }
2384 // if 0 it disables parameter checking (to use with e.g. va_list)
2385 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002387 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002388 return;
2389 }
2390
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002391 // Check whether we already have an equivalent format attribute.
2392 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002393 i = D->specific_attr_begin<FormatAttr>(),
2394 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002395 i != e ; ++i) {
2396 FormatAttr *f = *i;
2397 if (f->getType() == Format &&
2398 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2399 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2400 // If we don't have a valid location for this attribute, adopt the
2401 // location.
2402 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002403 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002404 return;
2405 }
2406 }
2407
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002408 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002409 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002410 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002411}
2412
Chandler Carruth1b03c872011-07-02 00:01:44 +00002413static void handleTransparentUnionAttr(Sema &S, Decl *D,
2414 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002415 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002416 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002417 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002418
Chris Lattner6b6b5372008-06-26 18:38:35 +00002419
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002420 // Try to find the underlying union declaration.
2421 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002422 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002423 if (TD && TD->getUnderlyingType()->isUnionType())
2424 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2425 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002426 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002427
2428 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002430 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002431 return;
2432 }
2433
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002434 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002435 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002436 diag::warn_transparent_union_attribute_not_definition);
2437 return;
2438 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002439
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002440 RecordDecl::field_iterator Field = RD->field_begin(),
2441 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002442 if (Field == FieldEnd) {
2443 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2444 return;
2445 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002446
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002447 FieldDecl *FirstField = *Field;
2448 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002449 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002450 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002451 diag::warn_transparent_union_attribute_floating)
2452 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002453 return;
2454 }
2455
2456 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2457 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2458 for (; Field != FieldEnd; ++Field) {
2459 QualType FieldType = Field->getType();
2460 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2461 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2462 // Warn if we drop the attribute.
2463 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002464 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002465 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002466 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002467 diag::warn_transparent_union_attribute_field_size_align)
2468 << isSize << Field->getDeclName() << FieldBits;
2469 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002470 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002471 diag::note_transparent_union_first_field_size_align)
2472 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002473 return;
2474 }
2475 }
2476
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002477 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002478}
2479
Chandler Carruth1b03c872011-07-02 00:01:44 +00002480static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002481 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002482 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002483 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002484
Peter Collingbourne7a730022010-11-23 20:45:58 +00002485 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002486 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002487
Chris Lattner6b6b5372008-06-26 18:38:35 +00002488 // Make sure that there is a string literal as the annotation's single
2489 // argument.
2490 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002491 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002492 return;
2493 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002494
2495 // Don't duplicate annotations that are already set.
2496 for (specific_attr_iterator<AnnotateAttr>
2497 i = D->specific_attr_begin<AnnotateAttr>(),
2498 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2499 if ((*i)->getAnnotation() == SE->getString())
2500 return;
2501 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002502 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002503 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002504}
2505
Chandler Carruth1b03c872011-07-02 00:01:44 +00002506static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002507 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002508 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002509 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002510 return;
2511 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002512
2513 //FIXME: The C++0x version of this attribute has more limited applicabilty
2514 // than GNU's, and should error out when it is used to specify a
2515 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002516
Chris Lattner545dd342008-06-28 23:36:30 +00002517 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002518 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002519 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002520 }
Mike Stumpbf916502009-07-24 19:02:52 +00002521
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002522 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002523}
2524
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002525void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002526 if (E->isTypeDependent() || E->isValueDependent()) {
2527 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002528 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002529 return;
2530 }
2531
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002532 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002533 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002534 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002535 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2536 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2537 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002538 return;
2539 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002540 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002541 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2542 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002543 return;
2544 }
2545
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002546 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Sean Huntcf807c42010-08-18 23:23:40 +00002547}
2548
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002549void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002550 // FIXME: Cache the number on the Attr object if non-dependent?
2551 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002552 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002553 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002554}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002555
Chandler Carruthd309c812011-07-01 23:49:16 +00002556/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002557/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002558///
Mike Stumpbf916502009-07-24 19:02:52 +00002559/// Despite what would be logical, the mode attribute is a decl attribute, not a
2560/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2561/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002562static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002563 // This attribute isn't documented, but glibc uses it. It changes
2564 // the width of an int or unsigned int to the specified size.
2565
2566 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002567 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002568 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002569
Chris Lattnerfbf13472008-06-27 22:18:37 +00002570
2571 IdentifierInfo *Name = Attr.getParameterName();
2572 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002573 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002574 return;
2575 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002576
Chris Lattner5f9e2722011-07-23 10:55:15 +00002577 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002578
2579 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002580 if (Str.startswith("__") && Str.endswith("__"))
2581 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002582
2583 unsigned DestWidth = 0;
2584 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002585 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002586 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002587 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002588 switch (Str[0]) {
2589 case 'Q': DestWidth = 8; break;
2590 case 'H': DestWidth = 16; break;
2591 case 'S': DestWidth = 32; break;
2592 case 'D': DestWidth = 64; break;
2593 case 'X': DestWidth = 96; break;
2594 case 'T': DestWidth = 128; break;
2595 }
2596 if (Str[1] == 'F') {
2597 IntegerMode = false;
2598 } else if (Str[1] == 'C') {
2599 IntegerMode = false;
2600 ComplexMode = true;
2601 } else if (Str[1] != 'I') {
2602 DestWidth = 0;
2603 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002604 break;
2605 case 4:
2606 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2607 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002608 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002609 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002610 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002611 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002612 break;
2613 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002614 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002615 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002616 break;
2617 }
2618
2619 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002620 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002621 OldTy = TD->getUnderlyingType();
2622 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2623 OldTy = VD->getType();
2624 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002625 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002626 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002627 return;
2628 }
Eli Friedman73397492009-03-03 06:41:03 +00002629
John McCall183700f2009-09-21 23:43:11 +00002630 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002631 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2632 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002633 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002634 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2635 } else if (ComplexMode) {
2636 if (!OldTy->isComplexType())
2637 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2638 } else {
2639 if (!OldTy->isFloatingType())
2640 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2641 }
2642
Mike Stump390b4cc2009-05-16 07:39:55 +00002643 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2644 // and friends, at least with glibc.
2645 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2646 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002647 // FIXME: Make sure floating-point mappings are accurate
2648 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002649 QualType NewTy;
2650 switch (DestWidth) {
2651 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002652 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002653 return;
2654 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002655 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002656 return;
2657 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002658 if (!IntegerMode) {
2659 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2660 return;
2661 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002662 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002663 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002664 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002665 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002666 break;
2667 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002668 if (!IntegerMode) {
2669 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2670 return;
2671 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002672 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002673 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002674 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002675 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002676 break;
2677 case 32:
2678 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002679 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002680 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002681 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002682 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002683 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002684 break;
2685 case 64:
2686 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002687 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002688 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002689 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002690 NewTy = S.Context.LongTy;
2691 else
2692 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002693 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002694 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002695 NewTy = S.Context.UnsignedLongTy;
2696 else
2697 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002698 break;
Eli Friedman73397492009-03-03 06:41:03 +00002699 case 96:
2700 NewTy = S.Context.LongDoubleTy;
2701 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002702 case 128:
2703 if (!IntegerMode) {
2704 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2705 return;
2706 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002707 if (OldTy->isSignedIntegerType())
2708 NewTy = S.Context.Int128Ty;
2709 else
2710 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002711 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002712 }
2713
Eli Friedman73397492009-03-03 06:41:03 +00002714 if (ComplexMode) {
2715 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002716 }
2717
2718 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002719 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002720 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002721 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002722 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002723 cast<ValueDecl>(D)->setType(NewTy);
2724}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002725
Chandler Carruth1b03c872011-07-02 00:01:44 +00002726static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002727 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002728 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002729 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002730
Chandler Carruth87c44602011-07-01 23:49:12 +00002731 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002733 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002734 return;
2735 }
Mike Stumpbf916502009-07-24 19:02:52 +00002736
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002737 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002738}
2739
Chandler Carruth1b03c872011-07-02 00:01:44 +00002740static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002741 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002742 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002743 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002744
Mike Stumpbf916502009-07-24 19:02:52 +00002745
Chandler Carruth87c44602011-07-01 23:49:12 +00002746 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002748 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002749 return;
2750 }
Mike Stumpbf916502009-07-24 19:02:52 +00002751
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002752 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002753}
2754
Chandler Carruth1b03c872011-07-02 00:01:44 +00002755static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2756 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002757 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002758 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002759 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002760
Chris Lattner7255a2d2010-06-22 00:03:40 +00002761
Chandler Carruth87c44602011-07-01 23:49:12 +00002762 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002763 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002764 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002765 return;
2766 }
2767
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002768 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002769 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002770}
2771
Chandler Carruth1b03c872011-07-02 00:01:44 +00002772static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002773 if (S.LangOpts.CUDA) {
2774 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002775 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2777 return;
2778 }
2779
Chandler Carruth87c44602011-07-01 23:49:12 +00002780 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002782 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002783 return;
2784 }
2785
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002786 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002787 } else {
2788 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2789 }
2790}
2791
Chandler Carruth1b03c872011-07-02 00:01:44 +00002792static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002793 if (S.LangOpts.CUDA) {
2794 // check the attribute arguments.
2795 if (Attr.getNumArgs() != 0) {
2796 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2797 return;
2798 }
2799
Chandler Carruth87c44602011-07-01 23:49:12 +00002800 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002802 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002803 return;
2804 }
2805
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002806 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002807 } else {
2808 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2809 }
2810}
2811
Chandler Carruth1b03c872011-07-02 00:01:44 +00002812static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002813 if (S.LangOpts.CUDA) {
2814 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002815 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002816 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002817
Chandler Carruth87c44602011-07-01 23:49:12 +00002818 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002820 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002821 return;
2822 }
2823
Chandler Carruth87c44602011-07-01 23:49:12 +00002824 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002825 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002826 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002827 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2828 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2829 << FD->getType()
2830 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2831 "void");
2832 } else {
2833 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2834 << FD->getType();
2835 }
2836 return;
2837 }
2838
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002839 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002840 } else {
2841 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2842 }
2843}
2844
Chandler Carruth1b03c872011-07-02 00:01:44 +00002845static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002846 if (S.LangOpts.CUDA) {
2847 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002848 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002849 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002850
Peter Collingbourneced76712010-12-01 03:15:31 +00002851
Chandler Carruth87c44602011-07-01 23:49:12 +00002852 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002854 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002855 return;
2856 }
2857
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002858 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002859 } else {
2860 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2861 }
2862}
2863
Chandler Carruth1b03c872011-07-02 00:01:44 +00002864static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002865 if (S.LangOpts.CUDA) {
2866 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002867 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002868 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002869
Peter Collingbourneced76712010-12-01 03:15:31 +00002870
Chandler Carruth87c44602011-07-01 23:49:12 +00002871 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002873 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002874 return;
2875 }
2876
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002877 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002878 } else {
2879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2880 }
2881}
2882
Chandler Carruth1b03c872011-07-02 00:01:44 +00002883static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002884 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002885 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002886 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002887
Chandler Carruth87c44602011-07-01 23:49:12 +00002888 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002889 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002890 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002891 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002892 return;
2893 }
Mike Stumpbf916502009-07-24 19:02:52 +00002894
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002895 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002896 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002897 return;
2898 }
Mike Stumpbf916502009-07-24 19:02:52 +00002899
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002900 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002901}
2902
Chandler Carruth1b03c872011-07-02 00:01:44 +00002903static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002904 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002905
Chandler Carruth87c44602011-07-01 23:49:12 +00002906 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002907 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2908 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002909 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002910 return;
2911
Chandler Carruth87c44602011-07-01 23:49:12 +00002912 if (!isa<ObjCMethodDecl>(D)) {
2913 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2914 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002915 return;
2916 }
2917
Chandler Carruth87c44602011-07-01 23:49:12 +00002918 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002919 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002920 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002921 return;
2922 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002923 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002924 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002925 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002926 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002927 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002928 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002929 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002930 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002931 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002932 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002933 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002934 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002935 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002936 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002937 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002938 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002939 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002940 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002941 return;
2942 }
2943
Chris Lattner5f9e2722011-07-23 10:55:15 +00002944 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002945 PcsAttr::PCSType PCS;
2946 if (StrRef == "aapcs")
2947 PCS = PcsAttr::AAPCS;
2948 else if (StrRef == "aapcs-vfp")
2949 PCS = PcsAttr::AAPCS_VFP;
2950 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002951 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2952 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002953 return;
2954 }
2955
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002956 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002957 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002958 default:
2959 llvm_unreachable("unexpected attribute kind");
2960 return;
2961 }
2962}
2963
Chandler Carruth1b03c872011-07-02 00:01:44 +00002964static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00002965 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002966 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002967}
2968
John McCall711c52b2011-01-05 12:14:39 +00002969bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2970 if (attr.isInvalid())
2971 return true;
2972
Ted Kremenek831efae2011-04-15 05:49:29 +00002973 if ((attr.getNumArgs() != 0 &&
2974 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2975 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002976 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2977 attr.setInvalid();
2978 return true;
2979 }
2980
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002981 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2982 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002983 switch (attr.getKind()) {
2984 case AttributeList::AT_cdecl: CC = CC_C; break;
2985 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2986 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2987 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2988 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002989 case AttributeList::AT_pcs: {
2990 Expr *Arg = attr.getArg(0);
2991 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002992 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002993 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2994 << "pcs" << 1;
2995 attr.setInvalid();
2996 return true;
2997 }
2998
Chris Lattner5f9e2722011-07-23 10:55:15 +00002999 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003000 if (StrRef == "aapcs") {
3001 CC = CC_AAPCS;
3002 break;
3003 } else if (StrRef == "aapcs-vfp") {
3004 CC = CC_AAPCS_VFP;
3005 break;
3006 }
3007 // FALLS THROUGH
3008 }
John McCall711c52b2011-01-05 12:14:39 +00003009 default: llvm_unreachable("unexpected attribute kind"); return true;
3010 }
3011
3012 return false;
3013}
3014
Chandler Carruth1b03c872011-07-02 00:01:44 +00003015static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003016 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003017
3018 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003019 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003020 return;
3021
Chandler Carruth87c44602011-07-01 23:49:12 +00003022 if (!isa<ObjCMethodDecl>(D)) {
3023 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3024 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003025 return;
3026 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003027
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003028 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003029}
3030
3031/// Checks a regparm attribute, returning true if it is ill-formed and
3032/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003033bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3034 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003035 return true;
3036
Chandler Carruth87c44602011-07-01 23:49:12 +00003037 if (Attr.getNumArgs() != 1) {
3038 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3039 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003040 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003041 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003042
Chandler Carruth87c44602011-07-01 23:49:12 +00003043 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003044 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003045 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003046 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003047 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003048 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003049 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003050 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003051 }
3052
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003053 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003054 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003055 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003056 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003057 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003058 }
3059
John McCall711c52b2011-01-05 12:14:39 +00003060 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003061 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003062 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003063 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003064 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003065 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003066 }
3067
John McCall711c52b2011-01-05 12:14:39 +00003068 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003069}
3070
Chandler Carruth1b03c872011-07-02 00:01:44 +00003071static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003072 if (S.LangOpts.CUDA) {
3073 // check the attribute arguments.
3074 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003075 // FIXME: 0 is not okay.
3076 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003077 return;
3078 }
3079
Chandler Carruth87c44602011-07-01 23:49:12 +00003080 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003081 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003082 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003083 return;
3084 }
3085
3086 Expr *MaxThreadsExpr = Attr.getArg(0);
3087 llvm::APSInt MaxThreads(32);
3088 if (MaxThreadsExpr->isTypeDependent() ||
3089 MaxThreadsExpr->isValueDependent() ||
3090 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3091 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3092 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3093 return;
3094 }
3095
3096 llvm::APSInt MinBlocks(32);
3097 if (Attr.getNumArgs() > 1) {
3098 Expr *MinBlocksExpr = Attr.getArg(1);
3099 if (MinBlocksExpr->isTypeDependent() ||
3100 MinBlocksExpr->isValueDependent() ||
3101 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3102 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3103 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3104 return;
3105 }
3106 }
3107
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003108 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003109 MaxThreads.getZExtValue(),
3110 MinBlocks.getZExtValue()));
3111 } else {
3112 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3113 }
3114}
3115
Chris Lattner0744e5f2008-06-29 00:23:49 +00003116//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003117// Checker-specific attribute handlers.
3118//===----------------------------------------------------------------------===//
3119
John McCallc7ad3812011-01-25 03:31:58 +00003120static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
3121 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
3122}
3123static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
3124 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
3125}
3126
Chandler Carruth1b03c872011-07-02 00:01:44 +00003127static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003128 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003129 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003130 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003131 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003132 return;
3133 }
3134
3135 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003136 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003137 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3138 cf = false;
3139 } else {
3140 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3141 cf = true;
3142 }
3143
3144 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003145 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003146 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003147 return;
3148 }
3149
3150 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003151 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003152 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003153 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003154}
3155
Chandler Carruth1b03c872011-07-02 00:01:44 +00003156static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3157 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003158 if (!isa<ObjCMethodDecl>(D)) {
3159 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003160 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003161 return;
3162 }
3163
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003164 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003165}
3166
Chandler Carruth1b03c872011-07-02 00:01:44 +00003167static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3168 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003169
John McCallc7ad3812011-01-25 03:31:58 +00003170 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003171
Chandler Carruth87c44602011-07-01 23:49:12 +00003172 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003173 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003174 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003175 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003176 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
3177 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003178 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003179 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003180 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003181 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003182 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003183 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003184 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003185 return;
3186 }
Mike Stumpbf916502009-07-24 19:02:52 +00003187
John McCallc7ad3812011-01-25 03:31:58 +00003188 bool typeOK;
3189 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003190 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00003191 default: llvm_unreachable("invalid ownership attribute"); return;
3192 case AttributeList::AT_ns_returns_autoreleased:
3193 case AttributeList::AT_ns_returns_retained:
3194 case AttributeList::AT_ns_returns_not_retained:
3195 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3196 cf = false;
3197 break;
3198
3199 case AttributeList::AT_cf_returns_retained:
3200 case AttributeList::AT_cf_returns_not_retained:
3201 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3202 cf = true;
3203 break;
3204 }
3205
3206 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003207 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003208 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003209 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003210 }
Mike Stumpbf916502009-07-24 19:02:52 +00003211
Chandler Carruth87c44602011-07-01 23:49:12 +00003212 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003213 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003214 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003215 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003216 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003217 S.Context));
3218 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003219 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003220 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003221 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003222 return;
3223 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003224 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003225 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003226 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003227 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003228 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003229 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003230 return;
3231 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003232 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003233 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003234 return;
3235 };
3236}
3237
John McCalldc7c5ad2011-07-22 08:53:00 +00003238static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3239 const AttributeList &attr) {
3240 SourceLocation loc = attr.getLoc();
3241
3242 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3243
3244 if (!isa<ObjCMethodDecl>(method)) {
3245 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3246 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
3247 return;
3248 }
3249
3250 // Check that the method returns a normal pointer.
3251 QualType resultType = method->getResultType();
3252 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
3253 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3254 << SourceRange(loc)
3255 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3256
3257 // Drop the attribute.
3258 return;
3259 }
3260
3261 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003262 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003263}
3264
Chandler Carruth1b03c872011-07-02 00:01:44 +00003265static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3266 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003267 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003268
Chandler Carruth87c44602011-07-01 23:49:12 +00003269 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003270 << Attr.getRange() << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003271}
3272
Chandler Carruth1b03c872011-07-02 00:01:44 +00003273static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3274 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003275 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003276 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003277 << Attr.getRange() << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003278 return;
3279 }
3280
Chandler Carruth87c44602011-07-01 23:49:12 +00003281 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003282 QualType type = vd->getType();
3283
3284 if (!type->isDependentType() &&
3285 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003286 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003287 << type;
3288 return;
3289 }
3290
3291 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3292
3293 // If we have no lifetime yet, check the lifetime we're presumably
3294 // going to infer.
3295 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3296 lifetime = type->getObjCARCImplicitLifetime();
3297
3298 switch (lifetime) {
3299 case Qualifiers::OCL_None:
3300 assert(type->isDependentType() &&
3301 "didn't infer lifetime for non-dependent type?");
3302 break;
3303
3304 case Qualifiers::OCL_Weak: // meaningful
3305 case Qualifiers::OCL_Strong: // meaningful
3306 break;
3307
3308 case Qualifiers::OCL_ExplicitNone:
3309 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003310 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003311 << (lifetime == Qualifiers::OCL_Autoreleasing);
3312 break;
3313 }
3314
Chandler Carruth87c44602011-07-01 23:49:12 +00003315 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003316 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003317}
3318
Charles Davisf0122fe2010-02-16 18:27:26 +00003319static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3320 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00003321 Attr.getKind() == AttributeList::AT_dllexport ||
3322 Attr.getKind() == AttributeList::AT_uuid;
3323}
3324
3325//===----------------------------------------------------------------------===//
3326// Microsoft specific attribute handlers.
3327//===----------------------------------------------------------------------===//
3328
Chandler Carruth1b03c872011-07-02 00:01:44 +00003329static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003330 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003331 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003332 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003333 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003334
Francois Pichet11542142010-12-19 06:50:37 +00003335 Expr *Arg = Attr.getArg(0);
3336 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003337 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003338 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3339 << "uuid" << 1;
3340 return;
3341 }
3342
Chris Lattner5f9e2722011-07-23 10:55:15 +00003343 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003344
3345 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3346 StrRef.back() == '}';
3347
3348 // Validate GUID length.
3349 if (IsCurly && StrRef.size() != 38) {
3350 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3351 return;
3352 }
3353 if (!IsCurly && StrRef.size() != 36) {
3354 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3355 return;
3356 }
3357
3358 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3359 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003360 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003361 if (IsCurly) // Skip the optional '{'
3362 ++I;
3363
3364 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003365 if (i == 8 || i == 13 || i == 18 || i == 23) {
3366 if (*I != '-') {
3367 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3368 return;
3369 }
3370 } else if (!isxdigit(*I)) {
3371 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3372 return;
3373 }
3374 I++;
3375 }
Francois Pichet11542142010-12-19 06:50:37 +00003376
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003377 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003378 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003379 } else
Francois Pichet11542142010-12-19 06:50:37 +00003380 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003381}
3382
Ted Kremenekb71368d2009-05-09 02:44:38 +00003383//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003384// Top Level Sema Entry Points
3385//===----------------------------------------------------------------------===//
3386
Chandler Carruth1b03c872011-07-02 00:01:44 +00003387static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3388 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003389 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003390 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3391 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3392 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003393 default:
3394 break;
3395 }
3396}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003397
Chandler Carruth1b03c872011-07-02 00:01:44 +00003398static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3399 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003400 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003401 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3402 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00003403 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003404 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003405 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003406 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003407 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003408 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003409 case AttributeList::AT_neon_vector_type:
3410 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003411 // Ignore these, these are type attributes, handled by
3412 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003413 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003414 case AttributeList::AT_device:
3415 case AttributeList::AT_host:
3416 case AttributeList::AT_overloadable:
3417 // Ignore, this is a non-inheritable attribute, handled
3418 // by ProcessNonInheritableDeclAttr.
3419 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003420 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3421 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003422 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003423 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003424 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003425 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3426 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3427 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003428 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003429 handleDependencyAttr (S, D, Attr); break;
3430 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3431 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3432 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3433 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3434 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003435 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003436 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003437 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003438 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3439 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3440 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3441 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003442 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003443 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003444 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003445 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3446 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3447 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3448 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3449 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003450 case AttributeList::AT_ownership_returns:
3451 case AttributeList::AT_ownership_takes:
3452 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003453 handleOwnershipAttr (S, D, Attr); break;
3454 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3455 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3456 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3457 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3458 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003459
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003460 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003461 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003462 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003463 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003464
John McCalldc7c5ad2011-07-22 08:53:00 +00003465 case AttributeList::AT_objc_returns_inner_pointer:
3466 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3467
Ted Kremenekb71368d2009-05-09 02:44:38 +00003468 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003469 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003470 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003471 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003472 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003473
3474 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003475 case AttributeList::AT_ns_returns_not_retained:
3476 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003477 case AttributeList::AT_ns_returns_retained:
3478 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003479 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003480
Nate Begeman6f3d8382009-06-26 06:32:41 +00003481 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003482 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003483
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003484 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003485 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003486
Chandler Carruth1b03c872011-07-02 00:01:44 +00003487 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3488 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3489 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3490 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003491 case AttributeList::AT_arc_weakref_unavailable:
3492 handleArcWeakrefUnavailableAttr (S, D, Attr);
3493 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003494 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3495 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3496 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3497 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003498 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003499 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3500 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3501 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003502 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003503 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003504 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003505 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003506 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003507 break;
John McCalld5313b02011-03-02 11:33:24 +00003508 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003509 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003510 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003511 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3512 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3513 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3514 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3515 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3516 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3517 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3518 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3519 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003520 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003521 // Just ignore
3522 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003523 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003524 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003525 break;
John McCall04a67a62010-02-05 21:31:56 +00003526 case AttributeList::AT_stdcall:
3527 case AttributeList::AT_cdecl:
3528 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003529 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003530 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003531 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003532 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003533 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003534 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003535 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003536 break;
Francois Pichet11542142010-12-19 06:50:37 +00003537 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003538 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003539 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003540
3541 // Thread safety attributes:
3542 case AttributeList::AT_guarded_var:
3543 handleGuardedVarAttr(S, D, Attr);
3544 break;
3545 case AttributeList::AT_pt_guarded_var:
3546 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3547 break;
3548 case AttributeList::AT_scoped_lockable:
3549 handleLockableAttr(S, D, Attr, /*scoped = */true);
3550 break;
3551 case AttributeList::AT_no_thread_safety_analysis:
3552 handleNoThreadSafetyAttr(S, D, Attr);
3553 break;
3554 case AttributeList::AT_lockable:
3555 handleLockableAttr(S, D, Attr);
3556 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003557 case AttributeList::AT_guarded_by:
3558 handleGuardedByAttr(S, D, Attr);
3559 break;
3560 case AttributeList::AT_pt_guarded_by:
3561 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3562 break;
3563 case AttributeList::AT_exclusive_lock_function:
3564 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3565 break;
3566 case AttributeList::AT_exclusive_locks_required:
3567 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3568 break;
3569 case AttributeList::AT_exclusive_trylock_function:
3570 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3571 break;
3572 case AttributeList::AT_lock_returned:
3573 handleLockReturnedAttr(S, D, Attr);
3574 break;
3575 case AttributeList::AT_locks_excluded:
3576 handleLocksExcludedAttr(S, D, Attr);
3577 break;
3578 case AttributeList::AT_shared_lock_function:
3579 handleLockFunAttr(S, D, Attr);
3580 break;
3581 case AttributeList::AT_shared_locks_required:
3582 handleLocksRequiredAttr(S, D, Attr);
3583 break;
3584 case AttributeList::AT_shared_trylock_function:
3585 handleTrylockFunAttr(S, D, Attr);
3586 break;
3587 case AttributeList::AT_unlock_function:
3588 handleUnlockFunAttr(S, D, Attr);
3589 break;
3590 case AttributeList::AT_acquired_before:
3591 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3592 break;
3593 case AttributeList::AT_acquired_after:
3594 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3595 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003596
Chris Lattner803d0802008-06-29 00:43:07 +00003597 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003598 // Ask target about the attribute.
3599 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3600 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003601 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3602 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003603 break;
3604 }
3605}
3606
Peter Collingbourne60700392011-01-21 02:08:45 +00003607/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3608/// the attribute applies to decls. If the attribute is a type attribute, just
3609/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3610/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3612 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003613 bool NonInheritable, bool Inheritable) {
3614 if (Attr.isInvalid())
3615 return;
3616
3617 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3618 // FIXME: Try to deal with other __declspec attributes!
3619 return;
3620
3621 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003622 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003623
3624 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003625 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003626}
3627
Chris Lattner803d0802008-06-29 00:43:07 +00003628/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3629/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003630void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003631 const AttributeList *AttrList,
3632 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003633 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003634 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003635 }
3636
3637 // GCC accepts
3638 // static int a9 __attribute__((weakref));
3639 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003640 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003641 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003642 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003643 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003644 }
3645}
3646
Ryan Flynne25ff832009-07-30 03:15:39 +00003647/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3648/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003649NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3650 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003651 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003652 NamedDecl *NewD = 0;
3653 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003654 FunctionDecl *NewFD;
3655 // FIXME: Missing call to CheckFunctionDeclaration().
3656 // FIXME: Mangling?
3657 // FIXME: Is the qualifier info correct?
3658 // FIXME: Is the DeclContext correct?
3659 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3660 Loc, Loc, DeclarationName(II),
3661 FD->getType(), FD->getTypeSourceInfo(),
3662 SC_None, SC_None,
3663 false/*isInlineSpecified*/,
3664 FD->hasPrototype(),
3665 false/*isConstexprSpecified*/);
3666 NewD = NewFD;
3667
3668 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003669 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003670
3671 // Fake up parameter variables; they are declared as if this were
3672 // a typedef.
3673 QualType FDTy = FD->getType();
3674 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3675 SmallVector<ParmVarDecl*, 16> Params;
3676 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3677 AE = FT->arg_type_end(); AI != AE; ++AI) {
3678 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3679 Param->setScopeInfo(0, Params.size());
3680 Params.push_back(Param);
3681 }
David Blaikie4278c652011-09-21 18:16:56 +00003682 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003683 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003684 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3685 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003686 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003687 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003688 VD->getStorageClass(),
3689 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003690 if (VD->getQualifier()) {
3691 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003692 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003693 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003694 }
3695 return NewD;
3696}
3697
3698/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3699/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003700void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003701 if (W.getUsed()) return; // only do this once
3702 W.setUsed(true);
3703 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3704 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003705 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003706 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3707 NDId->getName()));
3708 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003709 WeakTopLevelDecl.push_back(NewD);
3710 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3711 // to insert Decl at TU scope, sorry.
3712 DeclContext *SavedContext = CurContext;
3713 CurContext = Context.getTranslationUnitDecl();
3714 PushOnScopeChains(NewD, S);
3715 CurContext = SavedContext;
3716 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003717 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003718 }
3719}
3720
Chris Lattner0744e5f2008-06-29 00:23:49 +00003721/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3722/// it, apply them to D. This is a bit tricky because PD can have attributes
3723/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003724void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3725 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003726 // It's valid to "forward-declare" #pragma weak, in which case we
3727 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003728 if (Inheritable) {
3729 LoadExternalWeakUndeclaredIdentifiers();
3730 if (!WeakUndeclaredIdentifiers.empty()) {
3731 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3732 if (IdentifierInfo *Id = ND->getIdentifier()) {
3733 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3734 = WeakUndeclaredIdentifiers.find(Id);
3735 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3736 WeakInfo W = I->second;
3737 DeclApplyPragmaWeak(S, ND, W);
3738 WeakUndeclaredIdentifiers[Id] = W;
3739 }
John McCalld4aff0e2010-10-27 00:59:00 +00003740 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003741 }
3742 }
3743 }
3744
Chris Lattner0744e5f2008-06-29 00:23:49 +00003745 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003746 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003747 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003748
Chris Lattner0744e5f2008-06-29 00:23:49 +00003749 // Walk the declarator structure, applying decl attributes that were in a type
3750 // position to the decl itself. This handles cases like:
3751 // int *__attr__(x)** D;
3752 // when X is a decl attribute.
3753 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3754 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003755 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003756
Chris Lattner0744e5f2008-06-29 00:23:49 +00003757 // Finally, apply any attributes on the decl itself.
3758 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003759 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003760}
John McCall54abf7d2009-11-04 02:18:39 +00003761
John McCallf85e1932011-06-15 23:02:42 +00003762/// Is the given declaration allowed to use a forbidden type?
3763static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3764 // Private ivars are always okay. Unfortunately, people don't
3765 // always properly make their ivars private, even in system headers.
3766 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00003767 // Function declarations in sys headers will be marked unavailable.
3768 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
3769 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00003770 return false;
3771
3772 // Require it to be declared in a system header.
3773 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3774}
3775
3776/// Handle a delayed forbidden-type diagnostic.
3777static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3778 Decl *decl) {
3779 if (decl && isForbiddenTypeAllowed(S, decl)) {
3780 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3781 "this system declaration uses an unsupported type"));
3782 return;
3783 }
3784
3785 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3786 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3787 diag.Triggered = true;
3788}
3789
John McCalleee1d542011-02-14 07:13:47 +00003790// This duplicates a vector push_back but hides the need to know the
3791// size of the type.
3792void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3793 assert(StackSize <= StackCapacity);
3794
3795 // Grow the stack if necessary.
3796 if (StackSize == StackCapacity) {
3797 unsigned newCapacity = 2 * StackCapacity + 2;
3798 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3799 const char *oldBuffer = (const char*) Stack;
3800
3801 if (StackCapacity)
3802 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3803
3804 delete[] oldBuffer;
3805 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3806 StackCapacity = newCapacity;
3807 }
3808
3809 assert(StackSize < StackCapacity);
3810 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003811}
3812
John McCalleee1d542011-02-14 07:13:47 +00003813void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3814 Decl *decl) {
3815 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003816
John McCalleee1d542011-02-14 07:13:47 +00003817 // Check the invariants.
3818 assert(DD.StackSize >= state.SavedStackSize);
3819 assert(state.SavedStackSize >= DD.ActiveStackBase);
3820 assert(DD.ParsingDepth > 0);
3821
3822 // Drop the parsing depth.
3823 DD.ParsingDepth--;
3824
3825 // If there are no active diagnostics, we're done.
3826 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003827 return;
3828
John McCall2f514482010-01-27 03:50:35 +00003829 // We only want to actually emit delayed diagnostics when we
3830 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003831 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003832 // We emit all the active diagnostics, not just those starting
3833 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003834 // decl spec and another for each declarator; in a decl group like:
3835 // deprecated_typedef foo, *bar, baz();
3836 // only the declarator pops will be passed decls. This is correct;
3837 // we really do need to consider delayed diagnostics from the decl spec
3838 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003839 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3840 DelayedDiagnostic &diag = DD.Stack[i];
3841 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003842 continue;
3843
John McCalleee1d542011-02-14 07:13:47 +00003844 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003845 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003846 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003847 break;
3848
3849 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003850 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003851 break;
John McCallf85e1932011-06-15 23:02:42 +00003852
3853 case DelayedDiagnostic::ForbiddenType:
3854 handleDelayedForbiddenType(S, diag, decl);
3855 break;
John McCall2f514482010-01-27 03:50:35 +00003856 }
3857 }
3858 }
3859
John McCall58e6f342010-03-16 05:22:47 +00003860 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003861 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003862 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003863
John McCalleee1d542011-02-14 07:13:47 +00003864 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003865}
3866
3867static bool isDeclDeprecated(Decl *D) {
3868 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003869 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003870 return true;
3871 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3872 return false;
3873}
3874
John McCall9c3087b2010-08-26 02:13:20 +00003875void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003876 Decl *Ctx) {
3877 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003878 return;
3879
John McCall2f514482010-01-27 03:50:35 +00003880 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003881 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003882 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003883 << DD.getDeprecationDecl()->getDeclName()
3884 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003885 else
3886 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003887 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003888}
3889
Chris Lattner5f9e2722011-07-23 10:55:15 +00003890void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003891 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003892 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003893 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003894 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3895 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003896 return;
3897 }
3898
3899 // Otherwise, don't warn if our current context is deprecated.
3900 if (isDeclDeprecated(cast<Decl>(CurContext)))
3901 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003902 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003903 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3904 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003905 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003906 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003907 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003908 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003909 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003910 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3911 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003912 }
John McCall54abf7d2009-11-04 02:18:39 +00003913}