blob: 0483552c51952dde43e24b42f7f42858a83d80ba [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 Sadowskib51e0312011-08-09 17:59:31 +0000307 if (ArgExp->isTypeDependent())
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000308 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000309
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000310 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000311
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000312 // First see if we can just cast to record type, or point to record type.
313 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000314
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000315 // Now check if we index into a record type function param.
316 if(!RT && ParamIdxOk) {
317 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000318 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
319 if(FD && IL) {
320 unsigned int NumParams = FD->getNumParams();
321 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000322 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
323 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
324 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
326 << Attr.getName() << Idx + 1 << NumParams;
327 return false;
328 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000329 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
330 RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000331 }
332 }
333
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000334 if (!checkForLockableRecord(S, D, Attr, RT))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000335 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000336
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000337 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000338 }
339 return true;
340}
341
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000342//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000343// Attribute Implementations
344//===----------------------------------------------------------------------===//
345
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000346// FIXME: All this manual attribute parsing code is gross. At the
347// least add some helper functions to check most argument patterns (#
348// and types of args).
349
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000350static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
351 bool pointer = false) {
352 assert(!Attr.isInvalid());
353
354 if (!checkAttributeNumArgs(S, Attr, 0))
355 return;
356
357 // D must be either a member field or global (potentially shared) variable.
358 if (!mayBeSharedVariable(D)) {
359 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000360 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000361 return;
362 }
363
364 if (pointer && !checkIsPointer(S, D, Attr))
365 return;
366
367 if (pointer)
368 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getLoc(), S.Context));
369 else
370 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getLoc(), S.Context));
371}
372
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000373static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000374 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000375 assert(!Attr.isInvalid());
376
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000377 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000378 return;
379
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000380 Expr *Arg = Attr.getArg(0);
381
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000382 // D must be either a member field or global (potentially shared) variable.
383 if (!mayBeSharedVariable(D)) {
384 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000385 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000386 return;
387 }
388
389 if (pointer && !checkIsPointer(S, D, Attr))
390 return;
391
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000392 if (Arg->isTypeDependent())
393 return;
394
395 // check that the argument is lockable object
396 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000397 return;
398
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000399 if (pointer)
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000400 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getLoc(),
401 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000402 else
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000403 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getLoc(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000404}
405
406
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000407static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
408 bool scoped = false) {
409 assert(!Attr.isInvalid());
410
411 if (!checkAttributeNumArgs(S, Attr, 0))
412 return;
413
414 if (!isa<CXXRecordDecl>(D)) {
415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
416 << Attr.getName() << ExpectedClass;
417 return;
418 }
419
420 if (scoped)
421 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getLoc(), S.Context));
422 else
423 D->addAttr(::new (S.Context) LockableAttr(Attr.getLoc(), S.Context));
424}
425
426static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
427 const AttributeList &Attr) {
428 assert(!Attr.isInvalid());
429
430 if (!checkAttributeNumArgs(S, Attr, 0))
431 return;
432
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000433 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
435 << Attr.getName() << ExpectedFunctionOrMethod;
436 return;
437 }
438
439 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getLoc(),
440 S.Context));
441}
442
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000443static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
444 bool before) {
445 assert(!Attr.isInvalid());
446
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000447 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000448 return;
449
450 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000451 ValueDecl *VD = dyn_cast<ValueDecl>(D);
452 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000453 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000454 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000455 return;
456 }
457
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000458 // Check that this attribute only applies to lockable types
459 QualType QT = VD->getType();
460 if (!QT->isDependentType()) {
461 const RecordType *RT = getRecordType(QT);
462 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
463 S.Diag(Attr.getLoc(), diag::err_attribute_decl_not_lockable)
464 << Attr.getName();
465 return;
466 }
467 }
468
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000469 SmallVector<Expr*, 1> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000471 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000472 return;
473
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000474 unsigned Size = Args.size();
475 assert(Size == Attr.getNumArgs());
476 Expr **StartArg = Size == 0 ? 0 : &Args[0];
477
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000478 if (before)
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000479 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getLoc(), S.Context,
480 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000481 else
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000482 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getLoc(), S.Context,
483 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000484}
485
486static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000487 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000488 assert(!Attr.isInvalid());
489
490 // zero or more arguments ok
491
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000492 // check that the attribute is applied to a function
493 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000494 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
495 << Attr.getName() << ExpectedFunctionOrMethod;
496 return;
497 }
498
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000499 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000500 SmallVector<Expr*, 1> Args;
501 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000502 return;
503
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000504 unsigned Size = Args.size();
505 assert(Size == Attr.getNumArgs());
506 Expr **StartArg = Size == 0 ? 0 : &Args[0];
507
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000508 if (exclusive)
509 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getLoc(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000510 S.Context, StartArg,
511 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000512 else
513 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getLoc(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000514 S.Context, StartArg,
515 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000516}
517
518static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000519 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000520 assert(!Attr.isInvalid());
521
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000522 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000523 return;
524
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000525
526 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
528 << Attr.getName() << ExpectedFunctionOrMethod;
529 return;
530 }
531
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000532 if (!isIntOrBool(Attr.getArg(0))) {
533 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
534 << Attr.getName();
535 return;
536 }
537
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000538 SmallVector<Expr*, 2> Args;
539 Args.push_back(Attr.getArg(0)); //FIXME
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000540 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000541 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000542 return;
543
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000544 unsigned Size = Args.size();
545 assert(Size == Attr.getNumArgs());
546 Expr **StartArg = Size == 0 ? 0 : &Args[0];
547
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000548 if (exclusive)
549 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getLoc(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000550 S.Context,
551 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000552 else
553 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getLoc(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000554 S.Context, StartArg,
555 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000556}
557
558static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000559 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000560 assert(!Attr.isInvalid());
561
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000562 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000563 return;
564
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000565 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000566 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
567 << Attr.getName() << ExpectedFunctionOrMethod;
568 return;
569 }
570
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000571 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000572 SmallVector<Expr*, 1> Args;
573 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000574 return;
575
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000576 unsigned Size = Args.size();
577 assert(Size == Attr.getNumArgs());
578 Expr **StartArg = Size == 0 ? 0 : &Args[0];
579
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000580 if (exclusive)
581 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getLoc(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000582 S.Context, StartArg,
583 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 else
585 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getLoc(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000586 S.Context, StartArg,
587 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000588}
589
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000590static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000591 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000592 assert(!Attr.isInvalid());
593
594 // zero or more arguments ok
595
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000596 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
598 << Attr.getName() << ExpectedFunctionOrMethod;
599 return;
600 }
601
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000602 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000603 SmallVector<Expr*, 1> Args;
604 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000605 return;
606
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000607 unsigned Size = Args.size();
608 assert(Size == Attr.getNumArgs());
609 Expr **StartArg = Size == 0 ? 0 : &Args[0];
610
611 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getLoc(), S.Context,
612 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000613}
614
615static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000616 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000617 assert(!Attr.isInvalid());
618
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000619 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000620 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000621 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000622
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000623 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000624 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
625 << Attr.getName() << ExpectedFunctionOrMethod;
626 return;
627 }
628
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000629 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000630 return;
631
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000632 // check that the argument is lockable object
633 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
634 return;
635
636 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getLoc(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000637}
638
639static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000640 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000641 assert(!Attr.isInvalid());
642
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000643 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000644 return;
645
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000646 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
648 << Attr.getName() << ExpectedFunctionOrMethod;
649 return;
650 }
651
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000652 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000653 SmallVector<Expr*, 1> Args;
654 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000655 return;
656
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000657 unsigned Size = Args.size();
658 assert(Size == Attr.getNumArgs());
659 Expr **StartArg = Size == 0 ? 0 : &Args[0];
660
661 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getLoc(), S.Context,
662 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000663}
664
665
Chandler Carruth1b03c872011-07-02 00:01:44 +0000666static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
667 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000668 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000669 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000670 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000671 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000672 }
Mike Stumpbf916502009-07-24 19:02:52 +0000673
Chris Lattner6b6b5372008-06-26 18:38:35 +0000674 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000675
676 Expr *sizeExpr;
677
678 // Special case where the argument is a template id.
679 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000680 CXXScopeSpec SS;
681 UnqualifiedId id;
682 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000683
684 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
685 if (Size.isInvalid())
686 return;
687
688 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000689 } else {
690 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000691 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000692 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000693
Peter Collingbourne7a730022010-11-23 20:45:58 +0000694 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000695 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000696
697 // Instantiate/Install the vector type, and let Sema build the type for us.
698 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000699 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000700 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000701 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000702 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000703
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000704 // Remember this typedef decl, we will need it later for diagnostics.
705 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000706 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000707}
708
Chandler Carruth1b03c872011-07-02 00:01:44 +0000709static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000710 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000711 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000712 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000713
Chandler Carruth87c44602011-07-01 23:49:12 +0000714 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Sean Huntcf807c42010-08-18 23:23:40 +0000715 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000716 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000717 // If the alignment is less than or equal to 8 bits, the packed attribute
718 // has no effect.
719 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000720 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000721 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000722 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000723 else
Sean Huntcf807c42010-08-18 23:23:40 +0000724 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000725 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000726 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000727}
728
Chandler Carruth1b03c872011-07-02 00:01:44 +0000729static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000730 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000731 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
732 else
733 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
734}
735
Chandler Carruth1b03c872011-07-02 00:01:44 +0000736static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000737 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000738 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000739 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000740
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000741 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000742 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000743 if (MD->isInstanceMethod()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000744 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000745 return;
746 }
747
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000748 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000749}
750
Chandler Carruth1b03c872011-07-02 00:01:44 +0000751static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000752 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000753 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000754 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000755
756 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000757 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000758 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
759 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000760 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000761 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000762
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000763 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000764}
765
Chandler Carruth1b03c872011-07-02 00:01:44 +0000766static void handleIBOutletCollection(Sema &S, Decl *D,
767 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000768
769 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000770 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000771 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
772 return;
773 }
774
775 // The IBOutletCollection attributes only apply to instance variables of
776 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000777 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000778 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000779 return;
780 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000781 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000782 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
783 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
784 << VD->getType() << 0;
785 return;
786 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000787 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000788 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
789 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
790 << PD->getType() << 1;
791 return;
792 }
793
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000794 IdentifierInfo *II = Attr.getParameterName();
795 if (!II)
796 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000797
John McCallb3d87482010-08-24 05:47:05 +0000798 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000799 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000800 if (!TypeRep) {
801 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
802 return;
803 }
John McCallb3d87482010-08-24 05:47:05 +0000804 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000805 // Diagnose use of non-object type in iboutletcollection attribute.
806 // FIXME. Gnu attribute extension ignores use of builtin types in
807 // attributes. So, __attribute__((iboutletcollection(char))) will be
808 // treated as __attribute__((iboutletcollection())).
809 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
810 !QT->isObjCObjectType()) {
811 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
812 return;
813 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000814 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000815 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000816}
817
Chandler Carruthd309c812011-07-01 23:49:16 +0000818static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000819 if (const RecordType *UT = T->getAsUnionType())
820 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
821 RecordDecl *UD = UT->getDecl();
822 for (RecordDecl::field_iterator it = UD->field_begin(),
823 itend = UD->field_end(); it != itend; ++it) {
824 QualType QT = it->getType();
825 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
826 T = QT;
827 return;
828 }
829 }
830 }
831}
832
Chandler Carruth1b03c872011-07-02 00:01:44 +0000833static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000834 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
835 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000836 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000838 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000839 return;
840 }
Mike Stumpbf916502009-07-24 19:02:52 +0000841
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000842 // In C++ the implicit 'this' function parameter also counts, and they are
843 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000844 bool HasImplicitThisParam = isInstanceMethod(D);
845 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000846
847 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000848 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000849
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000850 for (AttributeList::arg_iterator I=Attr.arg_begin(),
851 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000852
853
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000854 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000855 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000856 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000857 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
858 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000859 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
860 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000861 return;
862 }
Mike Stumpbf916502009-07-24 19:02:52 +0000863
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000864 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000865
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000866 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000867 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000868 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000869 return;
870 }
Mike Stumpbf916502009-07-24 19:02:52 +0000871
Ted Kremenek465172f2008-07-21 22:09:15 +0000872 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000873 if (HasImplicitThisParam) {
874 if (x == 0) {
875 S.Diag(Attr.getLoc(),
876 diag::err_attribute_invalid_implicit_this_argument)
877 << "nonnull" << Ex->getSourceRange();
878 return;
879 }
880 --x;
881 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000882
883 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000884 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000885 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000886
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000887 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000888 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000889 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000890 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000891 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000892 }
Mike Stumpbf916502009-07-24 19:02:52 +0000893
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000894 NonNullArgs.push_back(x);
895 }
Mike Stumpbf916502009-07-24 19:02:52 +0000896
897 // If no arguments were specified to __attribute__((nonnull)) then all pointer
898 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000899 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000900 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
901 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000902 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000903 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000904 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000905 }
Mike Stumpbf916502009-07-24 19:02:52 +0000906
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000907 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000908 if (NonNullArgs.empty()) {
909 // Warn the trivial case only if attribute is not coming from a
910 // macro instantiation.
911 if (Attr.getLoc().isFileID())
912 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000913 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000914 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000915 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000916
917 unsigned* start = &NonNullArgs[0];
918 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000919 llvm::array_pod_sort(start, start + size);
Chandler Carruth87c44602011-07-01 23:49:12 +0000920 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000921 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000922}
923
Chandler Carruth1b03c872011-07-02 00:01:44 +0000924static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000925 // This attribute must be applied to a function declaration.
926 // The first argument to the attribute must be a string,
927 // the name of the resource, for example "malloc".
928 // The following arguments must be argument indexes, the arguments must be
929 // of integer type for Returns, otherwise of pointer type.
930 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000931 // after being held. free() should be __attribute((ownership_takes)), whereas
932 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000933
934 if (!AL.getParameterName()) {
935 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
936 << AL.getName()->getName() << 1;
937 return;
938 }
939 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000940 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000941 switch (AL.getKind()) {
942 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000943 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000944 if (AL.getNumArgs() < 1) {
945 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
946 return;
947 }
Jordy Rose2a479922010-08-12 08:54:03 +0000948 break;
949 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000950 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000951 if (AL.getNumArgs() < 1) {
952 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
953 return;
954 }
Jordy Rose2a479922010-08-12 08:54:03 +0000955 break;
956 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000957 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000958 if (AL.getNumArgs() > 1) {
959 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
960 << AL.getNumArgs() + 1;
961 return;
962 }
Jordy Rose2a479922010-08-12 08:54:03 +0000963 break;
964 default:
965 // This should never happen given how we are called.
966 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000967 }
968
Chandler Carruth87c44602011-07-01 23:49:12 +0000969 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000970 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
971 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000972 return;
973 }
974
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000975 // In C++ the implicit 'this' function parameter also counts, and they are
976 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000977 bool HasImplicitThisParam = isInstanceMethod(D);
978 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000979
Chris Lattner5f9e2722011-07-23 10:55:15 +0000980 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000981
982 // Normalize the argument, __foo__ becomes foo.
983 if (Module.startswith("__") && Module.endswith("__"))
984 Module = Module.substr(2, Module.size() - 4);
985
Chris Lattner5f9e2722011-07-23 10:55:15 +0000986 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000987
Jordy Rose2a479922010-08-12 08:54:03 +0000988 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
989 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000990
Peter Collingbourne7a730022010-11-23 20:45:58 +0000991 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000992 llvm::APSInt ArgNum(32);
993 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
994 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
995 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
996 << AL.getName()->getName() << IdxExpr->getSourceRange();
997 continue;
998 }
999
1000 unsigned x = (unsigned) ArgNum.getZExtValue();
1001
1002 if (x > NumArgs || x < 1) {
1003 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1004 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1005 continue;
1006 }
1007 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001008 if (HasImplicitThisParam) {
1009 if (x == 0) {
1010 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1011 << "ownership" << IdxExpr->getSourceRange();
1012 return;
1013 }
1014 --x;
1015 }
1016
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001017 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001018 case OwnershipAttr::Takes:
1019 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001020 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001021 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001022 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1023 // FIXME: Should also highlight argument in decl.
1024 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001025 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001026 << "pointer"
1027 << IdxExpr->getSourceRange();
1028 continue;
1029 }
1030 break;
1031 }
Sean Huntcf807c42010-08-18 23:23:40 +00001032 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001033 if (AL.getNumArgs() > 1) {
1034 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001035 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001036 llvm::APSInt ArgNum(32);
1037 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1038 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1039 S.Diag(AL.getLoc(), diag::err_ownership_type)
1040 << "ownership_returns" << "integer"
1041 << IdxExpr->getSourceRange();
1042 return;
1043 }
1044 }
1045 break;
1046 }
Jordy Rose2a479922010-08-12 08:54:03 +00001047 default:
1048 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001049 } // switch
1050
1051 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001052 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001053 i = D->specific_attr_begin<OwnershipAttr>(),
1054 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001055 i != e; ++i) {
1056 if ((*i)->getOwnKind() != K) {
1057 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1058 I!=E; ++I) {
1059 if (x == *I) {
1060 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1061 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001062 }
1063 }
1064 }
1065 }
1066 OwnershipArgs.push_back(x);
1067 }
1068
1069 unsigned* start = OwnershipArgs.data();
1070 unsigned size = OwnershipArgs.size();
1071 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001072
1073 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1074 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1075 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001076 }
Sean Huntcf807c42010-08-18 23:23:40 +00001077
Chandler Carruth87c44602011-07-01 23:49:12 +00001078 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001079 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001080}
1081
John McCall332bb2a2011-02-08 22:35:49 +00001082/// Whether this declaration has internal linkage for the purposes of
1083/// things that want to complain about things not have internal linkage.
1084static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1085 switch (D->getLinkage()) {
1086 case NoLinkage:
1087 case InternalLinkage:
1088 return true;
1089
1090 // Template instantiations that go from external to unique-external
1091 // shouldn't get diagnosed.
1092 case UniqueExternalLinkage:
1093 return true;
1094
1095 case ExternalLinkage:
1096 return false;
1097 }
1098 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001099 return false;
1100}
1101
Chandler Carruth1b03c872011-07-02 00:01:44 +00001102static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001103 // Check the attribute arguments.
1104 if (Attr.getNumArgs() > 1) {
1105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1106 return;
1107 }
1108
Chandler Carruth87c44602011-07-01 23:49:12 +00001109 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001110 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001111 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001112 return;
1113 }
1114
Chandler Carruth87c44602011-07-01 23:49:12 +00001115 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001116
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001117 // gcc rejects
1118 // class c {
1119 // static int a __attribute__((weakref ("v2")));
1120 // static int b() __attribute__((weakref ("f3")));
1121 // };
1122 // and ignores the attributes of
1123 // void f(void) {
1124 // static int a __attribute__((weakref ("v2")));
1125 // }
1126 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001127 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001128 if (!Ctx->isFileContext()) {
1129 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001130 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001131 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001132 }
1133
1134 // The GCC manual says
1135 //
1136 // At present, a declaration to which `weakref' is attached can only
1137 // be `static'.
1138 //
1139 // It also says
1140 //
1141 // Without a TARGET,
1142 // given as an argument to `weakref' or to `alias', `weakref' is
1143 // equivalent to `weak'.
1144 //
1145 // gcc 4.4.1 will accept
1146 // int a7 __attribute__((weakref));
1147 // as
1148 // int a7 __attribute__((weak));
1149 // This looks like a bug in gcc. We reject that for now. We should revisit
1150 // it if this behaviour is actually used.
1151
John McCall332bb2a2011-02-08 22:35:49 +00001152 if (!hasEffectivelyInternalLinkage(nd)) {
1153 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001154 return;
1155 }
1156
1157 // GCC rejects
1158 // static ((alias ("y"), weakref)).
1159 // Should we? How to check that weakref is before or after alias?
1160
1161 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001162 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001163 Arg = Arg->IgnoreParenCasts();
1164 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1165
Douglas Gregor5cee1192011-07-27 05:40:30 +00001166 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001167 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1168 << "weakref" << 1;
1169 return;
1170 }
1171 // GCC will accept anything as the argument of weakref. Should we
1172 // check for an existing decl?
Chandler Carruth87c44602011-07-01 23:49:12 +00001173 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001174 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001175 }
1176
Chandler Carruth87c44602011-07-01 23:49:12 +00001177 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001178}
1179
Chandler Carruth1b03c872011-07-02 00:01:44 +00001180static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001181 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001182 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001184 return;
1185 }
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Peter Collingbourne7a730022010-11-23 20:45:58 +00001187 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188 Arg = Arg->IgnoreParenCasts();
1189 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001190
Douglas Gregor5cee1192011-07-27 05:40:30 +00001191 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001192 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001193 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001194 return;
1195 }
Mike Stumpbf916502009-07-24 19:02:52 +00001196
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001197 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001198 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1199 return;
1200 }
1201
Chris Lattner6b6b5372008-06-26 18:38:35 +00001202 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001203
Chandler Carruth87c44602011-07-01 23:49:12 +00001204 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001205 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001206}
1207
Chandler Carruth1b03c872011-07-02 00:01:44 +00001208static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001209 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001210 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001211 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001212
Chandler Carruth87c44602011-07-01 23:49:12 +00001213 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001214 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001215 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001216 return;
1217 }
1218
Chandler Carruth87c44602011-07-01 23:49:12 +00001219 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001220}
1221
Chandler Carruth1b03c872011-07-02 00:01:44 +00001222static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1223 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001224 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001225 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1227 return;
1228 }
1229
Chandler Carruth87c44602011-07-01 23:49:12 +00001230 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001231 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001232 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001233 return;
1234 }
Mike Stumpbf916502009-07-24 19:02:52 +00001235
Chandler Carruth87c44602011-07-01 23:49:12 +00001236 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001237}
1238
Chandler Carruth1b03c872011-07-02 00:01:44 +00001239static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001240 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001241 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1243 return;
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chandler Carruth87c44602011-07-01 23:49:12 +00001246 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001247 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001248 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001249 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001250 return;
1251 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001252 }
1253
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001254 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001255}
1256
Chandler Carruth1b03c872011-07-02 00:01:44 +00001257static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001258 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001259 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001260 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001261
Chandler Carruth87c44602011-07-01 23:49:12 +00001262 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001263}
1264
Chandler Carruth1b03c872011-07-02 00:01:44 +00001265static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001266 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001267 if (isa<VarDecl>(D))
1268 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001269 else
1270 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001271 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001272}
1273
Chandler Carruth1b03c872011-07-02 00:01:44 +00001274static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001275 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001276 if (isa<VarDecl>(D))
1277 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001278 else
1279 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001280 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001281}
1282
Chandler Carruth1b03c872011-07-02 00:01:44 +00001283static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001284 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001285
1286 if (S.CheckNoReturnAttr(attr)) return;
1287
Chandler Carruth87c44602011-07-01 23:49:12 +00001288 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001289 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001290 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001291 return;
1292 }
1293
Chandler Carruth87c44602011-07-01 23:49:12 +00001294 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001295}
1296
1297bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001298 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001299 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1300 attr.setInvalid();
1301 return true;
1302 }
1303
1304 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001305}
1306
Chandler Carruth1b03c872011-07-02 00:01:44 +00001307static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1308 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001309
1310 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1311 // because 'analyzer_noreturn' does not impact the type.
1312
Chandler Carruth1731e202011-07-11 23:30:35 +00001313 if(!checkAttributeNumArgs(S, Attr, 0))
1314 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001315
Chandler Carruth87c44602011-07-01 23:49:12 +00001316 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1317 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001318 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1319 && !VD->getType()->isFunctionPointerType())) {
1320 S.Diag(Attr.getLoc(),
1321 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1322 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001323 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001324 return;
1325 }
1326 }
1327
Chandler Carruth87c44602011-07-01 23:49:12 +00001328 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001329}
1330
John Thompson35cc9622010-08-09 21:53:52 +00001331// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001332static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001333/*
1334 Returning a Vector Class in Registers
1335
Eric Christopherf48f3672010-12-01 22:13:54 +00001336 According to the PPU ABI specifications, a class with a single member of
1337 vector type is returned in memory when used as the return value of a function.
1338 This results in inefficient code when implementing vector classes. To return
1339 the value in a single vector register, add the vecreturn attribute to the
1340 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001341
1342 Example:
1343
1344 struct Vector
1345 {
1346 __vector float xyzw;
1347 } __attribute__((vecreturn));
1348
1349 Vector Add(Vector lhs, Vector rhs)
1350 {
1351 Vector result;
1352 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1353 return result; // This will be returned in a register
1354 }
1355*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001356 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001357 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001358 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001359 return;
1360 }
1361
Chandler Carruth87c44602011-07-01 23:49:12 +00001362 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001363 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1364 return;
1365 }
1366
Chandler Carruth87c44602011-07-01 23:49:12 +00001367 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001368 int count = 0;
1369
1370 if (!isa<CXXRecordDecl>(record)) {
1371 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1372 return;
1373 }
1374
1375 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1376 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1377 return;
1378 }
1379
Eric Christopherf48f3672010-12-01 22:13:54 +00001380 for (RecordDecl::field_iterator iter = record->field_begin();
1381 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001382 if ((count == 1) || !iter->getType()->isVectorType()) {
1383 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1384 return;
1385 }
1386 count++;
1387 }
1388
Chandler Carruth87c44602011-07-01 23:49:12 +00001389 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001390}
1391
Chandler Carruth1b03c872011-07-02 00:01:44 +00001392static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001393 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001394 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001395 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001396 return;
1397 }
1398 // FIXME: Actually store the attribute on the declaration
1399}
1400
Chandler Carruth1b03c872011-07-02 00:01:44 +00001401static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001402 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001403 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001404 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001405 return;
1406 }
Mike Stumpbf916502009-07-24 19:02:52 +00001407
Chandler Carruth87c44602011-07-01 23:49:12 +00001408 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1409 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001411 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001412 return;
1413 }
Mike Stumpbf916502009-07-24 19:02:52 +00001414
Chandler Carruth87c44602011-07-01 23:49:12 +00001415 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001416}
1417
Chandler Carruth1b03c872011-07-02 00:01:44 +00001418static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001419 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001420 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1422 return;
1423 }
Mike Stumpbf916502009-07-24 19:02:52 +00001424
Chandler Carruth87c44602011-07-01 23:49:12 +00001425 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001426 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001427 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1428 return;
1429 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001430 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001431 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001432 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001433 return;
1434 }
Mike Stumpbf916502009-07-24 19:02:52 +00001435
Chandler Carruth87c44602011-07-01 23:49:12 +00001436 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001437}
1438
Chandler Carruth1b03c872011-07-02 00:01:44 +00001439static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001440 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001441 if (Attr.getNumArgs() > 1) {
1442 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001443 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001444 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001445
1446 int priority = 65535; // FIXME: Do not hardcode such constants.
1447 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001448 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001449 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001450 if (E->isTypeDependent() || E->isValueDependent() ||
1451 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001453 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001454 return;
1455 }
1456 priority = Idx.getZExtValue();
1457 }
Mike Stumpbf916502009-07-24 19:02:52 +00001458
Chandler Carruth87c44602011-07-01 23:49:12 +00001459 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001460 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001461 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001462 return;
1463 }
1464
Chandler Carruth87c44602011-07-01 23:49:12 +00001465 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001466 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001467}
1468
Chandler Carruth1b03c872011-07-02 00:01:44 +00001469static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001470 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001471 if (Attr.getNumArgs() > 1) {
1472 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001473 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001474 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001475
1476 int priority = 65535; // FIXME: Do not hardcode such constants.
1477 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001478 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001479 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001480 if (E->isTypeDependent() || E->isValueDependent() ||
1481 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001483 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001484 return;
1485 }
1486 priority = Idx.getZExtValue();
1487 }
Mike Stumpbf916502009-07-24 19:02:52 +00001488
Chandler Carruth87c44602011-07-01 23:49:12 +00001489 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001490 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001491 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001492 return;
1493 }
1494
Chandler Carruth87c44602011-07-01 23:49:12 +00001495 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001496 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001497}
1498
Chandler Carruth1b03c872011-07-02 00:01:44 +00001499static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001500 unsigned NumArgs = Attr.getNumArgs();
1501 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001502 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503 return;
1504 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001505
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001506 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001507 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001508 if (NumArgs == 1) {
1509 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001510 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001511 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1512 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001513 return;
1514 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001515 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001516 }
Mike Stumpbf916502009-07-24 19:02:52 +00001517
Chandler Carruth87c44602011-07-01 23:49:12 +00001518 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001519}
1520
Chandler Carruth1b03c872011-07-02 00:01:44 +00001521static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001522 unsigned NumArgs = Attr.getNumArgs();
1523 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001524 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001525 return;
1526 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001527
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001528 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001529 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001530 if (NumArgs == 1) {
1531 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001532 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001533 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001534 diag::err_attribute_not_string) << "unavailable";
1535 return;
1536 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001537 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001538 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001539 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001540}
1541
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001542static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1543 const AttributeList &Attr) {
1544 unsigned NumArgs = Attr.getNumArgs();
1545 if (NumArgs > 0) {
1546 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1547 return;
1548 }
1549
1550 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1551 Attr.getLoc(), S.Context));
1552}
1553
Chandler Carruth1b03c872011-07-02 00:01:44 +00001554static void handleAvailabilityAttr(Sema &S, Decl *D,
1555 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001556 IdentifierInfo *Platform = Attr.getParameterName();
1557 SourceLocation PlatformLoc = Attr.getParameterLoc();
1558
Chris Lattner5f9e2722011-07-23 10:55:15 +00001559 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001560 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1561 if (PlatformName.empty()) {
1562 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1563 << Platform;
1564
1565 PlatformName = Platform->getName();
1566 }
1567
1568 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1569 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1570 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001571 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001572
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001573 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001574 // of these steps are needed).
1575 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001576 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001577 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1578 << 1 << PlatformName << Deprecated.Version.getAsString()
1579 << 0 << Introduced.Version.getAsString();
1580 return;
1581 }
1582
1583 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001584 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001585 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1586 << 2 << PlatformName << Obsoleted.Version.getAsString()
1587 << 0 << Introduced.Version.getAsString();
1588 return;
1589 }
1590
1591 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001592 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001593 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1594 << 2 << PlatformName << Obsoleted.Version.getAsString()
1595 << 1 << Deprecated.Version.getAsString();
1596 return;
1597 }
1598
Chandler Carruth87c44602011-07-01 23:49:12 +00001599 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001600 Platform,
1601 Introduced.Version,
1602 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001603 Obsoleted.Version,
1604 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001605}
1606
Chandler Carruth1b03c872011-07-02 00:01:44 +00001607static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001608 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001609 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001610 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001611
Peter Collingbourne7a730022010-11-23 20:45:58 +00001612 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001613 Arg = Arg->IgnoreParenCasts();
1614 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001615
Douglas Gregor5cee1192011-07-27 05:40:30 +00001616 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001617 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001618 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001619 return;
1620 }
Mike Stumpbf916502009-07-24 19:02:52 +00001621
Chris Lattner5f9e2722011-07-23 10:55:15 +00001622 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001623 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001624
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001625 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001626 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001627 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001628 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001629 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001630 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001631 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001632 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001633 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001634 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001635 return;
1636 }
Mike Stumpbf916502009-07-24 19:02:52 +00001637
Chandler Carruth87c44602011-07-01 23:49:12 +00001638 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001639}
1640
Chandler Carruth1b03c872011-07-02 00:01:44 +00001641static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1642 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001643 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1644 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001646 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001647 return;
1648 }
1649
Chandler Carruth87c44602011-07-01 23:49:12 +00001650 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1651 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1652 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001653 << "objc_method_family" << 1;
1654 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001655 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001656 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001657 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001658 return;
1659 }
1660
Chris Lattner5f9e2722011-07-23 10:55:15 +00001661 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001662 ObjCMethodFamilyAttr::FamilyKind family;
1663 if (param == "none")
1664 family = ObjCMethodFamilyAttr::OMF_None;
1665 else if (param == "alloc")
1666 family = ObjCMethodFamilyAttr::OMF_alloc;
1667 else if (param == "copy")
1668 family = ObjCMethodFamilyAttr::OMF_copy;
1669 else if (param == "init")
1670 family = ObjCMethodFamilyAttr::OMF_init;
1671 else if (param == "mutableCopy")
1672 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1673 else if (param == "new")
1674 family = ObjCMethodFamilyAttr::OMF_new;
1675 else {
1676 // Just warn and ignore it. This is future-proof against new
1677 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001678 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001679 return;
1680 }
1681
John McCallf85e1932011-06-15 23:02:42 +00001682 if (family == ObjCMethodFamilyAttr::OMF_init &&
1683 !method->getResultType()->isObjCObjectPointerType()) {
1684 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1685 << method->getResultType();
1686 // Ignore the attribute.
1687 return;
1688 }
1689
Chandler Carruth87c44602011-07-01 23:49:12 +00001690 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCallf85e1932011-06-15 23:02:42 +00001691 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001692}
1693
Chandler Carruth1b03c872011-07-02 00:01:44 +00001694static void handleObjCExceptionAttr(Sema &S, Decl *D,
1695 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001696 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001697 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001698
Chris Lattner0db29ec2009-02-14 08:09:34 +00001699 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1700 if (OCI == 0) {
1701 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1702 return;
1703 }
Mike Stumpbf916502009-07-24 19:02:52 +00001704
Sean Huntcf807c42010-08-18 23:23:40 +00001705 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001706}
1707
Chandler Carruth1b03c872011-07-02 00:01:44 +00001708static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001709 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001711 return;
1712 }
Richard Smith162e1c12011-04-15 14:24:37 +00001713 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001714 QualType T = TD->getUnderlyingType();
1715 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001716 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001717 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1718 return;
1719 }
1720 }
Sean Huntcf807c42010-08-18 23:23:40 +00001721 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001722}
1723
Mike Stumpbf916502009-07-24 19:02:52 +00001724static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001725handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001726 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001727 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001728 return;
1729 }
1730
1731 if (!isa<FunctionDecl>(D)) {
1732 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1733 return;
1734 }
1735
Sean Huntcf807c42010-08-18 23:23:40 +00001736 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001737}
1738
Chandler Carruth1b03c872011-07-02 00:01:44 +00001739static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001740 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001741 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001742 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001743 return;
1744 }
Mike Stumpbf916502009-07-24 19:02:52 +00001745
Steve Naroff9eae5762008-09-18 16:44:58 +00001746 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001747 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001748 return;
1749 }
Mike Stumpbf916502009-07-24 19:02:52 +00001750
Sean Huntcf807c42010-08-18 23:23:40 +00001751 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001752 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001753 type = BlocksAttr::ByRef;
1754 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001755 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001756 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001757 return;
1758 }
Mike Stumpbf916502009-07-24 19:02:52 +00001759
Chandler Carruth87c44602011-07-01 23:49:12 +00001760 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001761}
1762
Chandler Carruth1b03c872011-07-02 00:01:44 +00001763static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001764 // check the attribute arguments.
1765 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001766 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001767 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001768 }
1769
Anders Carlsson77091822008-10-05 18:05:59 +00001770 int sentinel = 0;
1771 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001772 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001773 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001774 if (E->isTypeDependent() || E->isValueDependent() ||
1775 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001776 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001777 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001778 return;
1779 }
1780 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001781
Anders Carlsson77091822008-10-05 18:05:59 +00001782 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001783 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1784 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001785 return;
1786 }
1787 }
1788
1789 int nullPos = 0;
1790 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001791 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001792 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001793 if (E->isTypeDependent() || E->isValueDependent() ||
1794 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001795 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001796 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001797 return;
1798 }
1799 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001800
Anders Carlsson77091822008-10-05 18:05:59 +00001801 if (nullPos > 1 || nullPos < 0) {
1802 // FIXME: This error message could be improved, it would be nice
1803 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001804 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1805 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001806 return;
1807 }
1808 }
1809
Chandler Carruth87c44602011-07-01 23:49:12 +00001810 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall183700f2009-09-21 23:43:11 +00001811 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001812 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001813
Chris Lattner897cd902009-03-17 23:03:47 +00001814 if (isa<FunctionNoProtoType>(FT)) {
1815 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1816 return;
1817 }
Mike Stumpbf916502009-07-24 19:02:52 +00001818
Chris Lattner897cd902009-03-17 23:03:47 +00001819 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001820 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001821 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001822 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001823 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001824 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001825 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001826 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001827 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001828 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001829 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1830 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001831 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001832 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001833 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001834 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001835 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001836 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001837 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001838 int m = Ty->isFunctionPointerType() ? 0 : 1;
1839 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001840 return;
1841 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001842 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001843 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001844 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001845 return;
1846 }
Anders Carlsson77091822008-10-05 18:05:59 +00001847 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001848 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001849 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001850 return;
1851 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001853 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001854}
1855
Chandler Carruth1b03c872011-07-02 00:01:44 +00001856static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001857 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001858 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001859 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001860
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001861 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001863 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001864 return;
1865 }
Mike Stumpbf916502009-07-24 19:02:52 +00001866
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001867 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1868 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1869 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001870 return;
1871 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001872 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1873 if (MD->getResultType()->isVoidType()) {
1874 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1875 << Attr.getName() << 1;
1876 return;
1877 }
1878
Sean Huntcf807c42010-08-18 23:23:40 +00001879 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001880}
1881
Chandler Carruth1b03c872011-07-02 00:01:44 +00001882static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001883 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001884 if (Attr.hasParameterOrArguments()) {
1885 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001886 return;
1887 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001888
Chandler Carruth87c44602011-07-01 23:49:12 +00001889 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1890 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1891 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001892 return;
1893 }
1894
Chandler Carruth87c44602011-07-01 23:49:12 +00001895 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001896
1897 // 'weak' only applies to declarations with external linkage.
1898 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001899 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001900 return;
1901 }
Mike Stumpbf916502009-07-24 19:02:52 +00001902
Chandler Carruth87c44602011-07-01 23:49:12 +00001903 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001904}
1905
Chandler Carruth1b03c872011-07-02 00:01:44 +00001906static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001907 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001908 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001909 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001910
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001911
1912 // weak_import only applies to variable & function declarations.
1913 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001914 if (!D->canBeWeakImported(isDef)) {
1915 if (isDef)
1916 S.Diag(Attr.getLoc(),
1917 diag::warn_attribute_weak_import_invalid_on_definition)
1918 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001919 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001920 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001921 isa<ObjCInterfaceDecl>(D))) {
1922 // Nothing to warn about here.
1923 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001925 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001926
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001927 return;
1928 }
1929
Sean Huntcf807c42010-08-18 23:23:40 +00001930 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001931}
1932
Chandler Carruth1b03c872011-07-02 00:01:44 +00001933static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1934 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001935 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001936 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001937 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001938
1939 unsigned WGSize[3];
1940 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001941 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001942 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001943 if (E->isTypeDependent() || E->isValueDependent() ||
1944 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001945 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1946 << "reqd_work_group_size" << E->getSourceRange();
1947 return;
1948 }
1949 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1950 }
Sean Huntcf807c42010-08-18 23:23:40 +00001951 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1952 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001953 WGSize[2]));
1954}
1955
Chandler Carruth1b03c872011-07-02 00:01:44 +00001956static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001957 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001958 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001959 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001960
1961 // Make sure that there is a string literal as the sections's single
1962 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001963 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001964 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001965 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001966 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001967 return;
1968 }
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Chris Lattner797c3c42009-08-10 19:03:04 +00001970 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001971 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001972 if (!Error.empty()) {
1973 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1974 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001975 return;
1976 }
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001978 // This attribute cannot be applied to local variables.
1979 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1980 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1981 return;
1982 }
1983
Eric Christopherf48f3672010-12-01 22:13:54 +00001984 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1985 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001986}
1987
Chris Lattner6b6b5372008-06-26 18:38:35 +00001988
Chandler Carruth1b03c872011-07-02 00:01:44 +00001989static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001990 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001991 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001993 return;
1994 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001995
Chandler Carruth87c44602011-07-01 23:49:12 +00001996 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001997 if (Existing->getLocation().isInvalid())
1998 Existing->setLocation(Attr.getLoc());
1999 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002000 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002001 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002002}
2003
Chandler Carruth1b03c872011-07-02 00:01:44 +00002004static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002005 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002006 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002007 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002008 return;
2009 }
Mike Stumpbf916502009-07-24 19:02:52 +00002010
Chandler Carruth87c44602011-07-01 23:49:12 +00002011 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002012 if (Existing->getLocation().isInvalid())
2013 Existing->setLocation(Attr.getLoc());
2014 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002015 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002016 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002017}
2018
Chandler Carruth1b03c872011-07-02 00:01:44 +00002019static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002020 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002021 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002022 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002023
Chandler Carruth87c44602011-07-01 23:49:12 +00002024 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002025}
2026
Chandler Carruth1b03c872011-07-02 00:01:44 +00002027static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002028 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002029 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2030 return;
2031 }
Mike Stumpbf916502009-07-24 19:02:52 +00002032
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002033 if (Attr.getNumArgs() != 0) {
2034 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2035 return;
2036 }
Mike Stumpbf916502009-07-24 19:02:52 +00002037
Chandler Carruth87c44602011-07-01 23:49:12 +00002038 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002039
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002040 if (!VD || !VD->hasLocalStorage()) {
2041 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2042 return;
2043 }
Mike Stumpbf916502009-07-24 19:02:52 +00002044
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002045 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002046 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002047 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002048 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2049 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002050 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002051 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002052 Attr.getParameterName();
2053 return;
2054 }
Mike Stumpbf916502009-07-24 19:02:52 +00002055
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002056 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2057 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002058 S.Diag(Attr.getParameterLoc(),
2059 diag::err_attribute_cleanup_arg_not_function)
2060 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002061 return;
2062 }
2063
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002064 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002065 S.Diag(Attr.getParameterLoc(),
2066 diag::err_attribute_cleanup_func_must_take_one_arg)
2067 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002068 return;
2069 }
Mike Stumpbf916502009-07-24 19:02:52 +00002070
Anders Carlsson89941c12009-02-07 23:16:50 +00002071 // We're currently more strict than GCC about what function types we accept.
2072 // If this ever proves to be a problem it should be easy to fix.
2073 QualType Ty = S.Context.getPointerType(VD->getType());
2074 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002075 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2076 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002077 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002078 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2079 Attr.getParameterName() << ParamTy << Ty;
2080 return;
2081 }
Mike Stumpbf916502009-07-24 19:02:52 +00002082
Chandler Carruth87c44602011-07-01 23:49:12 +00002083 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00002084 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002085}
2086
Mike Stumpbf916502009-07-24 19:02:52 +00002087/// Handle __attribute__((format_arg((idx)))) attribute based on
2088/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002089static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002090 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002091 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002092
Chandler Carruth87c44602011-07-01 23:49:12 +00002093 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002094 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002095 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002096 return;
2097 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002098
2099 // In C++ the implicit 'this' function parameter also counts, and they are
2100 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002101 bool HasImplicitThisParam = isInstanceMethod(D);
2102 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002103 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002104
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002105 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002106 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002107 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002108 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2109 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2111 << "format" << 2 << IdxExpr->getSourceRange();
2112 return;
2113 }
Mike Stumpbf916502009-07-24 19:02:52 +00002114
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002115 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2117 << "format" << 2 << IdxExpr->getSourceRange();
2118 return;
2119 }
Mike Stumpbf916502009-07-24 19:02:52 +00002120
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002121 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002122
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002123 if (HasImplicitThisParam) {
2124 if (ArgIdx == 0) {
2125 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2126 << "format_arg" << IdxExpr->getSourceRange();
2127 return;
2128 }
2129 ArgIdx--;
2130 }
2131
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002132 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002133 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002134
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002135 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2136 if (not_nsstring_type &&
2137 !isCFStringType(Ty, S.Context) &&
2138 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002139 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002140 // FIXME: Should highlight the actual expression that has the wrong type.
2141 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002142 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002143 << IdxExpr->getSourceRange();
2144 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002145 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002146 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002147 if (!isNSStringType(Ty, S.Context) &&
2148 !isCFStringType(Ty, S.Context) &&
2149 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002150 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002151 // FIXME: Should highlight the actual expression that has the wrong type.
2152 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002153 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002154 << IdxExpr->getSourceRange();
2155 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002156 }
2157
Chandler Carruth87c44602011-07-01 23:49:12 +00002158 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002159 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002160}
2161
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002162enum FormatAttrKind {
2163 CFStringFormat,
2164 NSStringFormat,
2165 StrftimeFormat,
2166 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002167 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002168 InvalidFormat
2169};
2170
2171/// getFormatAttrKind - Map from format attribute names to supported format
2172/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002173static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002174 // Check for formats that get handled specially.
2175 if (Format == "NSString")
2176 return NSStringFormat;
2177 if (Format == "CFString")
2178 return CFStringFormat;
2179 if (Format == "strftime")
2180 return StrftimeFormat;
2181
2182 // Otherwise, check for supported formats.
2183 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2184 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
2185 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002186 Format == "zcmn_err" ||
2187 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002188 return SupportedFormat;
2189
Duncan Sandsbc525952010-03-23 14:44:19 +00002190 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2191 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002192 return IgnoredFormat;
2193
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002194 return InvalidFormat;
2195}
2196
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002197/// Handle __attribute__((init_priority(priority))) attributes based on
2198/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002199static void handleInitPriorityAttr(Sema &S, Decl *D,
2200 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002201 if (!S.getLangOptions().CPlusPlus) {
2202 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2203 return;
2204 }
2205
Chandler Carruth87c44602011-07-01 23:49:12 +00002206 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002207 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2208 Attr.setInvalid();
2209 return;
2210 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002211 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002212 if (S.Context.getAsArrayType(T))
2213 T = S.Context.getBaseElementType(T);
2214 if (!T->getAs<RecordType>()) {
2215 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2216 Attr.setInvalid();
2217 return;
2218 }
2219
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002220 if (Attr.getNumArgs() != 1) {
2221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2222 Attr.setInvalid();
2223 return;
2224 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002225 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002226
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002227 llvm::APSInt priority(32);
2228 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2229 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2231 << "init_priority" << priorityExpr->getSourceRange();
2232 Attr.setInvalid();
2233 return;
2234 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002235 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002236 if (prioritynum < 101 || prioritynum > 65535) {
2237 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2238 << priorityExpr->getSourceRange();
2239 Attr.setInvalid();
2240 return;
2241 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002242 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002243 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002244}
2245
Mike Stumpbf916502009-07-24 19:02:52 +00002246/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2247/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002248static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002249
Chris Lattner545dd342008-06-28 23:36:30 +00002250 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002251 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002252 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002253 return;
2254 }
2255
Chris Lattner545dd342008-06-28 23:36:30 +00002256 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002258 return;
2259 }
2260
Chandler Carruth87c44602011-07-01 23:49:12 +00002261 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002262 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002263 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002264 return;
2265 }
2266
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002267 // In C++ the implicit 'this' function parameter also counts, and they are
2268 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002269 bool HasImplicitThisParam = isInstanceMethod(D);
2270 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002271 unsigned FirstIdx = 1;
2272
Chris Lattner5f9e2722011-07-23 10:55:15 +00002273 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002274
2275 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002276 if (Format.startswith("__") && Format.endswith("__"))
2277 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002278
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002279 // Check for supported formats.
2280 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002281
2282 if (Kind == IgnoredFormat)
2283 return;
2284
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002285 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002286 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002287 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002288 return;
2289 }
2290
2291 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002292 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002293 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002294 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2295 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002296 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002297 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002298 return;
2299 }
2300
2301 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002302 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002303 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002304 return;
2305 }
2306
2307 // FIXME: Do we need to bounds check?
2308 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002309
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002310 if (HasImplicitThisParam) {
2311 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002312 S.Diag(Attr.getLoc(),
2313 diag::err_format_attribute_implicit_this_format_string)
2314 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002315 return;
2316 }
2317 ArgIdx--;
2318 }
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Chris Lattner6b6b5372008-06-26 18:38:35 +00002320 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002321 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002322
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002323 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002324 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002325 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2326 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002327 return;
2328 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002329 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002330 // FIXME: do we need to check if the type is NSString*? What are the
2331 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002332 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002333 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002334 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2335 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002336 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002337 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002338 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002339 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002340 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002341 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2342 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002343 return;
2344 }
2345
2346 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002347 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002348 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002349 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2350 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002352 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002353 return;
2354 }
2355
2356 // check if the function is variadic if the 3rd argument non-zero
2357 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002358 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002359 ++NumArgs; // +1 for ...
2360 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002361 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002362 return;
2363 }
2364 }
2365
Chris Lattner3c73c412008-11-19 08:23:25 +00002366 // strftime requires FirstArg to be 0 because it doesn't read from any
2367 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002368 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002369 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002370 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2371 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002372 return;
2373 }
2374 // if 0 it disables parameter checking (to use with e.g. va_list)
2375 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002376 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002377 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002378 return;
2379 }
2380
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002381 // Check whether we already have an equivalent format attribute.
2382 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002383 i = D->specific_attr_begin<FormatAttr>(),
2384 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002385 i != e ; ++i) {
2386 FormatAttr *f = *i;
2387 if (f->getType() == Format &&
2388 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2389 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2390 // If we don't have a valid location for this attribute, adopt the
2391 // location.
2392 if (f->getLocation().isInvalid())
2393 f->setLocation(Attr.getLoc());
2394 return;
2395 }
2396 }
2397
Chandler Carruth87c44602011-07-01 23:49:12 +00002398 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002399 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002400 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002401}
2402
Chandler Carruth1b03c872011-07-02 00:01:44 +00002403static void handleTransparentUnionAttr(Sema &S, Decl *D,
2404 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002405 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002406 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002407 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002408
Chris Lattner6b6b5372008-06-26 18:38:35 +00002409
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002410 // Try to find the underlying union declaration.
2411 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002412 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002413 if (TD && TD->getUnderlyingType()->isUnionType())
2414 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2415 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002416 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002417
2418 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002420 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002421 return;
2422 }
2423
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002424 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002425 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002426 diag::warn_transparent_union_attribute_not_definition);
2427 return;
2428 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002429
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002430 RecordDecl::field_iterator Field = RD->field_begin(),
2431 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002432 if (Field == FieldEnd) {
2433 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2434 return;
2435 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002436
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002437 FieldDecl *FirstField = *Field;
2438 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002439 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002440 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002441 diag::warn_transparent_union_attribute_floating)
2442 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002443 return;
2444 }
2445
2446 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2447 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2448 for (; Field != FieldEnd; ++Field) {
2449 QualType FieldType = Field->getType();
2450 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2451 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2452 // Warn if we drop the attribute.
2453 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002454 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002455 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002456 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002457 diag::warn_transparent_union_attribute_field_size_align)
2458 << isSize << Field->getDeclName() << FieldBits;
2459 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002460 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002461 diag::note_transparent_union_first_field_size_align)
2462 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002463 return;
2464 }
2465 }
2466
Sean Huntcf807c42010-08-18 23:23:40 +00002467 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002468}
2469
Chandler Carruth1b03c872011-07-02 00:01:44 +00002470static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002471 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002472 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002473 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002474
Peter Collingbourne7a730022010-11-23 20:45:58 +00002475 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002476 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002477
Chris Lattner6b6b5372008-06-26 18:38:35 +00002478 // Make sure that there is a string literal as the annotation's single
2479 // argument.
2480 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002481 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002482 return;
2483 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002484 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002485 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002486}
2487
Chandler Carruth1b03c872011-07-02 00:01:44 +00002488static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002489 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002490 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002492 return;
2493 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002494
2495 //FIXME: The C++0x version of this attribute has more limited applicabilty
2496 // than GNU's, and should error out when it is used to specify a
2497 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002498
Chris Lattner545dd342008-06-28 23:36:30 +00002499 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002500 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002501 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002502 }
Mike Stumpbf916502009-07-24 19:02:52 +00002503
Peter Collingbourne7a730022010-11-23 20:45:58 +00002504 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002505}
2506
2507void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2508 if (E->isTypeDependent() || E->isValueDependent()) {
2509 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002510 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002511 return;
2512 }
2513
Sean Huntcf807c42010-08-18 23:23:40 +00002514 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002515 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002516 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2517 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2518 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002519 return;
2520 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002521 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002522 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2523 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002524 return;
2525 }
2526
Sean Huntcf807c42010-08-18 23:23:40 +00002527 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2528}
2529
2530void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2531 // FIXME: Cache the number on the Attr object if non-dependent?
2532 // FIXME: Perform checking of type validity
2533 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2534 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002535}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002536
Chandler Carruthd309c812011-07-01 23:49:16 +00002537/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002538/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002539///
Mike Stumpbf916502009-07-24 19:02:52 +00002540/// Despite what would be logical, the mode attribute is a decl attribute, not a
2541/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2542/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002543static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002544 // This attribute isn't documented, but glibc uses it. It changes
2545 // the width of an int or unsigned int to the specified size.
2546
2547 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002548 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002549 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002550
Chris Lattnerfbf13472008-06-27 22:18:37 +00002551
2552 IdentifierInfo *Name = Attr.getParameterName();
2553 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002555 return;
2556 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002557
Chris Lattner5f9e2722011-07-23 10:55:15 +00002558 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002559
2560 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002561 if (Str.startswith("__") && Str.endswith("__"))
2562 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002563
2564 unsigned DestWidth = 0;
2565 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002566 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002567 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002568 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002569 switch (Str[0]) {
2570 case 'Q': DestWidth = 8; break;
2571 case 'H': DestWidth = 16; break;
2572 case 'S': DestWidth = 32; break;
2573 case 'D': DestWidth = 64; break;
2574 case 'X': DestWidth = 96; break;
2575 case 'T': DestWidth = 128; break;
2576 }
2577 if (Str[1] == 'F') {
2578 IntegerMode = false;
2579 } else if (Str[1] == 'C') {
2580 IntegerMode = false;
2581 ComplexMode = true;
2582 } else if (Str[1] != 'I') {
2583 DestWidth = 0;
2584 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002585 break;
2586 case 4:
2587 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2588 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002589 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002590 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002591 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002592 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002593 break;
2594 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002595 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002596 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002597 break;
2598 }
2599
2600 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002601 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002602 OldTy = TD->getUnderlyingType();
2603 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2604 OldTy = VD->getType();
2605 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002606 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2607 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002608 return;
2609 }
Eli Friedman73397492009-03-03 06:41:03 +00002610
John McCall183700f2009-09-21 23:43:11 +00002611 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002612 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2613 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002614 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002615 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2616 } else if (ComplexMode) {
2617 if (!OldTy->isComplexType())
2618 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2619 } else {
2620 if (!OldTy->isFloatingType())
2621 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2622 }
2623
Mike Stump390b4cc2009-05-16 07:39:55 +00002624 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2625 // and friends, at least with glibc.
2626 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2627 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002628 // FIXME: Make sure floating-point mappings are accurate
2629 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002630 QualType NewTy;
2631 switch (DestWidth) {
2632 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002633 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002634 return;
2635 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002636 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002637 return;
2638 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002639 if (!IntegerMode) {
2640 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2641 return;
2642 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002643 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002644 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002645 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002646 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002647 break;
2648 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002649 if (!IntegerMode) {
2650 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2651 return;
2652 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002653 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002654 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002655 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002656 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002657 break;
2658 case 32:
2659 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002660 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002661 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002662 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002663 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002664 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002665 break;
2666 case 64:
2667 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002668 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002669 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002670 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002671 NewTy = S.Context.LongTy;
2672 else
2673 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002674 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002675 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002676 NewTy = S.Context.UnsignedLongTy;
2677 else
2678 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002679 break;
Eli Friedman73397492009-03-03 06:41:03 +00002680 case 96:
2681 NewTy = S.Context.LongDoubleTy;
2682 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002683 case 128:
2684 if (!IntegerMode) {
2685 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2686 return;
2687 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002688 if (OldTy->isSignedIntegerType())
2689 NewTy = S.Context.Int128Ty;
2690 else
2691 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002692 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002693 }
2694
Eli Friedman73397492009-03-03 06:41:03 +00002695 if (ComplexMode) {
2696 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002697 }
2698
2699 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002700 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002701 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002702 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002703 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002704 cast<ValueDecl>(D)->setType(NewTy);
2705}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002706
Chandler Carruth1b03c872011-07-02 00:01:44 +00002707static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002708 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002709 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002710 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002711
Chandler Carruth87c44602011-07-01 23:49:12 +00002712 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002713 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002714 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002715 return;
2716 }
Mike Stumpbf916502009-07-24 19:02:52 +00002717
Chandler Carruth87c44602011-07-01 23:49:12 +00002718 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002719}
2720
Chandler Carruth1b03c872011-07-02 00:01:44 +00002721static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002722 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002723 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002724 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002725
Mike Stumpbf916502009-07-24 19:02:52 +00002726
Chandler Carruth87c44602011-07-01 23:49:12 +00002727 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002728 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002729 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002730 return;
2731 }
Mike Stumpbf916502009-07-24 19:02:52 +00002732
Chandler Carruth87c44602011-07-01 23:49:12 +00002733 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002734}
2735
Chandler Carruth1b03c872011-07-02 00:01:44 +00002736static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2737 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002738 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002739 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002740 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002741
Chris Lattner7255a2d2010-06-22 00:03:40 +00002742
Chandler Carruth87c44602011-07-01 23:49:12 +00002743 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002745 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002746 return;
2747 }
2748
Chandler Carruth87c44602011-07-01 23:49:12 +00002749 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002750 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002751}
2752
Chandler Carruth1b03c872011-07-02 00:01:44 +00002753static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002754 if (S.LangOpts.CUDA) {
2755 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002756 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2758 return;
2759 }
2760
Chandler Carruth87c44602011-07-01 23:49:12 +00002761 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002763 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002764 return;
2765 }
2766
Chandler Carruth87c44602011-07-01 23:49:12 +00002767 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002768 } else {
2769 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2770 }
2771}
2772
Chandler Carruth1b03c872011-07-02 00:01:44 +00002773static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002774 if (S.LangOpts.CUDA) {
2775 // check the attribute arguments.
2776 if (Attr.getNumArgs() != 0) {
2777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2778 return;
2779 }
2780
Chandler Carruth87c44602011-07-01 23:49:12 +00002781 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002783 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002784 return;
2785 }
2786
Chandler Carruth87c44602011-07-01 23:49:12 +00002787 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002788 } else {
2789 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2790 }
2791}
2792
Chandler Carruth1b03c872011-07-02 00:01:44 +00002793static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002794 if (S.LangOpts.CUDA) {
2795 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002796 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002797 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002798
Chandler Carruth87c44602011-07-01 23:49:12 +00002799 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002800 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002801 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002802 return;
2803 }
2804
Chandler Carruth87c44602011-07-01 23:49:12 +00002805 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002806 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002807 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002808 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2809 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2810 << FD->getType()
2811 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2812 "void");
2813 } else {
2814 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2815 << FD->getType();
2816 }
2817 return;
2818 }
2819
Chandler Carruth87c44602011-07-01 23:49:12 +00002820 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002821 } else {
2822 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2823 }
2824}
2825
Chandler Carruth1b03c872011-07-02 00:01:44 +00002826static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002827 if (S.LangOpts.CUDA) {
2828 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002829 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002830 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002831
Peter Collingbourneced76712010-12-01 03:15:31 +00002832
Chandler Carruth87c44602011-07-01 23:49:12 +00002833 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002835 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002836 return;
2837 }
2838
Chandler Carruth87c44602011-07-01 23:49:12 +00002839 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002840 } else {
2841 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2842 }
2843}
2844
Chandler Carruth1b03c872011-07-02 00:01:44 +00002845static void handleSharedAttr(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<VarDecl>(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() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002855 return;
2856 }
2857
Chandler Carruth87c44602011-07-01 23:49:12 +00002858 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002859 } else {
2860 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2861 }
2862}
2863
Chandler Carruth1b03c872011-07-02 00:01:44 +00002864static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002865 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002866 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002867 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002868
Chandler Carruth87c44602011-07-01 23:49:12 +00002869 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002870 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002872 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002873 return;
2874 }
Mike Stumpbf916502009-07-24 19:02:52 +00002875
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002876 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002877 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002878 return;
2879 }
Mike Stumpbf916502009-07-24 19:02:52 +00002880
Chandler Carruth87c44602011-07-01 23:49:12 +00002881 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002882}
2883
Chandler Carruth1b03c872011-07-02 00:01:44 +00002884static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002885 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002886
Chandler Carruth87c44602011-07-01 23:49:12 +00002887 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002888 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2889 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002890 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002891 return;
2892
Chandler Carruth87c44602011-07-01 23:49:12 +00002893 if (!isa<ObjCMethodDecl>(D)) {
2894 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2895 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002896 return;
2897 }
2898
Chandler Carruth87c44602011-07-01 23:49:12 +00002899 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002900 case AttributeList::AT_fastcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002901 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002902 return;
2903 case AttributeList::AT_stdcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002904 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002905 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002906 case AttributeList::AT_thiscall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002907 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002908 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002909 case AttributeList::AT_cdecl:
Chandler Carruth87c44602011-07-01 23:49:12 +00002910 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002911 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002912 case AttributeList::AT_pascal:
Chandler Carruth87c44602011-07-01 23:49:12 +00002913 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002914 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002915 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002916 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002917 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002918 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002919 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002920 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002921 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002922 return;
2923 }
2924
Chris Lattner5f9e2722011-07-23 10:55:15 +00002925 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002926 PcsAttr::PCSType PCS;
2927 if (StrRef == "aapcs")
2928 PCS = PcsAttr::AAPCS;
2929 else if (StrRef == "aapcs-vfp")
2930 PCS = PcsAttr::AAPCS_VFP;
2931 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002932 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2933 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002934 return;
2935 }
2936
Chandler Carruth87c44602011-07-01 23:49:12 +00002937 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002938 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002939 default:
2940 llvm_unreachable("unexpected attribute kind");
2941 return;
2942 }
2943}
2944
Chandler Carruth1b03c872011-07-02 00:01:44 +00002945static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00002946 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00002947 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002948}
2949
John McCall711c52b2011-01-05 12:14:39 +00002950bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2951 if (attr.isInvalid())
2952 return true;
2953
Ted Kremenek831efae2011-04-15 05:49:29 +00002954 if ((attr.getNumArgs() != 0 &&
2955 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2956 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002957 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2958 attr.setInvalid();
2959 return true;
2960 }
2961
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002962 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2963 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002964 switch (attr.getKind()) {
2965 case AttributeList::AT_cdecl: CC = CC_C; break;
2966 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2967 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2968 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2969 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002970 case AttributeList::AT_pcs: {
2971 Expr *Arg = attr.getArg(0);
2972 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002973 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002974 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2975 << "pcs" << 1;
2976 attr.setInvalid();
2977 return true;
2978 }
2979
Chris Lattner5f9e2722011-07-23 10:55:15 +00002980 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002981 if (StrRef == "aapcs") {
2982 CC = CC_AAPCS;
2983 break;
2984 } else if (StrRef == "aapcs-vfp") {
2985 CC = CC_AAPCS_VFP;
2986 break;
2987 }
2988 // FALLS THROUGH
2989 }
John McCall711c52b2011-01-05 12:14:39 +00002990 default: llvm_unreachable("unexpected attribute kind"); return true;
2991 }
2992
2993 return false;
2994}
2995
Chandler Carruth1b03c872011-07-02 00:01:44 +00002996static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002997 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00002998
2999 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003000 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003001 return;
3002
Chandler Carruth87c44602011-07-01 23:49:12 +00003003 if (!isa<ObjCMethodDecl>(D)) {
3004 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3005 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003006 return;
3007 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003008
Chandler Carruth87c44602011-07-01 23:49:12 +00003009 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003010}
3011
3012/// Checks a regparm attribute, returning true if it is ill-formed and
3013/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003014bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3015 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003016 return true;
3017
Chandler Carruth87c44602011-07-01 23:49:12 +00003018 if (Attr.getNumArgs() != 1) {
3019 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3020 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003021 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003022 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003023
Chandler Carruth87c44602011-07-01 23:49:12 +00003024 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003025 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003026 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003027 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003028 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003029 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003030 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003031 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003032 }
3033
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003034 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003035 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003036 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003037 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003038 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003039 }
3040
John McCall711c52b2011-01-05 12:14:39 +00003041 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003042 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003043 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003044 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003045 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003046 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003047 }
3048
John McCall711c52b2011-01-05 12:14:39 +00003049 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003050}
3051
Chandler Carruth1b03c872011-07-02 00:01:44 +00003052static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003053 if (S.LangOpts.CUDA) {
3054 // check the attribute arguments.
3055 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003056 // FIXME: 0 is not okay.
3057 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003058 return;
3059 }
3060
Chandler Carruth87c44602011-07-01 23:49:12 +00003061 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003062 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003063 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003064 return;
3065 }
3066
3067 Expr *MaxThreadsExpr = Attr.getArg(0);
3068 llvm::APSInt MaxThreads(32);
3069 if (MaxThreadsExpr->isTypeDependent() ||
3070 MaxThreadsExpr->isValueDependent() ||
3071 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3072 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3073 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3074 return;
3075 }
3076
3077 llvm::APSInt MinBlocks(32);
3078 if (Attr.getNumArgs() > 1) {
3079 Expr *MinBlocksExpr = Attr.getArg(1);
3080 if (MinBlocksExpr->isTypeDependent() ||
3081 MinBlocksExpr->isValueDependent() ||
3082 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3083 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3084 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3085 return;
3086 }
3087 }
3088
Chandler Carruth87c44602011-07-01 23:49:12 +00003089 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003090 MaxThreads.getZExtValue(),
3091 MinBlocks.getZExtValue()));
3092 } else {
3093 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3094 }
3095}
3096
Chris Lattner0744e5f2008-06-29 00:23:49 +00003097//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003098// Checker-specific attribute handlers.
3099//===----------------------------------------------------------------------===//
3100
John McCallc7ad3812011-01-25 03:31:58 +00003101static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
3102 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
3103}
3104static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
3105 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
3106}
3107
Chandler Carruth1b03c872011-07-02 00:01:44 +00003108static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003109 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003110 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003111 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3112 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003113 return;
3114 }
3115
3116 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003117 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003118 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3119 cf = false;
3120 } else {
3121 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3122 cf = true;
3123 }
3124
3125 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003126 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3127 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003128 return;
3129 }
3130
3131 if (cf)
Chandler Carruth87c44602011-07-01 23:49:12 +00003132 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003133 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003134 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003135}
3136
Chandler Carruth1b03c872011-07-02 00:01:44 +00003137static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3138 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003139 if (!isa<ObjCMethodDecl>(D)) {
3140 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3141 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003142 return;
3143 }
3144
Chandler Carruth87c44602011-07-01 23:49:12 +00003145 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003146}
3147
Chandler Carruth1b03c872011-07-02 00:01:44 +00003148static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3149 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003150
John McCallc7ad3812011-01-25 03:31:58 +00003151 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003152
Chandler Carruth87c44602011-07-01 23:49:12 +00003153 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003154 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003155 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003156 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003157 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
3158 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003159 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003160 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003161 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003162 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003163 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3164 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003165 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003166 return;
3167 }
Mike Stumpbf916502009-07-24 19:02:52 +00003168
John McCallc7ad3812011-01-25 03:31:58 +00003169 bool typeOK;
3170 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003171 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00003172 default: llvm_unreachable("invalid ownership attribute"); return;
3173 case AttributeList::AT_ns_returns_autoreleased:
3174 case AttributeList::AT_ns_returns_retained:
3175 case AttributeList::AT_ns_returns_not_retained:
3176 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3177 cf = false;
3178 break;
3179
3180 case AttributeList::AT_cf_returns_retained:
3181 case AttributeList::AT_cf_returns_not_retained:
3182 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3183 cf = true;
3184 break;
3185 }
3186
3187 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003188 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3189 << SourceRange(Attr.getLoc())
3190 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003191 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003192 }
Mike Stumpbf916502009-07-24 19:02:52 +00003193
Chandler Carruth87c44602011-07-01 23:49:12 +00003194 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003195 default:
3196 assert(0 && "invalid ownership attribute");
3197 return;
John McCallc7ad3812011-01-25 03:31:58 +00003198 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruth87c44602011-07-01 23:49:12 +00003199 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCallc7ad3812011-01-25 03:31:58 +00003200 S.Context));
3201 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003202 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003203 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003204 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003205 return;
3206 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003207 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003208 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003209 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003210 case AttributeList::AT_cf_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003211 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003212 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003213 return;
3214 case AttributeList::AT_ns_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003215 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003216 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003217 return;
3218 };
3219}
3220
John McCalldc7c5ad2011-07-22 08:53:00 +00003221static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3222 const AttributeList &attr) {
3223 SourceLocation loc = attr.getLoc();
3224
3225 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3226
3227 if (!isa<ObjCMethodDecl>(method)) {
3228 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3229 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
3230 return;
3231 }
3232
3233 // Check that the method returns a normal pointer.
3234 QualType resultType = method->getResultType();
3235 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
3236 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3237 << SourceRange(loc)
3238 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3239
3240 // Drop the attribute.
3241 return;
3242 }
3243
3244 method->addAttr(
3245 ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context));
3246}
3247
Chandler Carruth1b03c872011-07-02 00:01:44 +00003248static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3249 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003250 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003251
Chandler Carruth87c44602011-07-01 23:49:12 +00003252 SourceLocation L = Attr.getLoc();
3253 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3254 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003255}
3256
Chandler Carruth1b03c872011-07-02 00:01:44 +00003257static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3258 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003259 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
3260 SourceLocation L = Attr.getLoc();
3261 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3262 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003263 return;
3264 }
3265
Chandler Carruth87c44602011-07-01 23:49:12 +00003266 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003267 QualType type = vd->getType();
3268
3269 if (!type->isDependentType() &&
3270 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003271 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003272 << type;
3273 return;
3274 }
3275
3276 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3277
3278 // If we have no lifetime yet, check the lifetime we're presumably
3279 // going to infer.
3280 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3281 lifetime = type->getObjCARCImplicitLifetime();
3282
3283 switch (lifetime) {
3284 case Qualifiers::OCL_None:
3285 assert(type->isDependentType() &&
3286 "didn't infer lifetime for non-dependent type?");
3287 break;
3288
3289 case Qualifiers::OCL_Weak: // meaningful
3290 case Qualifiers::OCL_Strong: // meaningful
3291 break;
3292
3293 case Qualifiers::OCL_ExplicitNone:
3294 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003295 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003296 << (lifetime == Qualifiers::OCL_Autoreleasing);
3297 break;
3298 }
3299
Chandler Carruth87c44602011-07-01 23:49:12 +00003300 D->addAttr(::new (S.Context)
3301 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003302}
3303
Charles Davisf0122fe2010-02-16 18:27:26 +00003304static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3305 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00003306 Attr.getKind() == AttributeList::AT_dllexport ||
3307 Attr.getKind() == AttributeList::AT_uuid;
3308}
3309
3310//===----------------------------------------------------------------------===//
3311// Microsoft specific attribute handlers.
3312//===----------------------------------------------------------------------===//
3313
Chandler Carruth1b03c872011-07-02 00:01:44 +00003314static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet11542142010-12-19 06:50:37 +00003315 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
3316 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003317 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003318 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003319
Francois Pichet11542142010-12-19 06:50:37 +00003320 Expr *Arg = Attr.getArg(0);
3321 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003322 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003323 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3324 << "uuid" << 1;
3325 return;
3326 }
3327
Chris Lattner5f9e2722011-07-23 10:55:15 +00003328 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003329
3330 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3331 StrRef.back() == '}';
3332
3333 // Validate GUID length.
3334 if (IsCurly && StrRef.size() != 38) {
3335 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3336 return;
3337 }
3338 if (!IsCurly && StrRef.size() != 36) {
3339 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3340 return;
3341 }
3342
3343 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3344 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003345 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003346 if (IsCurly) // Skip the optional '{'
3347 ++I;
3348
3349 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003350 if (i == 8 || i == 13 || i == 18 || i == 23) {
3351 if (*I != '-') {
3352 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3353 return;
3354 }
3355 } else if (!isxdigit(*I)) {
3356 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3357 return;
3358 }
3359 I++;
3360 }
Francois Pichet11542142010-12-19 06:50:37 +00003361
Chandler Carruth87c44602011-07-01 23:49:12 +00003362 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003363 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003364 } else
Francois Pichet11542142010-12-19 06:50:37 +00003365 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003366}
3367
Ted Kremenekb71368d2009-05-09 02:44:38 +00003368//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003369// Top Level Sema Entry Points
3370//===----------------------------------------------------------------------===//
3371
Chandler Carruth1b03c872011-07-02 00:01:44 +00003372static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3373 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003374 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003375 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3376 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3377 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003378 default:
3379 break;
3380 }
3381}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003382
Chandler Carruth1b03c872011-07-02 00:01:44 +00003383static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3384 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003385 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003386 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3387 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00003388 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003389 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003390 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003391 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003392 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003393 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003394 case AttributeList::AT_neon_vector_type:
3395 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003396 // Ignore these, these are type attributes, handled by
3397 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003398 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003399 case AttributeList::AT_device:
3400 case AttributeList::AT_host:
3401 case AttributeList::AT_overloadable:
3402 // Ignore, this is a non-inheritable attribute, handled
3403 // by ProcessNonInheritableDeclAttr.
3404 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003405 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3406 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003407 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003408 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003409 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003410 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3411 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3412 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003413 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003414 handleDependencyAttr (S, D, Attr); break;
3415 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3416 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3417 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3418 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3419 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003420 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003421 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003422 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003423 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3424 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3425 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3426 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003427 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003428 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003429 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003430 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3431 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3432 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3433 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3434 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003435 case AttributeList::AT_ownership_returns:
3436 case AttributeList::AT_ownership_takes:
3437 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003438 handleOwnershipAttr (S, D, Attr); break;
3439 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3440 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3441 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3442 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3443 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003444
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003445 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003446 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003447 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003448 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003449
John McCalldc7c5ad2011-07-22 08:53:00 +00003450 case AttributeList::AT_objc_returns_inner_pointer:
3451 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3452
Ted Kremenekb71368d2009-05-09 02:44:38 +00003453 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003454 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003455 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003456 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003457 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003458
3459 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003460 case AttributeList::AT_ns_returns_not_retained:
3461 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003462 case AttributeList::AT_ns_returns_retained:
3463 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003464 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003465
Nate Begeman6f3d8382009-06-26 06:32:41 +00003466 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003467 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003468
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003469 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003470 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003471
Chandler Carruth1b03c872011-07-02 00:01:44 +00003472 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3473 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3474 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3475 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003476 case AttributeList::AT_arc_weakref_unavailable:
3477 handleArcWeakrefUnavailableAttr (S, D, Attr);
3478 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003479 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3480 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3481 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3482 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003483 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003484 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3485 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3486 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003487 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003488 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003489 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003490 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003491 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003492 break;
John McCalld5313b02011-03-02 11:33:24 +00003493 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003494 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003495 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003496 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3497 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3498 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3499 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3500 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3501 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3502 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3503 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3504 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003505 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003506 // Just ignore
3507 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003508 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003509 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003510 break;
John McCall04a67a62010-02-05 21:31:56 +00003511 case AttributeList::AT_stdcall:
3512 case AttributeList::AT_cdecl:
3513 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003514 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003515 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003516 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003517 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003518 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003519 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003520 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003521 break;
Francois Pichet11542142010-12-19 06:50:37 +00003522 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003523 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003524 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003525
3526 // Thread safety attributes:
3527 case AttributeList::AT_guarded_var:
3528 handleGuardedVarAttr(S, D, Attr);
3529 break;
3530 case AttributeList::AT_pt_guarded_var:
3531 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3532 break;
3533 case AttributeList::AT_scoped_lockable:
3534 handleLockableAttr(S, D, Attr, /*scoped = */true);
3535 break;
3536 case AttributeList::AT_no_thread_safety_analysis:
3537 handleNoThreadSafetyAttr(S, D, Attr);
3538 break;
3539 case AttributeList::AT_lockable:
3540 handleLockableAttr(S, D, Attr);
3541 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003542 case AttributeList::AT_guarded_by:
3543 handleGuardedByAttr(S, D, Attr);
3544 break;
3545 case AttributeList::AT_pt_guarded_by:
3546 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3547 break;
3548 case AttributeList::AT_exclusive_lock_function:
3549 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3550 break;
3551 case AttributeList::AT_exclusive_locks_required:
3552 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3553 break;
3554 case AttributeList::AT_exclusive_trylock_function:
3555 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3556 break;
3557 case AttributeList::AT_lock_returned:
3558 handleLockReturnedAttr(S, D, Attr);
3559 break;
3560 case AttributeList::AT_locks_excluded:
3561 handleLocksExcludedAttr(S, D, Attr);
3562 break;
3563 case AttributeList::AT_shared_lock_function:
3564 handleLockFunAttr(S, D, Attr);
3565 break;
3566 case AttributeList::AT_shared_locks_required:
3567 handleLocksRequiredAttr(S, D, Attr);
3568 break;
3569 case AttributeList::AT_shared_trylock_function:
3570 handleTrylockFunAttr(S, D, Attr);
3571 break;
3572 case AttributeList::AT_unlock_function:
3573 handleUnlockFunAttr(S, D, Attr);
3574 break;
3575 case AttributeList::AT_acquired_before:
3576 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3577 break;
3578 case AttributeList::AT_acquired_after:
3579 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3580 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003581
Chris Lattner803d0802008-06-29 00:43:07 +00003582 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003583 // Ask target about the attribute.
3584 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3585 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003586 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3587 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003588 break;
3589 }
3590}
3591
Peter Collingbourne60700392011-01-21 02:08:45 +00003592/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3593/// the attribute applies to decls. If the attribute is a type attribute, just
3594/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3595/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003596static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3597 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003598 bool NonInheritable, bool Inheritable) {
3599 if (Attr.isInvalid())
3600 return;
3601
3602 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3603 // FIXME: Try to deal with other __declspec attributes!
3604 return;
3605
3606 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003607 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003608
3609 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003610 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003611}
3612
Chris Lattner803d0802008-06-29 00:43:07 +00003613/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3614/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003615void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003616 const AttributeList *AttrList,
3617 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003618 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003619 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003620 }
3621
3622 // GCC accepts
3623 // static int a9 __attribute__((weakref));
3624 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003625 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003626 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003627 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003628 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003629 }
3630}
3631
Ryan Flynne25ff832009-07-30 03:15:39 +00003632/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3633/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003634NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003635 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003636 NamedDecl *NewD = 0;
3637 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3638 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003639 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003640 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003641 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003642 if (FD->getQualifier()) {
3643 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003644 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003645 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003646 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3647 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003648 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003649 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003650 VD->getStorageClass(),
3651 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003652 if (VD->getQualifier()) {
3653 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003654 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003655 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003656 }
3657 return NewD;
3658}
3659
3660/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3661/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003662void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003663 if (W.getUsed()) return; // only do this once
3664 W.setUsed(true);
3665 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3666 IdentifierInfo *NDId = ND->getIdentifier();
3667 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003668 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3669 NDId->getName()));
3670 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003671 WeakTopLevelDecl.push_back(NewD);
3672 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3673 // to insert Decl at TU scope, sorry.
3674 DeclContext *SavedContext = CurContext;
3675 CurContext = Context.getTranslationUnitDecl();
3676 PushOnScopeChains(NewD, S);
3677 CurContext = SavedContext;
3678 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003679 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003680 }
3681}
3682
Chris Lattner0744e5f2008-06-29 00:23:49 +00003683/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3684/// it, apply them to D. This is a bit tricky because PD can have attributes
3685/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003686void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3687 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003688 // It's valid to "forward-declare" #pragma weak, in which case we
3689 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003690 if (Inheritable) {
3691 LoadExternalWeakUndeclaredIdentifiers();
3692 if (!WeakUndeclaredIdentifiers.empty()) {
3693 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3694 if (IdentifierInfo *Id = ND->getIdentifier()) {
3695 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3696 = WeakUndeclaredIdentifiers.find(Id);
3697 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3698 WeakInfo W = I->second;
3699 DeclApplyPragmaWeak(S, ND, W);
3700 WeakUndeclaredIdentifiers[Id] = W;
3701 }
John McCalld4aff0e2010-10-27 00:59:00 +00003702 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003703 }
3704 }
3705 }
3706
Chris Lattner0744e5f2008-06-29 00:23:49 +00003707 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003708 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003709 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003710
Chris Lattner0744e5f2008-06-29 00:23:49 +00003711 // Walk the declarator structure, applying decl attributes that were in a type
3712 // position to the decl itself. This handles cases like:
3713 // int *__attr__(x)** D;
3714 // when X is a decl attribute.
3715 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3716 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003717 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003718
Chris Lattner0744e5f2008-06-29 00:23:49 +00003719 // Finally, apply any attributes on the decl itself.
3720 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003721 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003722}
John McCall54abf7d2009-11-04 02:18:39 +00003723
John McCallf85e1932011-06-15 23:02:42 +00003724/// Is the given declaration allowed to use a forbidden type?
3725static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3726 // Private ivars are always okay. Unfortunately, people don't
3727 // always properly make their ivars private, even in system headers.
3728 // Plus we need to make fields okay, too.
3729 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3730 return false;
3731
3732 // Require it to be declared in a system header.
3733 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3734}
3735
3736/// Handle a delayed forbidden-type diagnostic.
3737static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3738 Decl *decl) {
3739 if (decl && isForbiddenTypeAllowed(S, decl)) {
3740 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3741 "this system declaration uses an unsupported type"));
3742 return;
3743 }
3744
3745 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3746 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3747 diag.Triggered = true;
3748}
3749
John McCalleee1d542011-02-14 07:13:47 +00003750// This duplicates a vector push_back but hides the need to know the
3751// size of the type.
3752void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3753 assert(StackSize <= StackCapacity);
3754
3755 // Grow the stack if necessary.
3756 if (StackSize == StackCapacity) {
3757 unsigned newCapacity = 2 * StackCapacity + 2;
3758 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3759 const char *oldBuffer = (const char*) Stack;
3760
3761 if (StackCapacity)
3762 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3763
3764 delete[] oldBuffer;
3765 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3766 StackCapacity = newCapacity;
3767 }
3768
3769 assert(StackSize < StackCapacity);
3770 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003771}
3772
John McCalleee1d542011-02-14 07:13:47 +00003773void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3774 Decl *decl) {
3775 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003776
John McCalleee1d542011-02-14 07:13:47 +00003777 // Check the invariants.
3778 assert(DD.StackSize >= state.SavedStackSize);
3779 assert(state.SavedStackSize >= DD.ActiveStackBase);
3780 assert(DD.ParsingDepth > 0);
3781
3782 // Drop the parsing depth.
3783 DD.ParsingDepth--;
3784
3785 // If there are no active diagnostics, we're done.
3786 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003787 return;
3788
John McCall2f514482010-01-27 03:50:35 +00003789 // We only want to actually emit delayed diagnostics when we
3790 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003791 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003792 // We emit all the active diagnostics, not just those starting
3793 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003794 // decl spec and another for each declarator; in a decl group like:
3795 // deprecated_typedef foo, *bar, baz();
3796 // only the declarator pops will be passed decls. This is correct;
3797 // we really do need to consider delayed diagnostics from the decl spec
3798 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003799 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3800 DelayedDiagnostic &diag = DD.Stack[i];
3801 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003802 continue;
3803
John McCalleee1d542011-02-14 07:13:47 +00003804 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003805 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003806 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003807 break;
3808
3809 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003810 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003811 break;
John McCallf85e1932011-06-15 23:02:42 +00003812
3813 case DelayedDiagnostic::ForbiddenType:
3814 handleDelayedForbiddenType(S, diag, decl);
3815 break;
John McCall2f514482010-01-27 03:50:35 +00003816 }
3817 }
3818 }
3819
John McCall58e6f342010-03-16 05:22:47 +00003820 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003821 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003822 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003823
John McCalleee1d542011-02-14 07:13:47 +00003824 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003825}
3826
3827static bool isDeclDeprecated(Decl *D) {
3828 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003829 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003830 return true;
3831 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3832 return false;
3833}
3834
John McCall9c3087b2010-08-26 02:13:20 +00003835void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003836 Decl *Ctx) {
3837 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003838 return;
3839
John McCall2f514482010-01-27 03:50:35 +00003840 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003841 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003842 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003843 << DD.getDeprecationDecl()->getDeclName()
3844 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003845 else
3846 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003847 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003848}
3849
Chris Lattner5f9e2722011-07-23 10:55:15 +00003850void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003851 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003852 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003853 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003854 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3855 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003856 return;
3857 }
3858
3859 // Otherwise, don't warn if our current context is deprecated.
3860 if (isDeclDeprecated(cast<Decl>(CurContext)))
3861 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003862 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003863 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3864 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003865 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003866 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003867 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003868 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003869 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003870 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3871 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003872 }
John McCall54abf7d2009-11-04 02:18:39 +00003873}