blob: e06d637eeb7ec10cc1480d3b052b883a94343ebc [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000022#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000024#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000025#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000027using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000028using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029
John McCall883cc2c2011-03-02 12:29:23 +000030/// These constants match the enumerated choices of
31/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000032enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000033 ExpectedFunction,
34 ExpectedUnion,
35 ExpectedVariableOrFunction,
36 ExpectedFunctionOrMethod,
37 ExpectedParameter,
38 ExpectedParameterOrMethod,
39 ExpectedFunctionMethodOrBlock,
40 ExpectedClassOrVirtualMethod,
41 ExpectedFunctionMethodOrParameter,
42 ExpectedClass,
43 ExpectedVirtualMethod,
44 ExpectedClassMember,
45 ExpectedVariable,
46 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000047 ExpectedVariableFunctionOrLabel,
48 ExpectedFieldOrGlobalVar
John McCall883cc2c2011-03-02 12:29:23 +000049};
50
Chris Lattnere5c5ee12008-06-29 00:16:31 +000051//===----------------------------------------------------------------------===//
52// Helper functions
53//===----------------------------------------------------------------------===//
54
Chandler Carruth87c44602011-07-01 23:49:12 +000055static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000056 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000057 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000058 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000059 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000060 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000061 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000062 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000063 Ty = decl->getUnderlyingType();
64 else
65 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000066
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000068 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000069 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000070 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000071
John McCall183700f2009-09-21 23:43:11 +000072 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000073}
74
Daniel Dunbar35682492008-09-26 04:12:28 +000075// FIXME: We should provide an abstraction around a method or function
76// to provide the following bits of information.
77
Nuno Lopesd20254f2009-12-20 23:11:08 +000078/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000079/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000080static bool isFunction(const Decl *D) {
81 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000082}
83
84/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000085/// type (function or function-typed variable) or an Objective-C
86/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000087static bool isFunctionOrMethod(const Decl *D) {
88 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000089}
90
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000091/// isFunctionOrMethodOrBlock - Return true if the given decl has function
92/// type (function or function-typed variable) or an Objective-C
93/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000094static bool isFunctionOrMethodOrBlock(const Decl *D) {
95 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000096 return true;
97 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000098 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000099 QualType Ty = V->getType();
100 return Ty->isBlockPointerType();
101 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000102 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000103}
104
John McCall711c52b2011-01-05 12:14:39 +0000105/// Return true if the given decl has a declarator that should have
106/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000107static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000108 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000109 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
110 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000111}
112
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000113/// hasFunctionProto - Return true if the given decl has a argument
114/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000115/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000116static bool hasFunctionProto(const Decl *D) {
117 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000119 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000120 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000121 return true;
122 }
123}
124
125/// getFunctionOrMethodNumArgs - Return number of function or method
126/// arguments. It is an error to call this on a K&R function (use
127/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000128static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
129 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000130 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000131 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000132 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000133 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000134}
135
Chandler Carruth87c44602011-07-01 23:49:12 +0000136static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
137 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000138 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000140 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000141
Chandler Carruth87c44602011-07-01 23:49:12 +0000142 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000143}
144
Chandler Carruth87c44602011-07-01 23:49:12 +0000145static QualType getFunctionOrMethodResultType(const Decl *D) {
146 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000147 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000148 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000149}
150
Chandler Carruth87c44602011-07-01 23:49:12 +0000151static bool isFunctionOrMethodVariadic(const Decl *D) {
152 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000153 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000154 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000155 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000156 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000157 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000158 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000159 }
160}
161
Chandler Carruth87c44602011-07-01 23:49:12 +0000162static bool isInstanceMethod(const Decl *D) {
163 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000164 return MethodDecl->isInstance();
165 return false;
166}
167
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000169 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000170 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000171 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000172
John McCall506b57e2010-05-17 21:00:27 +0000173 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
174 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000176
John McCall506b57e2010-05-17 21:00:27 +0000177 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000178
Chris Lattner6b6b5372008-06-26 18:38:35 +0000179 // FIXME: Should we walk the chain of classes?
180 return ClsName == &Ctx.Idents.get("NSString") ||
181 ClsName == &Ctx.Idents.get("NSMutableString");
182}
183
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000184static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000185 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000186 if (!PT)
187 return false;
188
Ted Kremenek6217b802009-07-29 21:53:49 +0000189 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 if (!RT)
191 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000192
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000193 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000194 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000195 return false;
196
197 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
198}
199
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000200/// \brief Check if the attribute has exactly as many args as Num. May
201/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000202static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
203 unsigned int Num) {
204 if (Attr.getNumArgs() != Num) {
205 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
206 return false;
207 }
208
209 return true;
210}
211
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000212
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000213/// \brief Check if the attribute has at least as many args as Num. May
214/// output an error.
215static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
216 unsigned int Num) {
217 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
219 return false;
220 }
221
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000222 return true;
223}
224
225///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000226/// \brief Check if passed in Decl is a field or potentially shared global var
227/// \return true if the Decl is a field or potentially shared global variable
228///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000229static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000230 if (isa<FieldDecl>(D))
231 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000232 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000233 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
234
235 return false;
236}
237
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000238/// \brief Check if the passed-in expression is of type int or bool.
239static bool isIntOrBool(Expr *Exp) {
240 QualType QT = Exp->getType();
241 return QT->isBooleanType() || QT->isIntegerType();
242}
243
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000244///
245/// \brief Check if passed in Decl is a pointer type.
246/// Note that this function may produce an error message.
247/// \return true if the Decl is a pointer type; false otherwise
248///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000249static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
250 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000251 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000252 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000253 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000254 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
255 << Attr.getName()->getName() << QT;
256 } else {
257 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
258 << Attr.getName();
259 }
260 return false;
261}
262
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000263/// \brief Checks that the passed in QualType either is of RecordType or points
264/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000265static const RecordType *getRecordType(QualType QT) {
266 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000267 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000268
269 // Now check if we point to record type.
270 if (const PointerType *PT = QT->getAs<PointerType>())
271 return PT->getPointeeType()->getAs<RecordType>();
272
273 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000274}
275
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000276/// \brief Thread Safety Analysis: Checks that the passed in RecordType
277/// resolves to a lockable object. May flag an error.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000278static bool checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
279 const RecordType *RT) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000280 // Flag error if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000281 if (!RT) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000282 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_class)
283 << Attr.getName();
284 return false;
285 }
286 // Flag error if the type is not lockable.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000287 if (!RT->getDecl()->getAttr<LockableAttr>()) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_lockable)
289 << Attr.getName();
290 return false;
291 }
292 return true;
293}
294
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000295/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
296/// from Sidx, resolve to a lockable object. May flag an error.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000297/// \param Sidx The attribute argument index to start checking with.
298/// \param ParamIdxOk Whether an argument can be indexing into a function
299/// parameter list.
300static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
301 const AttributeList &Attr,
302 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000303 int Sidx = 0,
304 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000305 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000306 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000307
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000308 if (ArgExp->isTypeDependent()) {
309 // FIXME -- need to processs this again on template instantiation
310 Args.push_back(ArgExp);
311 continue;
312 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000313
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000314 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000315
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000316 // First see if we can just cast to record type, or point to record type.
317 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000318
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000319 // Now check if we index into a record type function param.
320 if(!RT && ParamIdxOk) {
321 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000322 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
323 if(FD && IL) {
324 unsigned int NumParams = FD->getNumParams();
325 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000326 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
327 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
328 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000329 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
330 << Attr.getName() << Idx + 1 << NumParams;
331 return false;
332 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000333 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
334 RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000335 }
336 }
337
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000338 if (!checkForLockableRecord(S, D, Attr, RT))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000339 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000340
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000341 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000342 }
343 return true;
344}
345
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000346//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000347// Attribute Implementations
348//===----------------------------------------------------------------------===//
349
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000350// FIXME: All this manual attribute parsing code is gross. At the
351// least add some helper functions to check most argument patterns (#
352// and types of args).
353
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000354static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
355 bool pointer = false) {
356 assert(!Attr.isInvalid());
357
358 if (!checkAttributeNumArgs(S, Attr, 0))
359 return;
360
361 // D must be either a member field or global (potentially shared) variable.
362 if (!mayBeSharedVariable(D)) {
363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000364 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000365 return;
366 }
367
368 if (pointer && !checkIsPointer(S, D, Attr))
369 return;
370
371 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000372 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000373 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000374 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000375}
376
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000377static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000378 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000379 assert(!Attr.isInvalid());
380
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000381 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000382 return;
383
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000384 Expr *Arg = Attr.getArg(0);
385
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000386 // D must be either a member field or global (potentially shared) variable.
387 if (!mayBeSharedVariable(D)) {
388 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000389 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000390 return;
391 }
392
393 if (pointer && !checkIsPointer(S, D, Attr))
394 return;
395
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000396 if (!Arg->isTypeDependent()) {
397 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
398 return;
399 // FIXME -- semantic checks for dependent attributes
400 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000401
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000402 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000403 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000404 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000405 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000406 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000407}
408
409
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000410static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
411 bool scoped = false) {
412 assert(!Attr.isInvalid());
413
414 if (!checkAttributeNumArgs(S, Attr, 0))
415 return;
416
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000417 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000418 if (!isa<CXXRecordDecl>(D)) {
419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
420 << Attr.getName() << ExpectedClass;
421 return;
422 }
423
424 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000425 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000426 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000427 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000428}
429
430static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
431 const AttributeList &Attr) {
432 assert(!Attr.isInvalid());
433
434 if (!checkAttributeNumArgs(S, Attr, 0))
435 return;
436
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000437 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
439 << Attr.getName() << ExpectedFunctionOrMethod;
440 return;
441 }
442
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000443 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000444 S.Context));
445}
446
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000447static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
448 bool before) {
449 assert(!Attr.isInvalid());
450
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000451 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000452 return;
453
454 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000455 ValueDecl *VD = dyn_cast<ValueDecl>(D);
456 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000457 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000458 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000459 return;
460 }
461
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 // Check that this attribute only applies to lockable types
463 QualType QT = VD->getType();
464 if (!QT->isDependentType()) {
465 const RecordType *RT = getRecordType(QT);
466 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
467 S.Diag(Attr.getLoc(), diag::err_attribute_decl_not_lockable)
468 << Attr.getName();
469 return;
470 }
471 }
472
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000473 SmallVector<Expr*, 1> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000474 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000475 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000476 return;
477
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000478 unsigned Size = Args.size();
479 assert(Size == Attr.getNumArgs());
480 Expr **StartArg = Size == 0 ? 0 : &Args[0];
481
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000482 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000483 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000484 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000485 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000486 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000487 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000488}
489
490static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000491 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000492 assert(!Attr.isInvalid());
493
494 // zero or more arguments ok
495
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000496 // check that the attribute is applied to a function
497 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
499 << Attr.getName() << ExpectedFunctionOrMethod;
500 return;
501 }
502
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000503 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000504 SmallVector<Expr*, 1> Args;
505 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000506 return;
507
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000508 unsigned Size = Args.size();
509 assert(Size == Attr.getNumArgs());
510 Expr **StartArg = Size == 0 ? 0 : &Args[0];
511
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000512 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000513 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000514 S.Context, StartArg,
515 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000516 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000517 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000518 S.Context, StartArg,
519 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000520}
521
522static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000523 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000524 assert(!Attr.isInvalid());
525
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000526 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000527 return;
528
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000529
530 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
532 << Attr.getName() << ExpectedFunctionOrMethod;
533 return;
534 }
535
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000536 if (!isIntOrBool(Attr.getArg(0))) {
537 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
538 << Attr.getName();
539 return;
540 }
541
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000542 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000543 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000544 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000545 return;
546
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000547 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000548 Expr **StartArg = Size == 0 ? 0 : &Args[0];
549
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000550 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000551 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000552 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000553 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000554 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000555 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000556 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000557 S.Context,
558 Attr.getArg(0),
559 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000560}
561
562static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000563 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000564 assert(!Attr.isInvalid());
565
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000566 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000567 return;
568
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000569 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000570 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
571 << Attr.getName() << ExpectedFunctionOrMethod;
572 return;
573 }
574
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000575 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000576 SmallVector<Expr*, 1> Args;
577 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000578 return;
579
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000580 unsigned Size = Args.size();
581 assert(Size == Attr.getNumArgs());
582 Expr **StartArg = Size == 0 ? 0 : &Args[0];
583
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000585 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000586 S.Context, StartArg,
587 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000588 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000589 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000590 S.Context, StartArg,
591 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000592}
593
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000594static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000595 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000596 assert(!Attr.isInvalid());
597
598 // zero or more arguments ok
599
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000600 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000601 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
602 << Attr.getName() << ExpectedFunctionOrMethod;
603 return;
604 }
605
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000606 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000607 SmallVector<Expr*, 1> Args;
608 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000609 return;
610
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000611 unsigned Size = Args.size();
612 assert(Size == Attr.getNumArgs());
613 Expr **StartArg = Size == 0 ? 0 : &Args[0];
614
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000615 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000616 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000617}
618
619static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000620 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000621 assert(!Attr.isInvalid());
622
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000623 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000624 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000625 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000626
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000627 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
629 << Attr.getName() << ExpectedFunctionOrMethod;
630 return;
631 }
632
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000633 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000634 return;
635
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000636 // check that the argument is lockable object
637 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
638 return;
639
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000640 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000641}
642
643static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000644 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000645 assert(!Attr.isInvalid());
646
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000647 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000648 return;
649
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000650 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
652 << Attr.getName() << ExpectedFunctionOrMethod;
653 return;
654 }
655
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000656 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000657 SmallVector<Expr*, 1> Args;
658 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000659 return;
660
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000661 unsigned Size = Args.size();
662 assert(Size == Attr.getNumArgs());
663 Expr **StartArg = Size == 0 ? 0 : &Args[0];
664
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000665 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000666 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000667}
668
669
Chandler Carruth1b03c872011-07-02 00:01:44 +0000670static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
671 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000672 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000673 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000674 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000675 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000676 }
Mike Stumpbf916502009-07-24 19:02:52 +0000677
Chris Lattner6b6b5372008-06-26 18:38:35 +0000678 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000679
680 Expr *sizeExpr;
681
682 // Special case where the argument is a template id.
683 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000684 CXXScopeSpec SS;
685 UnqualifiedId id;
686 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000687
688 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
689 if (Size.isInvalid())
690 return;
691
692 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000693 } else {
694 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000695 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000696 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000697
Peter Collingbourne7a730022010-11-23 20:45:58 +0000698 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000699 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000700
701 // Instantiate/Install the vector type, and let Sema build the type for us.
702 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000703 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000704 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000705 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000706 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000707
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000708 // Remember this typedef decl, we will need it later for diagnostics.
709 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000710 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000711}
712
Chandler Carruth1b03c872011-07-02 00:01:44 +0000713static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000714 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000715 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000716 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000717
Chandler Carruth87c44602011-07-01 23:49:12 +0000718 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000719 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000720 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000721 // If the alignment is less than or equal to 8 bits, the packed attribute
722 // has no effect.
723 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000724 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000725 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000726 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000727 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000728 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000729 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000730 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000731}
732
Chandler Carruth1b03c872011-07-02 00:01:44 +0000733static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000734 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000735 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000736 else
737 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
738}
739
Chandler Carruth1b03c872011-07-02 00:01:44 +0000740static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000741 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000742 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000743 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000744
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000745 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000746 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000747 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000748 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000749 return;
750 }
751
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000752 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000753}
754
Ted Kremenek2f041d02011-09-29 07:02:25 +0000755static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
756 // The IBOutlet/IBOutletCollection attributes only apply to instance
757 // variables or properties of Objective-C classes. The outlet must also
758 // have an object reference type.
759 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
760 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000761 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000762 << Attr.getName() << VD->getType() << 0;
763 return false;
764 }
765 }
766 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
767 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000768 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000769 << Attr.getName() << PD->getType() << 1;
770 return false;
771 }
772 }
773 else {
774 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
775 return false;
776 }
777
778 return true;
779}
780
Chandler Carruth1b03c872011-07-02 00:01:44 +0000781static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000782 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000783 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000784 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000785
786 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000787 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000788
Ted Kremenek2f041d02011-09-29 07:02:25 +0000789 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000790}
791
Chandler Carruth1b03c872011-07-02 00:01:44 +0000792static void handleIBOutletCollection(Sema &S, Decl *D,
793 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000794
795 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000796 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
798 return;
799 }
800
Ted Kremenek2f041d02011-09-29 07:02:25 +0000801 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000802 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000803
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000804 IdentifierInfo *II = Attr.getParameterName();
805 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000806 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000807
John McCallb3d87482010-08-24 05:47:05 +0000808 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000809 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000810 if (!TypeRep) {
811 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
812 return;
813 }
John McCallb3d87482010-08-24 05:47:05 +0000814 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000815 // Diagnose use of non-object type in iboutletcollection attribute.
816 // FIXME. Gnu attribute extension ignores use of builtin types in
817 // attributes. So, __attribute__((iboutletcollection(char))) will be
818 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000819 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000820 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
821 return;
822 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000823 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
824 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000825}
826
Chandler Carruthd309c812011-07-01 23:49:16 +0000827static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000828 if (const RecordType *UT = T->getAsUnionType())
829 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
830 RecordDecl *UD = UT->getDecl();
831 for (RecordDecl::field_iterator it = UD->field_begin(),
832 itend = UD->field_end(); it != itend; ++it) {
833 QualType QT = it->getType();
834 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
835 T = QT;
836 return;
837 }
838 }
839 }
840}
841
Chandler Carruth1b03c872011-07-02 00:01:44 +0000842static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000843 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
844 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000845 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000846 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000847 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000848 return;
849 }
Mike Stumpbf916502009-07-24 19:02:52 +0000850
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000851 // In C++ the implicit 'this' function parameter also counts, and they are
852 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000853 bool HasImplicitThisParam = isInstanceMethod(D);
854 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000855
856 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000857 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000858
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000859 for (AttributeList::arg_iterator I=Attr.arg_begin(),
860 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000861
862
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000863 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000864 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000865 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000866 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
867 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
869 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000870 return;
871 }
Mike Stumpbf916502009-07-24 19:02:52 +0000872
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000873 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000874
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000875 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000876 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000877 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000878 return;
879 }
Mike Stumpbf916502009-07-24 19:02:52 +0000880
Ted Kremenek465172f2008-07-21 22:09:15 +0000881 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000882 if (HasImplicitThisParam) {
883 if (x == 0) {
884 S.Diag(Attr.getLoc(),
885 diag::err_attribute_invalid_implicit_this_argument)
886 << "nonnull" << Ex->getSourceRange();
887 return;
888 }
889 --x;
890 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000891
892 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000893 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000894 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000895
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000896 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000897 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000898 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000899 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000900 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000901 }
Mike Stumpbf916502009-07-24 19:02:52 +0000902
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000903 NonNullArgs.push_back(x);
904 }
Mike Stumpbf916502009-07-24 19:02:52 +0000905
906 // If no arguments were specified to __attribute__((nonnull)) then all pointer
907 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000908 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000909 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
910 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000911 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000912 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000913 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000914 }
Mike Stumpbf916502009-07-24 19:02:52 +0000915
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000916 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000917 if (NonNullArgs.empty()) {
918 // Warn the trivial case only if attribute is not coming from a
919 // macro instantiation.
920 if (Attr.getLoc().isFileID())
921 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000922 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000923 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000924 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000925
926 unsigned* start = &NonNullArgs[0];
927 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000928 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000929 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000930 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000931}
932
Chandler Carruth1b03c872011-07-02 00:01:44 +0000933static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000934 // This attribute must be applied to a function declaration.
935 // The first argument to the attribute must be a string,
936 // the name of the resource, for example "malloc".
937 // The following arguments must be argument indexes, the arguments must be
938 // of integer type for Returns, otherwise of pointer type.
939 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000940 // after being held. free() should be __attribute((ownership_takes)), whereas
941 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000942
943 if (!AL.getParameterName()) {
944 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
945 << AL.getName()->getName() << 1;
946 return;
947 }
948 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000949 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000950 switch (AL.getKind()) {
951 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000952 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000953 if (AL.getNumArgs() < 1) {
954 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
955 return;
956 }
Jordy Rose2a479922010-08-12 08:54:03 +0000957 break;
958 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000959 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000960 if (AL.getNumArgs() < 1) {
961 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
962 return;
963 }
Jordy Rose2a479922010-08-12 08:54:03 +0000964 break;
965 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000966 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000967 if (AL.getNumArgs() > 1) {
968 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
969 << AL.getNumArgs() + 1;
970 return;
971 }
Jordy Rose2a479922010-08-12 08:54:03 +0000972 break;
973 default:
974 // This should never happen given how we are called.
975 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000976 }
977
Chandler Carruth87c44602011-07-01 23:49:12 +0000978 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000979 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
980 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000981 return;
982 }
983
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000984 // In C++ the implicit 'this' function parameter also counts, and they are
985 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000986 bool HasImplicitThisParam = isInstanceMethod(D);
987 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000988
Chris Lattner5f9e2722011-07-23 10:55:15 +0000989 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000990
991 // Normalize the argument, __foo__ becomes foo.
992 if (Module.startswith("__") && Module.endswith("__"))
993 Module = Module.substr(2, Module.size() - 4);
994
Chris Lattner5f9e2722011-07-23 10:55:15 +0000995 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000996
Jordy Rose2a479922010-08-12 08:54:03 +0000997 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
998 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000999
Peter Collingbourne7a730022010-11-23 20:45:58 +00001000 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001001 llvm::APSInt ArgNum(32);
1002 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1003 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1004 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1005 << AL.getName()->getName() << IdxExpr->getSourceRange();
1006 continue;
1007 }
1008
1009 unsigned x = (unsigned) ArgNum.getZExtValue();
1010
1011 if (x > NumArgs || x < 1) {
1012 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1013 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1014 continue;
1015 }
1016 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001017 if (HasImplicitThisParam) {
1018 if (x == 0) {
1019 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1020 << "ownership" << IdxExpr->getSourceRange();
1021 return;
1022 }
1023 --x;
1024 }
1025
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001026 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001027 case OwnershipAttr::Takes:
1028 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001029 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001030 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001031 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1032 // FIXME: Should also highlight argument in decl.
1033 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001034 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001035 << "pointer"
1036 << IdxExpr->getSourceRange();
1037 continue;
1038 }
1039 break;
1040 }
Sean Huntcf807c42010-08-18 23:23:40 +00001041 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001042 if (AL.getNumArgs() > 1) {
1043 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001044 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001045 llvm::APSInt ArgNum(32);
1046 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1047 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1048 S.Diag(AL.getLoc(), diag::err_ownership_type)
1049 << "ownership_returns" << "integer"
1050 << IdxExpr->getSourceRange();
1051 return;
1052 }
1053 }
1054 break;
1055 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001056 } // switch
1057
1058 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001059 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001060 i = D->specific_attr_begin<OwnershipAttr>(),
1061 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001062 i != e; ++i) {
1063 if ((*i)->getOwnKind() != K) {
1064 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1065 I!=E; ++I) {
1066 if (x == *I) {
1067 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1068 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001069 }
1070 }
1071 }
1072 }
1073 OwnershipArgs.push_back(x);
1074 }
1075
1076 unsigned* start = OwnershipArgs.data();
1077 unsigned size = OwnershipArgs.size();
1078 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001079
1080 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1081 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1082 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001083 }
Sean Huntcf807c42010-08-18 23:23:40 +00001084
Chandler Carruth87c44602011-07-01 23:49:12 +00001085 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001086 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001087}
1088
John McCall332bb2a2011-02-08 22:35:49 +00001089/// Whether this declaration has internal linkage for the purposes of
1090/// things that want to complain about things not have internal linkage.
1091static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1092 switch (D->getLinkage()) {
1093 case NoLinkage:
1094 case InternalLinkage:
1095 return true;
1096
1097 // Template instantiations that go from external to unique-external
1098 // shouldn't get diagnosed.
1099 case UniqueExternalLinkage:
1100 return true;
1101
1102 case ExternalLinkage:
1103 return false;
1104 }
1105 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001106}
1107
Chandler Carruth1b03c872011-07-02 00:01:44 +00001108static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001109 // Check the attribute arguments.
1110 if (Attr.getNumArgs() > 1) {
1111 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1112 return;
1113 }
1114
Chandler Carruth87c44602011-07-01 23:49:12 +00001115 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001117 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001118 return;
1119 }
1120
Chandler Carruth87c44602011-07-01 23:49:12 +00001121 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001122
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001123 // gcc rejects
1124 // class c {
1125 // static int a __attribute__((weakref ("v2")));
1126 // static int b() __attribute__((weakref ("f3")));
1127 // };
1128 // and ignores the attributes of
1129 // void f(void) {
1130 // static int a __attribute__((weakref ("v2")));
1131 // }
1132 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001133 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001134 if (!Ctx->isFileContext()) {
1135 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001136 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001137 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001138 }
1139
1140 // The GCC manual says
1141 //
1142 // At present, a declaration to which `weakref' is attached can only
1143 // be `static'.
1144 //
1145 // It also says
1146 //
1147 // Without a TARGET,
1148 // given as an argument to `weakref' or to `alias', `weakref' is
1149 // equivalent to `weak'.
1150 //
1151 // gcc 4.4.1 will accept
1152 // int a7 __attribute__((weakref));
1153 // as
1154 // int a7 __attribute__((weak));
1155 // This looks like a bug in gcc. We reject that for now. We should revisit
1156 // it if this behaviour is actually used.
1157
John McCall332bb2a2011-02-08 22:35:49 +00001158 if (!hasEffectivelyInternalLinkage(nd)) {
1159 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001160 return;
1161 }
1162
1163 // GCC rejects
1164 // static ((alias ("y"), weakref)).
1165 // Should we? How to check that weakref is before or after alias?
1166
1167 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001168 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001169 Arg = Arg->IgnoreParenCasts();
1170 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1171
Douglas Gregor5cee1192011-07-27 05:40:30 +00001172 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001173 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1174 << "weakref" << 1;
1175 return;
1176 }
1177 // GCC will accept anything as the argument of weakref. Should we
1178 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001179 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001180 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001181 }
1182
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001183 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001184}
1185
Chandler Carruth1b03c872011-07-02 00:01:44 +00001186static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001187 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001188 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001190 return;
1191 }
Mike Stumpbf916502009-07-24 19:02:52 +00001192
Peter Collingbourne7a730022010-11-23 20:45:58 +00001193 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001194 Arg = Arg->IgnoreParenCasts();
1195 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001196
Douglas Gregor5cee1192011-07-27 05:40:30 +00001197 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001198 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001199 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001200 return;
1201 }
Mike Stumpbf916502009-07-24 19:02:52 +00001202
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001203 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001204 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1205 return;
1206 }
1207
Chris Lattner6b6b5372008-06-26 18:38:35 +00001208 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001209
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001210 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001211 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212}
1213
Chandler Carruth1b03c872011-07-02 00:01:44 +00001214static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001215 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001216 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001217 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001218
Chandler Carruth87c44602011-07-01 23:49:12 +00001219 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001220 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001221 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001222 return;
1223 }
1224
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001225 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001226}
1227
Chandler Carruth1b03c872011-07-02 00:01:44 +00001228static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1229 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001230 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001231 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1233 return;
1234 }
1235
Chandler Carruth87c44602011-07-01 23:49:12 +00001236 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001237 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001238 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001239 return;
1240 }
Mike Stumpbf916502009-07-24 19:02:52 +00001241
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001242 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001243}
1244
Chandler Carruth1b03c872011-07-02 00:01:44 +00001245static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001246 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001247 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1249 return;
1250 }
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Chandler Carruth87c44602011-07-01 23:49:12 +00001252 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001253 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001254 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001255 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001256 return;
1257 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001258 }
1259
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001260 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001261}
1262
Chandler Carruth1b03c872011-07-02 00:01:44 +00001263static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001264 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001265 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001266 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001267
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001268 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001269}
1270
Chandler Carruth1b03c872011-07-02 00:01:44 +00001271static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001272 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001273 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001274 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001275 else
1276 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001277 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001278}
1279
Chandler Carruth1b03c872011-07-02 00:01:44 +00001280static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001281 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001282 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001283 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001284 else
1285 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001286 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001287}
1288
Chandler Carruth1b03c872011-07-02 00:01:44 +00001289static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001290 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001291
1292 if (S.CheckNoReturnAttr(attr)) return;
1293
Chandler Carruth87c44602011-07-01 23:49:12 +00001294 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001295 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001296 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001297 return;
1298 }
1299
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001300 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001301}
1302
1303bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001304 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001305 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1306 attr.setInvalid();
1307 return true;
1308 }
1309
1310 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001311}
1312
Chandler Carruth1b03c872011-07-02 00:01:44 +00001313static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1314 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001315
1316 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1317 // because 'analyzer_noreturn' does not impact the type.
1318
Chandler Carruth1731e202011-07-11 23:30:35 +00001319 if(!checkAttributeNumArgs(S, Attr, 0))
1320 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001321
Chandler Carruth87c44602011-07-01 23:49:12 +00001322 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1323 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001324 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1325 && !VD->getType()->isFunctionPointerType())) {
1326 S.Diag(Attr.getLoc(),
1327 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1328 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001329 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001330 return;
1331 }
1332 }
1333
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001334 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001335}
1336
John Thompson35cc9622010-08-09 21:53:52 +00001337// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001338static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001339/*
1340 Returning a Vector Class in Registers
1341
Eric Christopherf48f3672010-12-01 22:13:54 +00001342 According to the PPU ABI specifications, a class with a single member of
1343 vector type is returned in memory when used as the return value of a function.
1344 This results in inefficient code when implementing vector classes. To return
1345 the value in a single vector register, add the vecreturn attribute to the
1346 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001347
1348 Example:
1349
1350 struct Vector
1351 {
1352 __vector float xyzw;
1353 } __attribute__((vecreturn));
1354
1355 Vector Add(Vector lhs, Vector rhs)
1356 {
1357 Vector result;
1358 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1359 return result; // This will be returned in a register
1360 }
1361*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001362 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001363 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001364 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001365 return;
1366 }
1367
Chandler Carruth87c44602011-07-01 23:49:12 +00001368 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001369 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1370 return;
1371 }
1372
Chandler Carruth87c44602011-07-01 23:49:12 +00001373 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001374 int count = 0;
1375
1376 if (!isa<CXXRecordDecl>(record)) {
1377 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1378 return;
1379 }
1380
1381 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1382 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1383 return;
1384 }
1385
Eric Christopherf48f3672010-12-01 22:13:54 +00001386 for (RecordDecl::field_iterator iter = record->field_begin();
1387 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001388 if ((count == 1) || !iter->getType()->isVectorType()) {
1389 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1390 return;
1391 }
1392 count++;
1393 }
1394
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001395 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001396}
1397
Chandler Carruth1b03c872011-07-02 00:01:44 +00001398static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001399 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001401 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001402 return;
1403 }
1404 // FIXME: Actually store the attribute on the declaration
1405}
1406
Chandler Carruth1b03c872011-07-02 00:01:44 +00001407static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001408 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001409 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001411 return;
1412 }
Mike Stumpbf916502009-07-24 19:02:52 +00001413
Chandler Carruth87c44602011-07-01 23:49:12 +00001414 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1415 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001417 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001418 return;
1419 }
Mike Stumpbf916502009-07-24 19:02:52 +00001420
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001421 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001422}
1423
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001424static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1425 const AttributeList &Attr) {
1426 // check the attribute arguments.
1427 if (Attr.hasParameterOrArguments()) {
1428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1429 return;
1430 }
1431
1432 if (!isa<FunctionDecl>(D)) {
1433 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1434 << Attr.getName() << ExpectedFunction;
1435 return;
1436 }
1437
1438 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1439}
1440
Chandler Carruth1b03c872011-07-02 00:01:44 +00001441static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001442 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001443 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001444 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1445 return;
1446 }
Mike Stumpbf916502009-07-24 19:02:52 +00001447
Chandler Carruth87c44602011-07-01 23:49:12 +00001448 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001449 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001450 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1451 return;
1452 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001453 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001454 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001455 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001456 return;
1457 }
Mike Stumpbf916502009-07-24 19:02:52 +00001458
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001459 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001460}
1461
Chandler Carruth1b03c872011-07-02 00:01:44 +00001462static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001463 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001464 if (Attr.getNumArgs() > 1) {
1465 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001466 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001467 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001468
1469 int priority = 65535; // FIXME: Do not hardcode such constants.
1470 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001471 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001472 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001473 if (E->isTypeDependent() || E->isValueDependent() ||
1474 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001475 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001476 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001477 return;
1478 }
1479 priority = Idx.getZExtValue();
1480 }
Mike Stumpbf916502009-07-24 19:02:52 +00001481
Chandler Carruth87c44602011-07-01 23:49:12 +00001482 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001484 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001485 return;
1486 }
1487
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001488 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001489 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001490}
1491
Chandler Carruth1b03c872011-07-02 00:01:44 +00001492static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001493 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001494 if (Attr.getNumArgs() > 1) {
1495 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001496 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001497 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001498
1499 int priority = 65535; // FIXME: Do not hardcode such constants.
1500 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001501 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001502 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001503 if (E->isTypeDependent() || E->isValueDependent() ||
1504 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001506 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001507 return;
1508 }
1509 priority = Idx.getZExtValue();
1510 }
Mike Stumpbf916502009-07-24 19:02:52 +00001511
Chandler Carruth87c44602011-07-01 23:49:12 +00001512 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001513 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001514 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001515 return;
1516 }
1517
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001518 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001519 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001520}
1521
Chandler Carruth1b03c872011-07-02 00:01:44 +00001522static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001523 unsigned NumArgs = Attr.getNumArgs();
1524 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001525 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001526 return;
1527 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001528
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001529 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001530 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001531 if (NumArgs == 1) {
1532 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001533 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001534 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1535 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001536 return;
1537 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001538 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001539 }
Mike Stumpbf916502009-07-24 19:02:52 +00001540
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001541 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001542}
1543
Chandler Carruth1b03c872011-07-02 00:01:44 +00001544static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001545 unsigned NumArgs = Attr.getNumArgs();
1546 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001547 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001548 return;
1549 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001550
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001551 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001552 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001553 if (NumArgs == 1) {
1554 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001555 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001556 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001557 diag::err_attribute_not_string) << "unavailable";
1558 return;
1559 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001560 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001561 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001562 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001563}
1564
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001565static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1566 const AttributeList &Attr) {
1567 unsigned NumArgs = Attr.getNumArgs();
1568 if (NumArgs > 0) {
1569 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1570 return;
1571 }
1572
1573 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001574 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001575}
1576
Ted Kremenek71207fc2012-01-05 22:47:47 +00001577static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001578 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001579 if (!isa<ObjCInterfaceDecl>(D)) {
1580 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1581 return;
1582 }
1583
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001584 unsigned NumArgs = Attr.getNumArgs();
1585 if (NumArgs > 0) {
1586 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1587 return;
1588 }
1589
Ted Kremenek71207fc2012-01-05 22:47:47 +00001590 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001591 Attr.getRange(), S.Context));
1592}
1593
Chandler Carruth1b03c872011-07-02 00:01:44 +00001594static void handleAvailabilityAttr(Sema &S, Decl *D,
1595 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001596 IdentifierInfo *Platform = Attr.getParameterName();
1597 SourceLocation PlatformLoc = Attr.getParameterLoc();
1598
Chris Lattner5f9e2722011-07-23 10:55:15 +00001599 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001600 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1601 if (PlatformName.empty()) {
1602 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1603 << Platform;
1604
1605 PlatformName = Platform->getName();
1606 }
1607
1608 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1609 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1610 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001611 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001612
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001613 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001614 // of these steps are needed).
1615 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001616 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001617 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1618 << 1 << PlatformName << Deprecated.Version.getAsString()
1619 << 0 << Introduced.Version.getAsString();
1620 return;
1621 }
1622
1623 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001624 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001625 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1626 << 2 << PlatformName << Obsoleted.Version.getAsString()
1627 << 0 << Introduced.Version.getAsString();
1628 return;
1629 }
1630
1631 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001632 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001633 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1634 << 2 << PlatformName << Obsoleted.Version.getAsString()
1635 << 1 << Deprecated.Version.getAsString();
1636 return;
1637 }
1638
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001639 StringRef Str;
1640 const StringLiteral *SE =
1641 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1642 if (SE)
1643 Str = SE->getString();
1644
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001645 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001646 Platform,
1647 Introduced.Version,
1648 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001649 Obsoleted.Version,
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001650 IsUnavailable,
1651 Str));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001652}
1653
Chandler Carruth1b03c872011-07-02 00:01:44 +00001654static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001655 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001656 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001657 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001658
Peter Collingbourne7a730022010-11-23 20:45:58 +00001659 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001660 Arg = Arg->IgnoreParenCasts();
1661 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001662
Douglas Gregor5cee1192011-07-27 05:40:30 +00001663 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001665 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001666 return;
1667 }
Mike Stumpbf916502009-07-24 19:02:52 +00001668
Chris Lattner5f9e2722011-07-23 10:55:15 +00001669 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001670 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001671
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001672 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001673 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001674 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001675 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001676 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001677 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001678 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001679 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001680 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001681 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001682 return;
1683 }
Mike Stumpbf916502009-07-24 19:02:52 +00001684
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001685 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001686}
1687
Chandler Carruth1b03c872011-07-02 00:01:44 +00001688static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1689 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001690 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1691 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001693 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001694 return;
1695 }
1696
Chandler Carruth87c44602011-07-01 23:49:12 +00001697 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1698 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1699 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001700 << "objc_method_family" << 1;
1701 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001703 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001704 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001705 return;
1706 }
1707
Chris Lattner5f9e2722011-07-23 10:55:15 +00001708 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001709 ObjCMethodFamilyAttr::FamilyKind family;
1710 if (param == "none")
1711 family = ObjCMethodFamilyAttr::OMF_None;
1712 else if (param == "alloc")
1713 family = ObjCMethodFamilyAttr::OMF_alloc;
1714 else if (param == "copy")
1715 family = ObjCMethodFamilyAttr::OMF_copy;
1716 else if (param == "init")
1717 family = ObjCMethodFamilyAttr::OMF_init;
1718 else if (param == "mutableCopy")
1719 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1720 else if (param == "new")
1721 family = ObjCMethodFamilyAttr::OMF_new;
1722 else {
1723 // Just warn and ignore it. This is future-proof against new
1724 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001725 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001726 return;
1727 }
1728
John McCallf85e1932011-06-15 23:02:42 +00001729 if (family == ObjCMethodFamilyAttr::OMF_init &&
1730 !method->getResultType()->isObjCObjectPointerType()) {
1731 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1732 << method->getResultType();
1733 // Ignore the attribute.
1734 return;
1735 }
1736
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001737 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001738 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001739}
1740
Chandler Carruth1b03c872011-07-02 00:01:44 +00001741static void handleObjCExceptionAttr(Sema &S, Decl *D,
1742 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001743 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001744 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001745
Chris Lattner0db29ec2009-02-14 08:09:34 +00001746 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1747 if (OCI == 0) {
1748 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1749 return;
1750 }
Mike Stumpbf916502009-07-24 19:02:52 +00001751
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001752 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001753}
1754
Chandler Carruth1b03c872011-07-02 00:01:44 +00001755static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001756 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001758 return;
1759 }
Richard Smith162e1c12011-04-15 14:24:37 +00001760 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001761 QualType T = TD->getUnderlyingType();
1762 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001763 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001764 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1765 return;
1766 }
1767 }
Fariborz Jahanian6b65d4a2011-12-16 15:54:29 +00001768 else
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00001769 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001770 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001771}
1772
Mike Stumpbf916502009-07-24 19:02:52 +00001773static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001774handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001775 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001777 return;
1778 }
1779
1780 if (!isa<FunctionDecl>(D)) {
1781 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1782 return;
1783 }
1784
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001785 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001786}
1787
Chandler Carruth1b03c872011-07-02 00:01:44 +00001788static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001789 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001790 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001791 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001792 return;
1793 }
Mike Stumpbf916502009-07-24 19:02:52 +00001794
Steve Naroff9eae5762008-09-18 16:44:58 +00001795 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001796 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001797 return;
1798 }
Mike Stumpbf916502009-07-24 19:02:52 +00001799
Sean Huntcf807c42010-08-18 23:23:40 +00001800 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001801 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001802 type = BlocksAttr::ByRef;
1803 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001804 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001805 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001806 return;
1807 }
Mike Stumpbf916502009-07-24 19:02:52 +00001808
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001809 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001810}
1811
Chandler Carruth1b03c872011-07-02 00:01:44 +00001812static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001813 // check the attribute arguments.
1814 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001816 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001817 }
1818
John McCall3323fad2011-09-09 07:56:05 +00001819 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001820 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001821 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001822 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001823 if (E->isTypeDependent() || E->isValueDependent() ||
1824 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001825 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001826 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001827 return;
1828 }
Mike Stumpbf916502009-07-24 19:02:52 +00001829
John McCall3323fad2011-09-09 07:56:05 +00001830 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001831 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1832 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001833 return;
1834 }
John McCall3323fad2011-09-09 07:56:05 +00001835
1836 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001837 }
1838
John McCall3323fad2011-09-09 07:56:05 +00001839 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001840 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001841 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001842 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001843 if (E->isTypeDependent() || E->isValueDependent() ||
1844 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001846 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001847 return;
1848 }
1849 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001850
John McCall3323fad2011-09-09 07:56:05 +00001851 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001852 // FIXME: This error message could be improved, it would be nice
1853 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001854 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1855 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001856 return;
1857 }
1858 }
1859
Chandler Carruth87c44602011-07-01 23:49:12 +00001860 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001861 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001862 if (isa<FunctionNoProtoType>(FT)) {
1863 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1864 return;
1865 }
Mike Stumpbf916502009-07-24 19:02:52 +00001866
Chris Lattner897cd902009-03-17 23:03:47 +00001867 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001868 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001869 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001870 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001871 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001872 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001873 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001874 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001875 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00001876 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1877 if (!BD->isVariadic()) {
1878 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1879 return;
1880 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001881 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001882 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001883 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001884 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001885 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001886 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001887 int m = Ty->isFunctionPointerType() ? 0 : 1;
1888 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001889 return;
1890 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001891 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001892 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001893 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001894 return;
1895 }
Anders Carlsson77091822008-10-05 18:05:59 +00001896 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001897 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001898 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001899 return;
1900 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001901 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001902 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001903}
1904
Chandler Carruth1b03c872011-07-02 00:01:44 +00001905static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001906 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001907 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001908 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001909
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001910 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001912 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001913 return;
1914 }
Mike Stumpbf916502009-07-24 19:02:52 +00001915
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001916 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1917 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1918 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001919 return;
1920 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001921 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1922 if (MD->getResultType()->isVoidType()) {
1923 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1924 << Attr.getName() << 1;
1925 return;
1926 }
1927
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001928 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001929}
1930
Chandler Carruth1b03c872011-07-02 00:01:44 +00001931static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001932 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001933 if (Attr.hasParameterOrArguments()) {
1934 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001935 return;
1936 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001937
Chandler Carruth87c44602011-07-01 23:49:12 +00001938 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00001939 if (isa<CXXRecordDecl>(D)) {
1940 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
1941 return;
1942 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1944 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001945 return;
1946 }
1947
Chandler Carruth87c44602011-07-01 23:49:12 +00001948 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001949
1950 // 'weak' only applies to declarations with external linkage.
1951 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001952 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001953 return;
1954 }
Mike Stumpbf916502009-07-24 19:02:52 +00001955
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001956 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001957}
1958
Chandler Carruth1b03c872011-07-02 00:01:44 +00001959static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001960 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001961 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001962 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001963
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001964
1965 // weak_import only applies to variable & function declarations.
1966 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001967 if (!D->canBeWeakImported(isDef)) {
1968 if (isDef)
1969 S.Diag(Attr.getLoc(),
1970 diag::warn_attribute_weak_import_invalid_on_definition)
1971 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001972 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001973 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00001974 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00001975 // Nothing to warn about here.
1976 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001977 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001978 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001979
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001980 return;
1981 }
1982
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001983 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001984}
1985
Chandler Carruth1b03c872011-07-02 00:01:44 +00001986static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1987 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001988 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001989 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001990 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001991
1992 unsigned WGSize[3];
1993 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001994 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001995 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001996 if (E->isTypeDependent() || E->isValueDependent() ||
1997 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1999 << "reqd_work_group_size" << E->getSourceRange();
2000 return;
2001 }
2002 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2003 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002004 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002005 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002006 WGSize[2]));
2007}
2008
Chandler Carruth1b03c872011-07-02 00:01:44 +00002009static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002010 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002011 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002012 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002013
2014 // Make sure that there is a string literal as the sections's single
2015 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002016 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002017 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002018 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002019 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002020 return;
2021 }
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Chris Lattner797c3c42009-08-10 19:03:04 +00002023 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002024 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002025 if (!Error.empty()) {
2026 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2027 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002028 return;
2029 }
Mike Stump1eb44332009-09-09 15:08:12 +00002030
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002031 // This attribute cannot be applied to local variables.
2032 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2033 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2034 return;
2035 }
2036
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002037 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002038 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002039}
2040
Chris Lattner6b6b5372008-06-26 18:38:35 +00002041
Chandler Carruth1b03c872011-07-02 00:01:44 +00002042static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002043 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002044 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002045 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002046 return;
2047 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002048
Chandler Carruth87c44602011-07-01 23:49:12 +00002049 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002050 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002051 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002052 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002053 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002054 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002055}
2056
Chandler Carruth1b03c872011-07-02 00:01:44 +00002057static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002058 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002059 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002060 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002061 return;
2062 }
Mike Stumpbf916502009-07-24 19:02:52 +00002063
Chandler Carruth87c44602011-07-01 23:49:12 +00002064 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002065 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002066 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002067 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002068 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002069 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002070}
2071
Chandler Carruth1b03c872011-07-02 00:01:44 +00002072static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002073 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002074 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002075 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002076
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002077 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002078}
2079
Chandler Carruth1b03c872011-07-02 00:01:44 +00002080static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002081 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2083 return;
2084 }
Mike Stumpbf916502009-07-24 19:02:52 +00002085
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002086 if (Attr.getNumArgs() != 0) {
2087 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2088 return;
2089 }
Mike Stumpbf916502009-07-24 19:02:52 +00002090
Chandler Carruth87c44602011-07-01 23:49:12 +00002091 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002092
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002093 if (!VD || !VD->hasLocalStorage()) {
2094 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2095 return;
2096 }
Mike Stumpbf916502009-07-24 19:02:52 +00002097
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002098 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002099 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002100 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002101 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2102 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002103 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002104 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002105 Attr.getParameterName();
2106 return;
2107 }
Mike Stumpbf916502009-07-24 19:02:52 +00002108
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002109 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2110 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002111 S.Diag(Attr.getParameterLoc(),
2112 diag::err_attribute_cleanup_arg_not_function)
2113 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002114 return;
2115 }
2116
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002117 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002118 S.Diag(Attr.getParameterLoc(),
2119 diag::err_attribute_cleanup_func_must_take_one_arg)
2120 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002121 return;
2122 }
Mike Stumpbf916502009-07-24 19:02:52 +00002123
Anders Carlsson89941c12009-02-07 23:16:50 +00002124 // We're currently more strict than GCC about what function types we accept.
2125 // If this ever proves to be a problem it should be easy to fix.
2126 QualType Ty = S.Context.getPointerType(VD->getType());
2127 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002128 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2129 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002130 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002131 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2132 Attr.getParameterName() << ParamTy << Ty;
2133 return;
2134 }
Mike Stumpbf916502009-07-24 19:02:52 +00002135
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002136 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00002137 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002138}
2139
Mike Stumpbf916502009-07-24 19:02:52 +00002140/// Handle __attribute__((format_arg((idx)))) attribute based on
2141/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002142static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002143 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002144 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002145
Chandler Carruth87c44602011-07-01 23:49:12 +00002146 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002147 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002148 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002149 return;
2150 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002151
2152 // In C++ the implicit 'this' function parameter also counts, and they are
2153 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002154 bool HasImplicitThisParam = isInstanceMethod(D);
2155 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002156 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002157
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002158 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002159 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002160 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002161 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2162 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2164 << "format" << 2 << IdxExpr->getSourceRange();
2165 return;
2166 }
Mike Stumpbf916502009-07-24 19:02:52 +00002167
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002168 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2170 << "format" << 2 << IdxExpr->getSourceRange();
2171 return;
2172 }
Mike Stumpbf916502009-07-24 19:02:52 +00002173
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002174 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002175
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002176 if (HasImplicitThisParam) {
2177 if (ArgIdx == 0) {
2178 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2179 << "format_arg" << IdxExpr->getSourceRange();
2180 return;
2181 }
2182 ArgIdx--;
2183 }
2184
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002185 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002186 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002187
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002188 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2189 if (not_nsstring_type &&
2190 !isCFStringType(Ty, S.Context) &&
2191 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002192 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002193 // FIXME: Should highlight the actual expression that has the wrong type.
2194 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002195 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002196 << IdxExpr->getSourceRange();
2197 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002198 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002199 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002200 if (!isNSStringType(Ty, S.Context) &&
2201 !isCFStringType(Ty, S.Context) &&
2202 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002203 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002204 // FIXME: Should highlight the actual expression that has the wrong type.
2205 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002206 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002207 << IdxExpr->getSourceRange();
2208 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002209 }
2210
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002211 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002212 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002213}
2214
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002215enum FormatAttrKind {
2216 CFStringFormat,
2217 NSStringFormat,
2218 StrftimeFormat,
2219 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002220 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002221 InvalidFormat
2222};
2223
2224/// getFormatAttrKind - Map from format attribute names to supported format
2225/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002226static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002227 // Check for formats that get handled specially.
2228 if (Format == "NSString")
2229 return NSStringFormat;
2230 if (Format == "CFString")
2231 return CFStringFormat;
2232 if (Format == "strftime")
2233 return StrftimeFormat;
2234
2235 // Otherwise, check for supported formats.
2236 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2237 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
2238 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002239 Format == "zcmn_err" ||
2240 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002241 return SupportedFormat;
2242
Duncan Sandsbc525952010-03-23 14:44:19 +00002243 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2244 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002245 return IgnoredFormat;
2246
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002247 return InvalidFormat;
2248}
2249
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002250/// Handle __attribute__((init_priority(priority))) attributes based on
2251/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002252static void handleInitPriorityAttr(Sema &S, Decl *D,
2253 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002254 if (!S.getLangOptions().CPlusPlus) {
2255 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2256 return;
2257 }
2258
Chandler Carruth87c44602011-07-01 23:49:12 +00002259 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002260 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2261 Attr.setInvalid();
2262 return;
2263 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002264 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002265 if (S.Context.getAsArrayType(T))
2266 T = S.Context.getBaseElementType(T);
2267 if (!T->getAs<RecordType>()) {
2268 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2269 Attr.setInvalid();
2270 return;
2271 }
2272
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002273 if (Attr.getNumArgs() != 1) {
2274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2275 Attr.setInvalid();
2276 return;
2277 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002278 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002279
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002280 llvm::APSInt priority(32);
2281 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2282 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2283 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2284 << "init_priority" << priorityExpr->getSourceRange();
2285 Attr.setInvalid();
2286 return;
2287 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002288 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002289 if (prioritynum < 101 || prioritynum > 65535) {
2290 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2291 << priorityExpr->getSourceRange();
2292 Attr.setInvalid();
2293 return;
2294 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002295 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002296 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002297}
2298
Mike Stumpbf916502009-07-24 19:02:52 +00002299/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2300/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002301static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002302
Chris Lattner545dd342008-06-28 23:36:30 +00002303 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002305 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002306 return;
2307 }
2308
Chris Lattner545dd342008-06-28 23:36:30 +00002309 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002311 return;
2312 }
2313
Chandler Carruth87c44602011-07-01 23:49:12 +00002314 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002316 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002317 return;
2318 }
2319
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002320 // In C++ the implicit 'this' function parameter also counts, and they are
2321 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002322 bool HasImplicitThisParam = isInstanceMethod(D);
2323 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002324 unsigned FirstIdx = 1;
2325
Chris Lattner5f9e2722011-07-23 10:55:15 +00002326 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002327
2328 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002329 if (Format.startswith("__") && Format.endswith("__"))
2330 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002331
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002332 // Check for supported formats.
2333 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002334
2335 if (Kind == IgnoredFormat)
2336 return;
2337
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002338 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002339 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002340 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002341 return;
2342 }
2343
2344 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002345 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002346 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002347 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2348 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002350 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002351 return;
2352 }
2353
2354 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002355 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002356 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002357 return;
2358 }
2359
2360 // FIXME: Do we need to bounds check?
2361 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002362
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002363 if (HasImplicitThisParam) {
2364 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002365 S.Diag(Attr.getLoc(),
2366 diag::err_format_attribute_implicit_this_format_string)
2367 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002368 return;
2369 }
2370 ArgIdx--;
2371 }
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Chris Lattner6b6b5372008-06-26 18:38:35 +00002373 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002374 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002375
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002376 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002377 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002378 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2379 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002380 return;
2381 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002382 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002383 // FIXME: do we need to check if the type is NSString*? What are the
2384 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002385 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002386 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002387 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2388 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002389 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002390 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002391 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002392 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002393 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002394 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2395 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002396 return;
2397 }
2398
2399 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002400 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002401 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002402 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2403 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002405 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002406 return;
2407 }
2408
2409 // check if the function is variadic if the 3rd argument non-zero
2410 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002411 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002412 ++NumArgs; // +1 for ...
2413 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002414 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002415 return;
2416 }
2417 }
2418
Chris Lattner3c73c412008-11-19 08:23:25 +00002419 // strftime requires FirstArg to be 0 because it doesn't read from any
2420 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002421 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002422 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002423 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2424 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002425 return;
2426 }
2427 // if 0 it disables parameter checking (to use with e.g. va_list)
2428 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002429 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002430 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002431 return;
2432 }
2433
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002434 // Check whether we already have an equivalent format attribute.
2435 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002436 i = D->specific_attr_begin<FormatAttr>(),
2437 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002438 i != e ; ++i) {
2439 FormatAttr *f = *i;
2440 if (f->getType() == Format &&
2441 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2442 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2443 // If we don't have a valid location for this attribute, adopt the
2444 // location.
2445 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002446 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002447 return;
2448 }
2449 }
2450
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002451 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002452 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002453 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002454}
2455
Chandler Carruth1b03c872011-07-02 00:01:44 +00002456static void handleTransparentUnionAttr(Sema &S, Decl *D,
2457 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002458 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002459 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002460 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002461
Chris Lattner6b6b5372008-06-26 18:38:35 +00002462
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002463 // Try to find the underlying union declaration.
2464 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002465 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002466 if (TD && TD->getUnderlyingType()->isUnionType())
2467 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2468 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002469 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002470
2471 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002473 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002474 return;
2475 }
2476
John McCall5e1cdac2011-10-07 06:10:15 +00002477 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002478 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002479 diag::warn_transparent_union_attribute_not_definition);
2480 return;
2481 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002482
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002483 RecordDecl::field_iterator Field = RD->field_begin(),
2484 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002485 if (Field == FieldEnd) {
2486 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2487 return;
2488 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002489
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002490 FieldDecl *FirstField = *Field;
2491 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002492 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002493 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002494 diag::warn_transparent_union_attribute_floating)
2495 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002496 return;
2497 }
2498
2499 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2500 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2501 for (; Field != FieldEnd; ++Field) {
2502 QualType FieldType = Field->getType();
2503 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2504 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2505 // Warn if we drop the attribute.
2506 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002507 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002508 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002509 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002510 diag::warn_transparent_union_attribute_field_size_align)
2511 << isSize << Field->getDeclName() << FieldBits;
2512 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002513 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002514 diag::note_transparent_union_first_field_size_align)
2515 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002516 return;
2517 }
2518 }
2519
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002520 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002521}
2522
Chandler Carruth1b03c872011-07-02 00:01:44 +00002523static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002524 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002525 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002526 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002527
Peter Collingbourne7a730022010-11-23 20:45:58 +00002528 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002529 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002530
Chris Lattner6b6b5372008-06-26 18:38:35 +00002531 // Make sure that there is a string literal as the annotation's single
2532 // argument.
2533 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002534 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002535 return;
2536 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002537
2538 // Don't duplicate annotations that are already set.
2539 for (specific_attr_iterator<AnnotateAttr>
2540 i = D->specific_attr_begin<AnnotateAttr>(),
2541 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2542 if ((*i)->getAnnotation() == SE->getString())
2543 return;
2544 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002545 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002546 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002547}
2548
Chandler Carruth1b03c872011-07-02 00:01:44 +00002549static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002550 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002551 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002553 return;
2554 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002555
2556 //FIXME: The C++0x version of this attribute has more limited applicabilty
2557 // than GNU's, and should error out when it is used to specify a
2558 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002559
Chris Lattner545dd342008-06-28 23:36:30 +00002560 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002561 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002562 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002563 }
Mike Stumpbf916502009-07-24 19:02:52 +00002564
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002565 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002566}
2567
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002568void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002569 // FIXME: Handle pack-expansions here.
2570 if (DiagnoseUnexpandedParameterPack(E))
2571 return;
2572
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002573 if (E->isTypeDependent() || E->isValueDependent()) {
2574 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002575 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002576 return;
2577 }
2578
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002579 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002580 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002581 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002582 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2583 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2584 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002585 return;
2586 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002587 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002588 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2589 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002590 return;
2591 }
2592
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002593 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Sean Huntcf807c42010-08-18 23:23:40 +00002594}
2595
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002596void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002597 // FIXME: Cache the number on the Attr object if non-dependent?
2598 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002599 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002600 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002601}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002602
Chandler Carruthd309c812011-07-01 23:49:16 +00002603/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002604/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002605///
Mike Stumpbf916502009-07-24 19:02:52 +00002606/// Despite what would be logical, the mode attribute is a decl attribute, not a
2607/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2608/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002609static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002610 // This attribute isn't documented, but glibc uses it. It changes
2611 // the width of an int or unsigned int to the specified size.
2612
2613 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002614 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002615 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002616
Chris Lattnerfbf13472008-06-27 22:18:37 +00002617
2618 IdentifierInfo *Name = Attr.getParameterName();
2619 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002620 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002621 return;
2622 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002623
Chris Lattner5f9e2722011-07-23 10:55:15 +00002624 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002625
2626 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002627 if (Str.startswith("__") && Str.endswith("__"))
2628 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002629
2630 unsigned DestWidth = 0;
2631 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002632 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002633 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002634 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002635 switch (Str[0]) {
2636 case 'Q': DestWidth = 8; break;
2637 case 'H': DestWidth = 16; break;
2638 case 'S': DestWidth = 32; break;
2639 case 'D': DestWidth = 64; break;
2640 case 'X': DestWidth = 96; break;
2641 case 'T': DestWidth = 128; break;
2642 }
2643 if (Str[1] == 'F') {
2644 IntegerMode = false;
2645 } else if (Str[1] == 'C') {
2646 IntegerMode = false;
2647 ComplexMode = true;
2648 } else if (Str[1] != 'I') {
2649 DestWidth = 0;
2650 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002651 break;
2652 case 4:
2653 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2654 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002655 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002656 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002657 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002658 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002659 break;
2660 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002661 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002662 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002663 break;
2664 }
2665
2666 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002667 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002668 OldTy = TD->getUnderlyingType();
2669 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2670 OldTy = VD->getType();
2671 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002672 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002673 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002674 return;
2675 }
Eli Friedman73397492009-03-03 06:41:03 +00002676
John McCall183700f2009-09-21 23:43:11 +00002677 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002678 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2679 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002680 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002681 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2682 } else if (ComplexMode) {
2683 if (!OldTy->isComplexType())
2684 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2685 } else {
2686 if (!OldTy->isFloatingType())
2687 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2688 }
2689
Mike Stump390b4cc2009-05-16 07:39:55 +00002690 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2691 // and friends, at least with glibc.
2692 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2693 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002694 // FIXME: Make sure floating-point mappings are accurate
2695 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002696 QualType NewTy;
2697 switch (DestWidth) {
2698 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002699 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002700 return;
2701 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002702 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002703 return;
2704 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002705 if (!IntegerMode) {
2706 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2707 return;
2708 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002709 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002710 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002711 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002712 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002713 break;
2714 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002715 if (!IntegerMode) {
2716 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2717 return;
2718 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002719 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002720 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002721 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002722 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002723 break;
2724 case 32:
2725 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002726 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002727 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002728 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002729 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002730 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002731 break;
2732 case 64:
2733 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002734 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002735 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002736 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002737 NewTy = S.Context.LongTy;
2738 else
2739 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002740 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002741 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002742 NewTy = S.Context.UnsignedLongTy;
2743 else
2744 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002745 break;
Eli Friedman73397492009-03-03 06:41:03 +00002746 case 96:
2747 NewTy = S.Context.LongDoubleTy;
2748 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002749 case 128:
2750 if (!IntegerMode) {
2751 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2752 return;
2753 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002754 if (OldTy->isSignedIntegerType())
2755 NewTy = S.Context.Int128Ty;
2756 else
2757 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002758 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002759 }
2760
Eli Friedman73397492009-03-03 06:41:03 +00002761 if (ComplexMode) {
2762 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002763 }
2764
2765 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002766 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002767 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002768 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002769 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002770 cast<ValueDecl>(D)->setType(NewTy);
2771}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002772
Chandler Carruth1b03c872011-07-02 00:01:44 +00002773static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002774 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002775 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002776 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002777
Chandler Carruth87c44602011-07-01 23:49:12 +00002778 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002780 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002781 return;
2782 }
Mike Stumpbf916502009-07-24 19:02:52 +00002783
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002784 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002785}
2786
Chandler Carruth1b03c872011-07-02 00:01:44 +00002787static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002788 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002789 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002790 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002791
Mike Stumpbf916502009-07-24 19:02:52 +00002792
Chandler Carruth87c44602011-07-01 23:49:12 +00002793 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002795 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002796 return;
2797 }
Mike Stumpbf916502009-07-24 19:02:52 +00002798
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002799 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002800}
2801
Chandler Carruth1b03c872011-07-02 00:01:44 +00002802static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2803 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002804 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002805 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002806 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002807
Chris Lattner7255a2d2010-06-22 00:03:40 +00002808
Chandler Carruth87c44602011-07-01 23:49:12 +00002809 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002811 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002812 return;
2813 }
2814
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002815 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002816 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002817}
2818
Chandler Carruth1b03c872011-07-02 00:01:44 +00002819static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002820 if (S.LangOpts.CUDA) {
2821 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002822 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2824 return;
2825 }
2826
Chandler Carruth87c44602011-07-01 23:49:12 +00002827 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002829 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002830 return;
2831 }
2832
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002833 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002834 } else {
2835 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2836 }
2837}
2838
Chandler Carruth1b03c872011-07-02 00:01:44 +00002839static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002840 if (S.LangOpts.CUDA) {
2841 // check the attribute arguments.
2842 if (Attr.getNumArgs() != 0) {
2843 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2844 return;
2845 }
2846
Chandler Carruth87c44602011-07-01 23:49:12 +00002847 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002848 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002849 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002850 return;
2851 }
2852
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002853 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002854 } else {
2855 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2856 }
2857}
2858
Chandler Carruth1b03c872011-07-02 00:01:44 +00002859static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002860 if (S.LangOpts.CUDA) {
2861 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002862 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002863 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002864
Chandler Carruth87c44602011-07-01 23:49:12 +00002865 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002866 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002867 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002868 return;
2869 }
2870
Chandler Carruth87c44602011-07-01 23:49:12 +00002871 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002872 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002873 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002874 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2875 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2876 << FD->getType()
2877 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2878 "void");
2879 } else {
2880 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2881 << FD->getType();
2882 }
2883 return;
2884 }
2885
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002886 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002887 } else {
2888 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2889 }
2890}
2891
Chandler Carruth1b03c872011-07-02 00:01:44 +00002892static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002893 if (S.LangOpts.CUDA) {
2894 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002895 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002896 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002897
Peter Collingbourneced76712010-12-01 03:15:31 +00002898
Chandler Carruth87c44602011-07-01 23:49:12 +00002899 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002901 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002902 return;
2903 }
2904
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002905 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002906 } else {
2907 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2908 }
2909}
2910
Chandler Carruth1b03c872011-07-02 00:01:44 +00002911static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002912 if (S.LangOpts.CUDA) {
2913 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002914 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002915 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002916
Peter Collingbourneced76712010-12-01 03:15:31 +00002917
Chandler Carruth87c44602011-07-01 23:49:12 +00002918 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002919 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002920 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002921 return;
2922 }
2923
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002924 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002925 } else {
2926 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2927 }
2928}
2929
Chandler Carruth1b03c872011-07-02 00:01:44 +00002930static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002931 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002932 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002933 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002934
Chandler Carruth87c44602011-07-01 23:49:12 +00002935 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002936 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002937 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002938 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002939 return;
2940 }
Mike Stumpbf916502009-07-24 19:02:52 +00002941
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002942 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002943 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002944 return;
2945 }
Mike Stumpbf916502009-07-24 19:02:52 +00002946
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002947 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002948}
2949
Chandler Carruth1b03c872011-07-02 00:01:44 +00002950static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002951 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002952
Chandler Carruth87c44602011-07-01 23:49:12 +00002953 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002954 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2955 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002956 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002957 return;
2958
Chandler Carruth87c44602011-07-01 23:49:12 +00002959 if (!isa<ObjCMethodDecl>(D)) {
2960 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2961 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002962 return;
2963 }
2964
Chandler Carruth87c44602011-07-01 23:49:12 +00002965 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002966 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002967 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002968 return;
2969 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002970 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002971 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002972 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002973 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002974 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002975 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002976 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002977 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002978 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002979 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002980 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002981 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002982 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002983 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002984 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002985 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002986 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002987 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002988 return;
2989 }
2990
Chris Lattner5f9e2722011-07-23 10:55:15 +00002991 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002992 PcsAttr::PCSType PCS;
2993 if (StrRef == "aapcs")
2994 PCS = PcsAttr::AAPCS;
2995 else if (StrRef == "aapcs-vfp")
2996 PCS = PcsAttr::AAPCS_VFP;
2997 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002998 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2999 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003000 return;
3001 }
3002
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003003 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003004 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003005 default:
3006 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003007 }
3008}
3009
Chandler Carruth1b03c872011-07-02 00:01:44 +00003010static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003011 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003012 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003013}
3014
John McCall711c52b2011-01-05 12:14:39 +00003015bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3016 if (attr.isInvalid())
3017 return true;
3018
Ted Kremenek831efae2011-04-15 05:49:29 +00003019 if ((attr.getNumArgs() != 0 &&
3020 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3021 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003022 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3023 attr.setInvalid();
3024 return true;
3025 }
3026
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003027 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3028 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003029 switch (attr.getKind()) {
3030 case AttributeList::AT_cdecl: CC = CC_C; break;
3031 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3032 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3033 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3034 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003035 case AttributeList::AT_pcs: {
3036 Expr *Arg = attr.getArg(0);
3037 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003038 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003039 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3040 << "pcs" << 1;
3041 attr.setInvalid();
3042 return true;
3043 }
3044
Chris Lattner5f9e2722011-07-23 10:55:15 +00003045 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003046 if (StrRef == "aapcs") {
3047 CC = CC_AAPCS;
3048 break;
3049 } else if (StrRef == "aapcs-vfp") {
3050 CC = CC_AAPCS_VFP;
3051 break;
3052 }
3053 // FALLS THROUGH
3054 }
David Blaikie7530c032012-01-17 06:56:22 +00003055 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003056 }
3057
3058 return false;
3059}
3060
Chandler Carruth1b03c872011-07-02 00:01:44 +00003061static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003062 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003063
3064 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003065 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003066 return;
3067
Chandler Carruth87c44602011-07-01 23:49:12 +00003068 if (!isa<ObjCMethodDecl>(D)) {
3069 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3070 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003071 return;
3072 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003073
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003074 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003075}
3076
3077/// Checks a regparm attribute, returning true if it is ill-formed and
3078/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003079bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3080 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003081 return true;
3082
Chandler Carruth87c44602011-07-01 23:49:12 +00003083 if (Attr.getNumArgs() != 1) {
3084 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3085 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003086 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003087 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003088
Chandler Carruth87c44602011-07-01 23:49:12 +00003089 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003090 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003091 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003092 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003093 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003094 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003095 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003096 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003097 }
3098
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003099 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003100 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003101 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003102 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003103 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003104 }
3105
John McCall711c52b2011-01-05 12:14:39 +00003106 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003107 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003108 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003109 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003110 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003111 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003112 }
3113
John McCall711c52b2011-01-05 12:14:39 +00003114 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003115}
3116
Chandler Carruth1b03c872011-07-02 00:01:44 +00003117static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003118 if (S.LangOpts.CUDA) {
3119 // check the attribute arguments.
3120 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003121 // FIXME: 0 is not okay.
3122 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003123 return;
3124 }
3125
Chandler Carruth87c44602011-07-01 23:49:12 +00003126 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003127 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003128 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003129 return;
3130 }
3131
3132 Expr *MaxThreadsExpr = Attr.getArg(0);
3133 llvm::APSInt MaxThreads(32);
3134 if (MaxThreadsExpr->isTypeDependent() ||
3135 MaxThreadsExpr->isValueDependent() ||
3136 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3137 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3138 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3139 return;
3140 }
3141
3142 llvm::APSInt MinBlocks(32);
3143 if (Attr.getNumArgs() > 1) {
3144 Expr *MinBlocksExpr = Attr.getArg(1);
3145 if (MinBlocksExpr->isTypeDependent() ||
3146 MinBlocksExpr->isValueDependent() ||
3147 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3149 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3150 return;
3151 }
3152 }
3153
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003154 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003155 MaxThreads.getZExtValue(),
3156 MinBlocks.getZExtValue()));
3157 } else {
3158 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3159 }
3160}
3161
Chris Lattner0744e5f2008-06-29 00:23:49 +00003162//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003163// Checker-specific attribute handlers.
3164//===----------------------------------------------------------------------===//
3165
John McCallc7ad3812011-01-25 03:31:58 +00003166static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003167 return type->isDependentType() ||
3168 type->isObjCObjectPointerType() ||
3169 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003170}
3171static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003172 return type->isDependentType() ||
3173 type->isPointerType() ||
3174 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003175}
3176
Chandler Carruth1b03c872011-07-02 00:01:44 +00003177static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003178 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003179 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003180 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003181 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003182 return;
3183 }
3184
3185 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003186 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003187 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3188 cf = false;
3189 } else {
3190 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3191 cf = true;
3192 }
3193
3194 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003195 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003196 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003197 return;
3198 }
3199
3200 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003201 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003202 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003203 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003204}
3205
Chandler Carruth1b03c872011-07-02 00:01:44 +00003206static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3207 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003208 if (!isa<ObjCMethodDecl>(D)) {
3209 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003210 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003211 return;
3212 }
3213
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003214 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003215}
3216
Chandler Carruth1b03c872011-07-02 00:01:44 +00003217static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3218 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003219
John McCallc7ad3812011-01-25 03:31:58 +00003220 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003221
Chandler Carruth87c44602011-07-01 23:49:12 +00003222 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003223 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003224 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003225 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003226 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
3227 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003228 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003229 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003230 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003231 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003232 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003233 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003234 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003235 return;
3236 }
Mike Stumpbf916502009-07-24 19:02:52 +00003237
John McCallc7ad3812011-01-25 03:31:58 +00003238 bool typeOK;
3239 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003240 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003241 default: llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003242 case AttributeList::AT_ns_returns_autoreleased:
3243 case AttributeList::AT_ns_returns_retained:
3244 case AttributeList::AT_ns_returns_not_retained:
3245 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3246 cf = false;
3247 break;
3248
3249 case AttributeList::AT_cf_returns_retained:
3250 case AttributeList::AT_cf_returns_not_retained:
3251 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3252 cf = true;
3253 break;
3254 }
3255
3256 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003257 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003258 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003259 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003260 }
Mike Stumpbf916502009-07-24 19:02:52 +00003261
Chandler Carruth87c44602011-07-01 23:49:12 +00003262 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003263 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003264 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003265 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003266 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003267 S.Context));
3268 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003269 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003270 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003271 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003272 return;
3273 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003274 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003275 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003276 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003277 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003278 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003279 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003280 return;
3281 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003282 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003283 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003284 return;
3285 };
3286}
3287
John McCalldc7c5ad2011-07-22 08:53:00 +00003288static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3289 const AttributeList &attr) {
3290 SourceLocation loc = attr.getLoc();
3291
3292 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3293
3294 if (!isa<ObjCMethodDecl>(method)) {
3295 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3296 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
3297 return;
3298 }
3299
3300 // Check that the method returns a normal pointer.
3301 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003302
3303 if (!resultType->isReferenceType() &&
3304 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003305 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3306 << SourceRange(loc)
3307 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3308
3309 // Drop the attribute.
3310 return;
3311 }
3312
3313 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003314 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003315}
3316
John McCall8dfac0b2011-09-30 05:12:12 +00003317/// Handle cf_audited_transfer and cf_unknown_transfer.
3318static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3319 if (!isa<FunctionDecl>(D)) {
3320 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3321 << A.getRange() << A.getName() << 0 /*function*/;
3322 return;
3323 }
3324
3325 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3326
3327 // Check whether there's a conflicting attribute already present.
3328 Attr *Existing;
3329 if (IsAudited) {
3330 Existing = D->getAttr<CFUnknownTransferAttr>();
3331 } else {
3332 Existing = D->getAttr<CFAuditedTransferAttr>();
3333 }
3334 if (Existing) {
3335 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3336 << A.getName()
3337 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3338 << A.getRange() << Existing->getRange();
3339 return;
3340 }
3341
3342 // All clear; add the attribute.
3343 if (IsAudited) {
3344 D->addAttr(
3345 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3346 } else {
3347 D->addAttr(
3348 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3349 }
3350}
3351
John McCallfe98da02011-09-29 07:17:38 +00003352static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3353 const AttributeList &Attr) {
3354 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3355 if (!RD || RD->isUnion()) {
3356 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3357 << Attr.getRange() << Attr.getName() << 14 /*struct */;
3358 }
3359
3360 IdentifierInfo *ParmName = Attr.getParameterName();
3361
3362 // In Objective-C, verify that the type names an Objective-C type.
3363 // We don't want to check this outside of ObjC because people sometimes
3364 // do crazy C declarations of Objective-C types.
3365 if (ParmName && S.getLangOptions().ObjC1) {
3366 // Check for an existing type with this name.
3367 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3368 Sema::LookupOrdinaryName);
3369 if (S.LookupName(R, Sc)) {
3370 NamedDecl *Target = R.getFoundDecl();
3371 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3372 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3373 S.Diag(Target->getLocStart(), diag::note_declared_at);
3374 }
3375 }
3376 }
3377
3378 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3379 ParmName));
3380}
3381
Chandler Carruth1b03c872011-07-02 00:01:44 +00003382static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3383 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003384 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003385
Chandler Carruth87c44602011-07-01 23:49:12 +00003386 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003387 << Attr.getRange() << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003388}
3389
Chandler Carruth1b03c872011-07-02 00:01:44 +00003390static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3391 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003392 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003393 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003394 << Attr.getRange() << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003395 return;
3396 }
3397
Chandler Carruth87c44602011-07-01 23:49:12 +00003398 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003399 QualType type = vd->getType();
3400
3401 if (!type->isDependentType() &&
3402 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003403 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003404 << type;
3405 return;
3406 }
3407
3408 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3409
3410 // If we have no lifetime yet, check the lifetime we're presumably
3411 // going to infer.
3412 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3413 lifetime = type->getObjCARCImplicitLifetime();
3414
3415 switch (lifetime) {
3416 case Qualifiers::OCL_None:
3417 assert(type->isDependentType() &&
3418 "didn't infer lifetime for non-dependent type?");
3419 break;
3420
3421 case Qualifiers::OCL_Weak: // meaningful
3422 case Qualifiers::OCL_Strong: // meaningful
3423 break;
3424
3425 case Qualifiers::OCL_ExplicitNone:
3426 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003427 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003428 << (lifetime == Qualifiers::OCL_Autoreleasing);
3429 break;
3430 }
3431
Chandler Carruth87c44602011-07-01 23:49:12 +00003432 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003433 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003434}
3435
Charles Davisf0122fe2010-02-16 18:27:26 +00003436static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3437 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00003438 Attr.getKind() == AttributeList::AT_dllexport ||
3439 Attr.getKind() == AttributeList::AT_uuid;
3440}
3441
3442//===----------------------------------------------------------------------===//
3443// Microsoft specific attribute handlers.
3444//===----------------------------------------------------------------------===//
3445
Chandler Carruth1b03c872011-07-02 00:01:44 +00003446static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003447 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003448 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003449 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003450 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003451
Francois Pichet11542142010-12-19 06:50:37 +00003452 Expr *Arg = Attr.getArg(0);
3453 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003454 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003455 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3456 << "uuid" << 1;
3457 return;
3458 }
3459
Chris Lattner5f9e2722011-07-23 10:55:15 +00003460 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003461
3462 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3463 StrRef.back() == '}';
3464
3465 // Validate GUID length.
3466 if (IsCurly && StrRef.size() != 38) {
3467 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3468 return;
3469 }
3470 if (!IsCurly && StrRef.size() != 36) {
3471 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3472 return;
3473 }
3474
3475 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3476 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003477 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003478 if (IsCurly) // Skip the optional '{'
3479 ++I;
3480
3481 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003482 if (i == 8 || i == 13 || i == 18 || i == 23) {
3483 if (*I != '-') {
3484 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3485 return;
3486 }
3487 } else if (!isxdigit(*I)) {
3488 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3489 return;
3490 }
3491 I++;
3492 }
Francois Pichet11542142010-12-19 06:50:37 +00003493
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003494 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003495 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003496 } else
Francois Pichet11542142010-12-19 06:50:37 +00003497 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003498}
3499
Ted Kremenekb71368d2009-05-09 02:44:38 +00003500//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003501// Top Level Sema Entry Points
3502//===----------------------------------------------------------------------===//
3503
Chandler Carruth1b03c872011-07-02 00:01:44 +00003504static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3505 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003506 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003507 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3508 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3509 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003510 default:
3511 break;
3512 }
3513}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003514
Chandler Carruth1b03c872011-07-02 00:01:44 +00003515static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3516 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003517 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003518 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3519 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00003520 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003521 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003522 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003523 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003524 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003525 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003526 case AttributeList::AT_neon_vector_type:
3527 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003528 // Ignore these, these are type attributes, handled by
3529 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003530 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003531 case AttributeList::AT_device:
3532 case AttributeList::AT_host:
3533 case AttributeList::AT_overloadable:
3534 // Ignore, this is a non-inheritable attribute, handled
3535 // by ProcessNonInheritableDeclAttr.
3536 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003537 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3538 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003539 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003540 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003541 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003542 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3543 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3544 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003545 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003546 handleDependencyAttr (S, D, Attr); break;
3547 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3548 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3549 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3550 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3551 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003552 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003553 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003554 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003555 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3556 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3557 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3558 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003559 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003560 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003561 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003562 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3563 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3564 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3565 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3566 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003567 case AttributeList::AT_ownership_returns:
3568 case AttributeList::AT_ownership_takes:
3569 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003570 handleOwnershipAttr (S, D, Attr); break;
3571 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3572 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3573 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3574 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3575 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003576
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003577 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003578 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003579 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003580 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003581
John McCalldc7c5ad2011-07-22 08:53:00 +00003582 case AttributeList::AT_objc_returns_inner_pointer:
3583 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3584
John McCallfe98da02011-09-29 07:17:38 +00003585 case AttributeList::AT_ns_bridged:
3586 handleNSBridgedAttr(S, scope, D, Attr); break;
3587
John McCall8dfac0b2011-09-30 05:12:12 +00003588 case AttributeList::AT_cf_audited_transfer:
3589 case AttributeList::AT_cf_unknown_transfer:
3590 handleCFTransferAttr(S, D, Attr); break;
3591
Ted Kremenekb71368d2009-05-09 02:44:38 +00003592 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003593 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003594 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003595 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003596 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003597
3598 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003599 case AttributeList::AT_ns_returns_not_retained:
3600 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003601 case AttributeList::AT_ns_returns_retained:
3602 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003603 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003604
Nate Begeman6f3d8382009-06-26 06:32:41 +00003605 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003606 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003607
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003608 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003609 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003610
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3612 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3613 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3614 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003615 case AttributeList::AT_arc_weakref_unavailable:
3616 handleArcWeakrefUnavailableAttr (S, D, Attr);
3617 break;
Ted Kremenek71207fc2012-01-05 22:47:47 +00003618 case AttributeList::AT_objc_requires_property_definitions:
3619 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00003620 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003621 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00003622 case AttributeList::AT_returns_twice:
3623 handleReturnsTwiceAttr(S, D, Attr);
3624 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003625 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3626 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3627 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003628 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003629 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3630 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3631 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003632 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003633 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003634 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003635 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003636 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003637 break;
John McCalld5313b02011-03-02 11:33:24 +00003638 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003639 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003640 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003641 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3642 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3643 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3644 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3645 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3646 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3647 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3648 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3649 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003650 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003651 // Just ignore
3652 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003653 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003654 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003655 break;
John McCall04a67a62010-02-05 21:31:56 +00003656 case AttributeList::AT_stdcall:
3657 case AttributeList::AT_cdecl:
3658 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003659 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003660 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003661 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003662 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003663 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003664 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003665 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003666 break;
Francois Pichet11542142010-12-19 06:50:37 +00003667 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003668 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003669 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003670
3671 // Thread safety attributes:
3672 case AttributeList::AT_guarded_var:
3673 handleGuardedVarAttr(S, D, Attr);
3674 break;
3675 case AttributeList::AT_pt_guarded_var:
3676 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3677 break;
3678 case AttributeList::AT_scoped_lockable:
3679 handleLockableAttr(S, D, Attr, /*scoped = */true);
3680 break;
3681 case AttributeList::AT_no_thread_safety_analysis:
3682 handleNoThreadSafetyAttr(S, D, Attr);
3683 break;
3684 case AttributeList::AT_lockable:
3685 handleLockableAttr(S, D, Attr);
3686 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003687 case AttributeList::AT_guarded_by:
3688 handleGuardedByAttr(S, D, Attr);
3689 break;
3690 case AttributeList::AT_pt_guarded_by:
3691 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3692 break;
3693 case AttributeList::AT_exclusive_lock_function:
3694 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3695 break;
3696 case AttributeList::AT_exclusive_locks_required:
3697 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3698 break;
3699 case AttributeList::AT_exclusive_trylock_function:
3700 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3701 break;
3702 case AttributeList::AT_lock_returned:
3703 handleLockReturnedAttr(S, D, Attr);
3704 break;
3705 case AttributeList::AT_locks_excluded:
3706 handleLocksExcludedAttr(S, D, Attr);
3707 break;
3708 case AttributeList::AT_shared_lock_function:
3709 handleLockFunAttr(S, D, Attr);
3710 break;
3711 case AttributeList::AT_shared_locks_required:
3712 handleLocksRequiredAttr(S, D, Attr);
3713 break;
3714 case AttributeList::AT_shared_trylock_function:
3715 handleTrylockFunAttr(S, D, Attr);
3716 break;
3717 case AttributeList::AT_unlock_function:
3718 handleUnlockFunAttr(S, D, Attr);
3719 break;
3720 case AttributeList::AT_acquired_before:
3721 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3722 break;
3723 case AttributeList::AT_acquired_after:
3724 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3725 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003726
Chris Lattner803d0802008-06-29 00:43:07 +00003727 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003728 // Ask target about the attribute.
3729 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3730 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003731 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3732 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003733 break;
3734 }
3735}
3736
Peter Collingbourne60700392011-01-21 02:08:45 +00003737/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3738/// the attribute applies to decls. If the attribute is a type attribute, just
3739/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3740/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003741static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3742 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003743 bool NonInheritable, bool Inheritable) {
3744 if (Attr.isInvalid())
3745 return;
3746
3747 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3748 // FIXME: Try to deal with other __declspec attributes!
3749 return;
3750
3751 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003752 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003753
3754 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003755 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003756}
3757
Chris Lattner803d0802008-06-29 00:43:07 +00003758/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3759/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003760void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003761 const AttributeList *AttrList,
3762 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003763 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003764 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003765 }
3766
3767 // GCC accepts
3768 // static int a9 __attribute__((weakref));
3769 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003770 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003771 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003772 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003773 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003774 }
3775}
3776
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003777// Annotation attributes are the only attributes allowed after an access
3778// specifier.
3779bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3780 const AttributeList *AttrList) {
3781 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3782 if (l->getKind() == AttributeList::AT_annotate) {
3783 handleAnnotateAttr(*this, ASDecl, *l);
3784 } else {
3785 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3786 return true;
3787 }
3788 }
3789
3790 return false;
3791}
3792
John McCalle82247a2011-10-01 05:17:03 +00003793/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3794/// contains any decl attributes that we should warn about.
3795static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3796 for ( ; A; A = A->getNext()) {
3797 // Only warn if the attribute is an unignored, non-type attribute.
3798 if (A->isUsedAsTypeAttr()) continue;
3799 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3800
3801 if (A->getKind() == AttributeList::UnknownAttribute) {
3802 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3803 << A->getName() << A->getRange();
3804 } else {
3805 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3806 << A->getName() << A->getRange();
3807 }
3808 }
3809}
3810
3811/// checkUnusedDeclAttributes - Given a declarator which is not being
3812/// used to build a declaration, complain about any decl attributes
3813/// which might be lying around on it.
3814void Sema::checkUnusedDeclAttributes(Declarator &D) {
3815 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3816 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3817 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3818 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3819}
3820
Ryan Flynne25ff832009-07-30 03:15:39 +00003821/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3822/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003823NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3824 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003825 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003826 NamedDecl *NewD = 0;
3827 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003828 FunctionDecl *NewFD;
3829 // FIXME: Missing call to CheckFunctionDeclaration().
3830 // FIXME: Mangling?
3831 // FIXME: Is the qualifier info correct?
3832 // FIXME: Is the DeclContext correct?
3833 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3834 Loc, Loc, DeclarationName(II),
3835 FD->getType(), FD->getTypeSourceInfo(),
3836 SC_None, SC_None,
3837 false/*isInlineSpecified*/,
3838 FD->hasPrototype(),
3839 false/*isConstexprSpecified*/);
3840 NewD = NewFD;
3841
3842 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003843 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003844
3845 // Fake up parameter variables; they are declared as if this were
3846 // a typedef.
3847 QualType FDTy = FD->getType();
3848 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3849 SmallVector<ParmVarDecl*, 16> Params;
3850 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3851 AE = FT->arg_type_end(); AI != AE; ++AI) {
3852 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3853 Param->setScopeInfo(0, Params.size());
3854 Params.push_back(Param);
3855 }
David Blaikie4278c652011-09-21 18:16:56 +00003856 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003857 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003858 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3859 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003860 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003861 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003862 VD->getStorageClass(),
3863 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003864 if (VD->getQualifier()) {
3865 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003866 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003867 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003868 }
3869 return NewD;
3870}
3871
3872/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3873/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003874void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003875 if (W.getUsed()) return; // only do this once
3876 W.setUsed(true);
3877 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3878 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003879 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003880 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3881 NDId->getName()));
3882 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003883 WeakTopLevelDecl.push_back(NewD);
3884 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3885 // to insert Decl at TU scope, sorry.
3886 DeclContext *SavedContext = CurContext;
3887 CurContext = Context.getTranslationUnitDecl();
3888 PushOnScopeChains(NewD, S);
3889 CurContext = SavedContext;
3890 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003891 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003892 }
3893}
3894
Chris Lattner0744e5f2008-06-29 00:23:49 +00003895/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3896/// it, apply them to D. This is a bit tricky because PD can have attributes
3897/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003898void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3899 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003900 // It's valid to "forward-declare" #pragma weak, in which case we
3901 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003902 if (Inheritable) {
3903 LoadExternalWeakUndeclaredIdentifiers();
3904 if (!WeakUndeclaredIdentifiers.empty()) {
3905 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3906 if (IdentifierInfo *Id = ND->getIdentifier()) {
3907 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3908 = WeakUndeclaredIdentifiers.find(Id);
3909 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3910 WeakInfo W = I->second;
3911 DeclApplyPragmaWeak(S, ND, W);
3912 WeakUndeclaredIdentifiers[Id] = W;
3913 }
John McCalld4aff0e2010-10-27 00:59:00 +00003914 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003915 }
3916 }
3917 }
3918
Chris Lattner0744e5f2008-06-29 00:23:49 +00003919 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003920 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003921 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003922
Chris Lattner0744e5f2008-06-29 00:23:49 +00003923 // Walk the declarator structure, applying decl attributes that were in a type
3924 // position to the decl itself. This handles cases like:
3925 // int *__attr__(x)** D;
3926 // when X is a decl attribute.
3927 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3928 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003929 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003930
Chris Lattner0744e5f2008-06-29 00:23:49 +00003931 // Finally, apply any attributes on the decl itself.
3932 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003933 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003934}
John McCall54abf7d2009-11-04 02:18:39 +00003935
John McCallf85e1932011-06-15 23:02:42 +00003936/// Is the given declaration allowed to use a forbidden type?
3937static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3938 // Private ivars are always okay. Unfortunately, people don't
3939 // always properly make their ivars private, even in system headers.
3940 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00003941 // Function declarations in sys headers will be marked unavailable.
3942 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
3943 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00003944 return false;
3945
3946 // Require it to be declared in a system header.
3947 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3948}
3949
3950/// Handle a delayed forbidden-type diagnostic.
3951static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3952 Decl *decl) {
3953 if (decl && isForbiddenTypeAllowed(S, decl)) {
3954 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3955 "this system declaration uses an unsupported type"));
3956 return;
3957 }
Fariborz Jahanian175fb102011-10-03 22:11:57 +00003958 if (S.getLangOptions().ObjCAutoRefCount)
3959 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
3960 // FIXME. we may want to supress diagnostics for all
3961 // kind of forbidden type messages on unavailable functions.
3962 if (FD->hasAttr<UnavailableAttr>() &&
3963 diag.getForbiddenTypeDiagnostic() ==
3964 diag::err_arc_array_param_no_ownership) {
3965 diag.Triggered = true;
3966 return;
3967 }
3968 }
John McCallf85e1932011-06-15 23:02:42 +00003969
3970 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3971 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3972 diag.Triggered = true;
3973}
3974
John McCalleee1d542011-02-14 07:13:47 +00003975// This duplicates a vector push_back but hides the need to know the
3976// size of the type.
3977void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3978 assert(StackSize <= StackCapacity);
3979
3980 // Grow the stack if necessary.
3981 if (StackSize == StackCapacity) {
3982 unsigned newCapacity = 2 * StackCapacity + 2;
3983 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3984 const char *oldBuffer = (const char*) Stack;
3985
3986 if (StackCapacity)
3987 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3988
3989 delete[] oldBuffer;
3990 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3991 StackCapacity = newCapacity;
3992 }
3993
3994 assert(StackSize < StackCapacity);
3995 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003996}
3997
John McCalleee1d542011-02-14 07:13:47 +00003998void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3999 Decl *decl) {
4000 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00004001
John McCalleee1d542011-02-14 07:13:47 +00004002 // Check the invariants.
4003 assert(DD.StackSize >= state.SavedStackSize);
4004 assert(state.SavedStackSize >= DD.ActiveStackBase);
4005 assert(DD.ParsingDepth > 0);
4006
4007 // Drop the parsing depth.
4008 DD.ParsingDepth--;
4009
4010 // If there are no active diagnostics, we're done.
4011 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00004012 return;
4013
John McCall2f514482010-01-27 03:50:35 +00004014 // We only want to actually emit delayed diagnostics when we
4015 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00004016 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00004017 // We emit all the active diagnostics, not just those starting
4018 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00004019 // decl spec and another for each declarator; in a decl group like:
4020 // deprecated_typedef foo, *bar, baz();
4021 // only the declarator pops will be passed decls. This is correct;
4022 // we really do need to consider delayed diagnostics from the decl spec
4023 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00004024 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4025 DelayedDiagnostic &diag = DD.Stack[i];
4026 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004027 continue;
4028
John McCalleee1d542011-02-14 07:13:47 +00004029 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004030 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00004031 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004032 break;
4033
4034 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00004035 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004036 break;
John McCallf85e1932011-06-15 23:02:42 +00004037
4038 case DelayedDiagnostic::ForbiddenType:
4039 handleDelayedForbiddenType(S, diag, decl);
4040 break;
John McCall2f514482010-01-27 03:50:35 +00004041 }
4042 }
4043 }
4044
John McCall58e6f342010-03-16 05:22:47 +00004045 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00004046 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00004047 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00004048
John McCalleee1d542011-02-14 07:13:47 +00004049 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00004050}
4051
4052static bool isDeclDeprecated(Decl *D) {
4053 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004054 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004055 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004056 // A category implicitly has the availability of the interface.
4057 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4058 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004059 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4060 return false;
4061}
4062
John McCall9c3087b2010-08-26 02:13:20 +00004063void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004064 Decl *Ctx) {
4065 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004066 return;
4067
John McCall2f514482010-01-27 03:50:35 +00004068 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004069 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004070 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004071 << DD.getDeprecationDecl()->getDeclName()
4072 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004073 else
4074 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004075 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004076}
4077
Chris Lattner5f9e2722011-07-23 10:55:15 +00004078void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004079 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004080 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004081 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004082 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
4083 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00004084 return;
4085 }
4086
4087 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004088 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004089 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004090 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004091 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4092 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004093 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00004094 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004095 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004096 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004097 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004098 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4099 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004100 }
John McCall54abf7d2009-11-04 02:18:39 +00004101}