blob: 0644103b44e118996dcabbe4320b506be55a6847 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000022#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000024#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000026using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000027using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000028
John McCall883cc2c2011-03-02 12:29:23 +000029/// These constants match the enumerated choices of
30/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000031enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000032 ExpectedFunction,
33 ExpectedUnion,
34 ExpectedVariableOrFunction,
35 ExpectedFunctionOrMethod,
36 ExpectedParameter,
37 ExpectedParameterOrMethod,
38 ExpectedFunctionMethodOrBlock,
39 ExpectedClassOrVirtualMethod,
40 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
42 ExpectedVirtualMethod,
43 ExpectedClassMember,
44 ExpectedVariable,
45 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000046 ExpectedVariableFunctionOrLabel,
47 ExpectedFieldOrGlobalVar
John McCall883cc2c2011-03-02 12:29:23 +000048};
49
Chris Lattnere5c5ee12008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruth87c44602011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000065
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070
John McCall183700f2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000072}
73
Daniel Dunbar35682492008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopesd20254f2009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
87 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000088}
89
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000102}
103
John McCall711c52b2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000110}
111
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000118 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000133}
134
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148}
149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000156 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000175
John McCall506b57e2010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000211
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221 return true;
222}
223
224///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000225/// \brief Check if passed in Decl is a field or potentially shared global var
226/// \return true if the Decl is a field or potentially shared global variable
227///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000228static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000229 if (isa<FieldDecl>(D))
230 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000231 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000232 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
233
234 return false;
235}
236
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000237/// \brief Check if the passed-in expression is of type int or bool.
238static bool isIntOrBool(Expr *Exp) {
239 QualType QT = Exp->getType();
240 return QT->isBooleanType() || QT->isIntegerType();
241}
242
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000243///
244/// \brief Check if passed in Decl is a pointer type.
245/// Note that this function may produce an error message.
246/// \return true if the Decl is a pointer type; false otherwise
247///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000248static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
249 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000250 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000251 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000252 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000253 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
254 << Attr.getName()->getName() << QT;
255 } else {
256 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
257 << Attr.getName();
258 }
259 return false;
260}
261
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000262/// \brief Checks that the passed in QualType either is of RecordType or points
263/// to RecordType. Returns the relevant RecordType, null if it does not exit.
264const RecordType *getRecordType(QualType QT) {
265 const RecordType *RT = QT->getAs<RecordType>();
266 // now check if we point to record type
267 if(!RT && QT->isPointerType()){
268 QualType PT = QT->getAs<PointerType>()->getPointeeType();
269 RT = PT->getAs<RecordType>();
270 }
271 return RT;
272}
273
274/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
275/// from Sidx, resolve to a lockable object. May flag an error.
276static bool checkAttrArgsAreLockableObjs(Sema & S, Decl *D,
277 const AttributeList & Attr,
278 int Sidx = 0,
279 bool ParamIdxOk = false) {
280 for(unsigned int Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
281 Expr *ArgExp = Attr.getArg(Idx);
282 if (ArgExp->isTypeDependent())
283 continue;
284
285 QualType Arg_QT = ArgExp->getType();
286
287 // Get record type.
288 // first see if we can just cast to record type, or point to record type
289 const RecordType *RT = getRecordType(Arg_QT);
290
291 // now check if we idx into a record type function param
292 if (!RT && ParamIdxOk) {
293 FunctionDecl *FD = dyn_cast <FunctionDecl>(D);
294 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
295 if(FD && IL) {
296 unsigned int NumParams = FD->getNumParams();
297 llvm::APInt ArgValue = IL->getValue();
298 uint64_t ParamIdx_from1 = ArgValue.getZExtValue();
299 uint64_t ParamIdx_from0 = ParamIdx_from1 - 1;
300 if(!ArgValue.isStrictlyPositive() || ParamIdx_from1 > NumParams) {
301 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
302 << Attr.getName() << Idx + 1 << NumParams;
303 return false;
304 }
305 Arg_QT = FD->getParamDecl(ParamIdx_from0)->getType();
306 RT = getRecordType(Arg_QT);
307 }
308 }
309
310 // Flag error if could not get record type for this argument
311 if (!RT) {
312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_class)
313 << Attr.getName();
314 return false;
315 }
316
317 // Flag error if the type is not lockable
318 if (!RT->getDecl()->getAttr<LockableAttr>()) {
319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_lockable)
320 << Attr.getName();
321 return false;
322 }
323 }
324 return true;
325}
326
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000327//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000328// Attribute Implementations
329//===----------------------------------------------------------------------===//
330
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000331// FIXME: All this manual attribute parsing code is gross. At the
332// least add some helper functions to check most argument patterns (#
333// and types of args).
334
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000335static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
336 bool pointer = false) {
337 assert(!Attr.isInvalid());
338
339 if (!checkAttributeNumArgs(S, Attr, 0))
340 return;
341
342 // D must be either a member field or global (potentially shared) variable.
343 if (!mayBeSharedVariable(D)) {
344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000345 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000346 return;
347 }
348
349 if (pointer && !checkIsPointer(S, D, Attr))
350 return;
351
352 if (pointer)
353 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getLoc(), S.Context));
354 else
355 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getLoc(), S.Context));
356}
357
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000358static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000359 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000360 assert(!Attr.isInvalid());
361
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000362 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000363 return;
364
365 // D must be either a member field or global (potentially shared) variable.
366 if (!mayBeSharedVariable(D)) {
367 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000368 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000369 return;
370 }
371
372 if (pointer && !checkIsPointer(S, D, Attr))
373 return;
374
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000375 // check that all arguments are lockable objects
376 if (!checkAttrArgsAreLockableObjs(S, D, Attr))
377 return;
378
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000379 if (pointer)
380 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getLoc(), S.Context));
381 else
382 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getLoc(), S.Context));
383}
384
385
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000386static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
387 bool scoped = false) {
388 assert(!Attr.isInvalid());
389
390 if (!checkAttributeNumArgs(S, Attr, 0))
391 return;
392
393 if (!isa<CXXRecordDecl>(D)) {
394 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
395 << Attr.getName() << ExpectedClass;
396 return;
397 }
398
399 if (scoped)
400 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getLoc(), S.Context));
401 else
402 D->addAttr(::new (S.Context) LockableAttr(Attr.getLoc(), S.Context));
403}
404
405static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
406 const AttributeList &Attr) {
407 assert(!Attr.isInvalid());
408
409 if (!checkAttributeNumArgs(S, Attr, 0))
410 return;
411
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000412 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000413 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
414 << Attr.getName() << ExpectedFunctionOrMethod;
415 return;
416 }
417
418 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getLoc(),
419 S.Context));
420}
421
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000422static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
423 bool before) {
424 assert(!Attr.isInvalid());
425
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000426 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000427 return;
428
429 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000430 ValueDecl *VD = dyn_cast<ValueDecl>(D);
431 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000432 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000433 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000434 return;
435 }
436
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000437 // Check that this attribute only applies to lockable types
438 QualType QT = VD->getType();
439 if (!QT->isDependentType()) {
440 const RecordType *RT = getRecordType(QT);
441 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
442 S.Diag(Attr.getLoc(), diag::err_attribute_decl_not_lockable)
443 << Attr.getName();
444 return;
445 }
446 }
447
448 // check that all arguments are lockable objects
449 if (!checkAttrArgsAreLockableObjs(S, D, Attr))
450 return;
451
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000452 if (before)
453 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getLoc(), S.Context));
454 else
455 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getLoc(), S.Context));
456}
457
458static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000460 assert(!Attr.isInvalid());
461
462 // zero or more arguments ok
463
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000464 // check that the attribute is applied to a function
465 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
467 << Attr.getName() << ExpectedFunctionOrMethod;
468 return;
469 }
470
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000471 // check that all arguments are lockable objects
472 if (!checkAttrArgsAreLockableObjs(S, D, Attr, 0, /*ParamIdxOk=*/true))
473 return;
474
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000475 if (exclusive)
476 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getLoc(),
477 S.Context));
478 else
479 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getLoc(),
480 S.Context));
481}
482
483static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000484 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000485 assert(!Attr.isInvalid());
486
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000487 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000488 return;
489
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000490
491 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000492 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
493 << Attr.getName() << ExpectedFunctionOrMethod;
494 return;
495 }
496
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000497 if (!isIntOrBool(Attr.getArg(0))) {
498 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
499 << Attr.getName();
500 return;
501 }
502
503 // check that all arguments are lockable objects
504 if (!checkAttrArgsAreLockableObjs(S, D, Attr, 1))
505 return;
506
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000507 if (exclusive)
508 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getLoc(),
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000509 S.Context));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000510 else
511 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getLoc(),
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000512 S.Context));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000513}
514
515static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000516 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000517 assert(!Attr.isInvalid());
518
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000519 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000520 return;
521
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000522 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000523 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
524 << Attr.getName() << ExpectedFunctionOrMethod;
525 return;
526 }
527
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000528 // check that all arguments are lockable objects
529 if (!checkAttrArgsAreLockableObjs(S, D, Attr))
530 return;
531
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000532 if (exclusive)
533 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getLoc(),
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000534 S.Context));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000535 else
536 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getLoc(),
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000537 S.Context));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000538}
539
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000541 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000542 assert(!Attr.isInvalid());
543
544 // zero or more arguments ok
545
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000546 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000547 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
548 << Attr.getName() << ExpectedFunctionOrMethod;
549 return;
550 }
551
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000552 // check that all arguments are lockable objects
553 if (!checkAttrArgsAreLockableObjs(S, D, Attr, 0, /*ParamIdxOk=*/true))
554 return;
555
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000556 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getLoc(), S.Context));
557}
558
559static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000560 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000561 assert(!Attr.isInvalid());
562
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000563 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000564 return;
565
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000566 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
568 << Attr.getName() << ExpectedFunctionOrMethod;
569 return;
570 }
571
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000572 // check that all arguments are lockable objects
573 if (!checkAttrArgsAreLockableObjs(S, D, Attr))
574 return;
575
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000576 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getLoc(), S.Context));
577}
578
579static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000580 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000581 assert(!Attr.isInvalid());
582
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000583 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 return;
585
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000586 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
588 << Attr.getName() << ExpectedFunctionOrMethod;
589 return;
590 }
591
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000592 // check that all arguments are lockable objects
593 if (!checkAttrArgsAreLockableObjs(S, D, Attr))
594 return;
595
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000596 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getLoc(), S.Context));
597}
598
599
Chandler Carruth1b03c872011-07-02 00:01:44 +0000600static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
601 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000602 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000603 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000604 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000605 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 }
Mike Stumpbf916502009-07-24 19:02:52 +0000607
Chris Lattner6b6b5372008-06-26 18:38:35 +0000608 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000609
610 Expr *sizeExpr;
611
612 // Special case where the argument is a template id.
613 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000614 CXXScopeSpec SS;
615 UnqualifiedId id;
616 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000617
618 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
619 if (Size.isInvalid())
620 return;
621
622 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000623 } else {
624 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000625 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000626 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000627
Peter Collingbourne7a730022010-11-23 20:45:58 +0000628 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000629 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000630
631 // Instantiate/Install the vector type, and let Sema build the type for us.
632 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000633 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000634 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000635 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000636 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000637
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000638 // Remember this typedef decl, we will need it later for diagnostics.
639 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000640 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000641}
642
Chandler Carruth1b03c872011-07-02 00:01:44 +0000643static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000644 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000645 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000646 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000647
Chandler Carruth87c44602011-07-01 23:49:12 +0000648 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Sean Huntcf807c42010-08-18 23:23:40 +0000649 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000650 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000651 // If the alignment is less than or equal to 8 bits, the packed attribute
652 // has no effect.
653 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000654 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000655 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000656 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000657 else
Sean Huntcf807c42010-08-18 23:23:40 +0000658 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000659 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661}
662
Chandler Carruth1b03c872011-07-02 00:01:44 +0000663static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000664 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000665 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
666 else
667 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
668}
669
Chandler Carruth1b03c872011-07-02 00:01:44 +0000670static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000671 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000672 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000673 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000674
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000675 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000676 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000677 if (MD->isInstanceMethod()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000678 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000679 return;
680 }
681
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000682 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000683}
684
Chandler Carruth1b03c872011-07-02 00:01:44 +0000685static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000686 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000687 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000688 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000689
690 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000691 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000692 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
693 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000694 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000695 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000696
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000697 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000698}
699
Chandler Carruth1b03c872011-07-02 00:01:44 +0000700static void handleIBOutletCollection(Sema &S, Decl *D,
701 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000702
703 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000704 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000705 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
706 return;
707 }
708
709 // The IBOutletCollection attributes only apply to instance variables of
710 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000711 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000712 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000713 return;
714 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000715 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000716 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
717 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
718 << VD->getType() << 0;
719 return;
720 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000721 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000722 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
723 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
724 << PD->getType() << 1;
725 return;
726 }
727
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000728 IdentifierInfo *II = Attr.getParameterName();
729 if (!II)
730 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000731
John McCallb3d87482010-08-24 05:47:05 +0000732 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000733 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000734 if (!TypeRep) {
735 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
736 return;
737 }
John McCallb3d87482010-08-24 05:47:05 +0000738 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000739 // Diagnose use of non-object type in iboutletcollection attribute.
740 // FIXME. Gnu attribute extension ignores use of builtin types in
741 // attributes. So, __attribute__((iboutletcollection(char))) will be
742 // treated as __attribute__((iboutletcollection())).
743 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
744 !QT->isObjCObjectType()) {
745 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
746 return;
747 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000748 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000749 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000750}
751
Chandler Carruthd309c812011-07-01 23:49:16 +0000752static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000753 if (const RecordType *UT = T->getAsUnionType())
754 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
755 RecordDecl *UD = UT->getDecl();
756 for (RecordDecl::field_iterator it = UD->field_begin(),
757 itend = UD->field_end(); it != itend; ++it) {
758 QualType QT = it->getType();
759 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
760 T = QT;
761 return;
762 }
763 }
764 }
765}
766
Chandler Carruth1b03c872011-07-02 00:01:44 +0000767static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000768 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
769 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000770 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000771 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000772 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000773 return;
774 }
Mike Stumpbf916502009-07-24 19:02:52 +0000775
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000776 // In C++ the implicit 'this' function parameter also counts, and they are
777 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000778 bool HasImplicitThisParam = isInstanceMethod(D);
779 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000780
781 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000782 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000783
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000784 for (AttributeList::arg_iterator I=Attr.arg_begin(),
785 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000786
787
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000788 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000789 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000790 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000791 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
792 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
794 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000795 return;
796 }
Mike Stumpbf916502009-07-24 19:02:52 +0000797
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000798 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000799
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000800 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000801 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000802 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000803 return;
804 }
Mike Stumpbf916502009-07-24 19:02:52 +0000805
Ted Kremenek465172f2008-07-21 22:09:15 +0000806 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000807 if (HasImplicitThisParam) {
808 if (x == 0) {
809 S.Diag(Attr.getLoc(),
810 diag::err_attribute_invalid_implicit_this_argument)
811 << "nonnull" << Ex->getSourceRange();
812 return;
813 }
814 --x;
815 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000816
817 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000818 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000819 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000820
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000821 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000822 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000823 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000824 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000825 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000826 }
Mike Stumpbf916502009-07-24 19:02:52 +0000827
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000828 NonNullArgs.push_back(x);
829 }
Mike Stumpbf916502009-07-24 19:02:52 +0000830
831 // If no arguments were specified to __attribute__((nonnull)) then all pointer
832 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000833 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000834 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
835 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000836 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000837 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000838 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000839 }
Mike Stumpbf916502009-07-24 19:02:52 +0000840
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000841 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000842 if (NonNullArgs.empty()) {
843 // Warn the trivial case only if attribute is not coming from a
844 // macro instantiation.
845 if (Attr.getLoc().isFileID())
846 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000847 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000848 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000849 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000850
851 unsigned* start = &NonNullArgs[0];
852 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000853 llvm::array_pod_sort(start, start + size);
Chandler Carruth87c44602011-07-01 23:49:12 +0000854 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000855 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000856}
857
Chandler Carruth1b03c872011-07-02 00:01:44 +0000858static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000859 // This attribute must be applied to a function declaration.
860 // The first argument to the attribute must be a string,
861 // the name of the resource, for example "malloc".
862 // The following arguments must be argument indexes, the arguments must be
863 // of integer type for Returns, otherwise of pointer type.
864 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000865 // after being held. free() should be __attribute((ownership_takes)), whereas
866 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000867
868 if (!AL.getParameterName()) {
869 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
870 << AL.getName()->getName() << 1;
871 return;
872 }
873 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000874 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000875 switch (AL.getKind()) {
876 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000877 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000878 if (AL.getNumArgs() < 1) {
879 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
880 return;
881 }
Jordy Rose2a479922010-08-12 08:54:03 +0000882 break;
883 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000884 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000885 if (AL.getNumArgs() < 1) {
886 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
887 return;
888 }
Jordy Rose2a479922010-08-12 08:54:03 +0000889 break;
890 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000891 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000892 if (AL.getNumArgs() > 1) {
893 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
894 << AL.getNumArgs() + 1;
895 return;
896 }
Jordy Rose2a479922010-08-12 08:54:03 +0000897 break;
898 default:
899 // This should never happen given how we are called.
900 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000901 }
902
Chandler Carruth87c44602011-07-01 23:49:12 +0000903 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000904 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
905 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000906 return;
907 }
908
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000909 // In C++ the implicit 'this' function parameter also counts, and they are
910 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000911 bool HasImplicitThisParam = isInstanceMethod(D);
912 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000913
Chris Lattner5f9e2722011-07-23 10:55:15 +0000914 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000915
916 // Normalize the argument, __foo__ becomes foo.
917 if (Module.startswith("__") && Module.endswith("__"))
918 Module = Module.substr(2, Module.size() - 4);
919
Chris Lattner5f9e2722011-07-23 10:55:15 +0000920 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000921
Jordy Rose2a479922010-08-12 08:54:03 +0000922 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
923 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000924
Peter Collingbourne7a730022010-11-23 20:45:58 +0000925 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000926 llvm::APSInt ArgNum(32);
927 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
928 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
929 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
930 << AL.getName()->getName() << IdxExpr->getSourceRange();
931 continue;
932 }
933
934 unsigned x = (unsigned) ArgNum.getZExtValue();
935
936 if (x > NumArgs || x < 1) {
937 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
938 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
939 continue;
940 }
941 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000942 if (HasImplicitThisParam) {
943 if (x == 0) {
944 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
945 << "ownership" << IdxExpr->getSourceRange();
946 return;
947 }
948 --x;
949 }
950
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000951 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000952 case OwnershipAttr::Takes:
953 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000954 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000955 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000956 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
957 // FIXME: Should also highlight argument in decl.
958 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000959 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000960 << "pointer"
961 << IdxExpr->getSourceRange();
962 continue;
963 }
964 break;
965 }
Sean Huntcf807c42010-08-18 23:23:40 +0000966 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000967 if (AL.getNumArgs() > 1) {
968 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000969 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000970 llvm::APSInt ArgNum(32);
971 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
972 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
973 S.Diag(AL.getLoc(), diag::err_ownership_type)
974 << "ownership_returns" << "integer"
975 << IdxExpr->getSourceRange();
976 return;
977 }
978 }
979 break;
980 }
Jordy Rose2a479922010-08-12 08:54:03 +0000981 default:
982 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000983 } // switch
984
985 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000986 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +0000987 i = D->specific_attr_begin<OwnershipAttr>(),
988 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +0000989 i != e; ++i) {
990 if ((*i)->getOwnKind() != K) {
991 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
992 I!=E; ++I) {
993 if (x == *I) {
994 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
995 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000996 }
997 }
998 }
999 }
1000 OwnershipArgs.push_back(x);
1001 }
1002
1003 unsigned* start = OwnershipArgs.data();
1004 unsigned size = OwnershipArgs.size();
1005 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001006
1007 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1008 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1009 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001010 }
Sean Huntcf807c42010-08-18 23:23:40 +00001011
Chandler Carruth87c44602011-07-01 23:49:12 +00001012 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001013 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001014}
1015
John McCall332bb2a2011-02-08 22:35:49 +00001016/// Whether this declaration has internal linkage for the purposes of
1017/// things that want to complain about things not have internal linkage.
1018static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1019 switch (D->getLinkage()) {
1020 case NoLinkage:
1021 case InternalLinkage:
1022 return true;
1023
1024 // Template instantiations that go from external to unique-external
1025 // shouldn't get diagnosed.
1026 case UniqueExternalLinkage:
1027 return true;
1028
1029 case ExternalLinkage:
1030 return false;
1031 }
1032 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001033 return false;
1034}
1035
Chandler Carruth1b03c872011-07-02 00:01:44 +00001036static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001037 // Check the attribute arguments.
1038 if (Attr.getNumArgs() > 1) {
1039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1040 return;
1041 }
1042
Chandler Carruth87c44602011-07-01 23:49:12 +00001043 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001045 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001046 return;
1047 }
1048
Chandler Carruth87c44602011-07-01 23:49:12 +00001049 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001050
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001051 // gcc rejects
1052 // class c {
1053 // static int a __attribute__((weakref ("v2")));
1054 // static int b() __attribute__((weakref ("f3")));
1055 // };
1056 // and ignores the attributes of
1057 // void f(void) {
1058 // static int a __attribute__((weakref ("v2")));
1059 // }
1060 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001061 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001062 if (!Ctx->isFileContext()) {
1063 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001064 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001065 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001066 }
1067
1068 // The GCC manual says
1069 //
1070 // At present, a declaration to which `weakref' is attached can only
1071 // be `static'.
1072 //
1073 // It also says
1074 //
1075 // Without a TARGET,
1076 // given as an argument to `weakref' or to `alias', `weakref' is
1077 // equivalent to `weak'.
1078 //
1079 // gcc 4.4.1 will accept
1080 // int a7 __attribute__((weakref));
1081 // as
1082 // int a7 __attribute__((weak));
1083 // This looks like a bug in gcc. We reject that for now. We should revisit
1084 // it if this behaviour is actually used.
1085
John McCall332bb2a2011-02-08 22:35:49 +00001086 if (!hasEffectivelyInternalLinkage(nd)) {
1087 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001088 return;
1089 }
1090
1091 // GCC rejects
1092 // static ((alias ("y"), weakref)).
1093 // Should we? How to check that weakref is before or after alias?
1094
1095 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001096 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001097 Arg = Arg->IgnoreParenCasts();
1098 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1099
Douglas Gregor5cee1192011-07-27 05:40:30 +00001100 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1102 << "weakref" << 1;
1103 return;
1104 }
1105 // GCC will accept anything as the argument of weakref. Should we
1106 // check for an existing decl?
Chandler Carruth87c44602011-07-01 23:49:12 +00001107 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001108 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001109 }
1110
Chandler Carruth87c44602011-07-01 23:49:12 +00001111 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001112}
1113
Chandler Carruth1b03c872011-07-02 00:01:44 +00001114static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001115 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001116 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001117 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001118 return;
1119 }
Mike Stumpbf916502009-07-24 19:02:52 +00001120
Peter Collingbourne7a730022010-11-23 20:45:58 +00001121 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001122 Arg = Arg->IgnoreParenCasts();
1123 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001124
Douglas Gregor5cee1192011-07-27 05:40:30 +00001125 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001126 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001127 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001128 return;
1129 }
Mike Stumpbf916502009-07-24 19:02:52 +00001130
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001131 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001132 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1133 return;
1134 }
1135
Chris Lattner6b6b5372008-06-26 18:38:35 +00001136 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001137
Chandler Carruth87c44602011-07-01 23:49:12 +00001138 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001139 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001140}
1141
Chandler Carruth1b03c872011-07-02 00:01:44 +00001142static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001143 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001144 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001145 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001146
Chandler Carruth87c44602011-07-01 23:49:12 +00001147 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001148 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001149 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001150 return;
1151 }
1152
Chandler Carruth87c44602011-07-01 23:49:12 +00001153 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001154}
1155
Chandler Carruth1b03c872011-07-02 00:01:44 +00001156static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1157 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001158 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001159 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001160 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1161 return;
1162 }
1163
Chandler Carruth87c44602011-07-01 23:49:12 +00001164 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001165 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001166 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001167 return;
1168 }
Mike Stumpbf916502009-07-24 19:02:52 +00001169
Chandler Carruth87c44602011-07-01 23:49:12 +00001170 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001171}
1172
Chandler Carruth1b03c872011-07-02 00:01:44 +00001173static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001174 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001175 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001176 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1177 return;
1178 }
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chandler Carruth87c44602011-07-01 23:49:12 +00001180 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001181 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001182 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001183 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001184 return;
1185 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001186 }
1187
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001188 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001189}
1190
Chandler Carruth1b03c872011-07-02 00:01:44 +00001191static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001192 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001193 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001194 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001195
Chandler Carruth87c44602011-07-01 23:49:12 +00001196 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001197}
1198
Chandler Carruth1b03c872011-07-02 00:01:44 +00001199static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001200 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001201 if (isa<VarDecl>(D))
1202 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001203 else
1204 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001205 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001206}
1207
Chandler Carruth1b03c872011-07-02 00:01:44 +00001208static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001209 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001210 if (isa<VarDecl>(D))
1211 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001212 else
1213 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001214 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001215}
1216
Chandler Carruth1b03c872011-07-02 00:01:44 +00001217static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001218 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001219
1220 if (S.CheckNoReturnAttr(attr)) return;
1221
Chandler Carruth87c44602011-07-01 23:49:12 +00001222 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001223 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001224 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001225 return;
1226 }
1227
Chandler Carruth87c44602011-07-01 23:49:12 +00001228 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001229}
1230
1231bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001232 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001233 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1234 attr.setInvalid();
1235 return true;
1236 }
1237
1238 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001239}
1240
Chandler Carruth1b03c872011-07-02 00:01:44 +00001241static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1242 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001243
1244 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1245 // because 'analyzer_noreturn' does not impact the type.
1246
Chandler Carruth1731e202011-07-11 23:30:35 +00001247 if(!checkAttributeNumArgs(S, Attr, 0))
1248 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001249
Chandler Carruth87c44602011-07-01 23:49:12 +00001250 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1251 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001252 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1253 && !VD->getType()->isFunctionPointerType())) {
1254 S.Diag(Attr.getLoc(),
1255 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1256 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001257 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001258 return;
1259 }
1260 }
1261
Chandler Carruth87c44602011-07-01 23:49:12 +00001262 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001263}
1264
John Thompson35cc9622010-08-09 21:53:52 +00001265// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001266static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001267/*
1268 Returning a Vector Class in Registers
1269
Eric Christopherf48f3672010-12-01 22:13:54 +00001270 According to the PPU ABI specifications, a class with a single member of
1271 vector type is returned in memory when used as the return value of a function.
1272 This results in inefficient code when implementing vector classes. To return
1273 the value in a single vector register, add the vecreturn attribute to the
1274 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001275
1276 Example:
1277
1278 struct Vector
1279 {
1280 __vector float xyzw;
1281 } __attribute__((vecreturn));
1282
1283 Vector Add(Vector lhs, Vector rhs)
1284 {
1285 Vector result;
1286 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1287 return result; // This will be returned in a register
1288 }
1289*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001290 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001291 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001292 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001293 return;
1294 }
1295
Chandler Carruth87c44602011-07-01 23:49:12 +00001296 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001297 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1298 return;
1299 }
1300
Chandler Carruth87c44602011-07-01 23:49:12 +00001301 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001302 int count = 0;
1303
1304 if (!isa<CXXRecordDecl>(record)) {
1305 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1306 return;
1307 }
1308
1309 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1310 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1311 return;
1312 }
1313
Eric Christopherf48f3672010-12-01 22:13:54 +00001314 for (RecordDecl::field_iterator iter = record->field_begin();
1315 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001316 if ((count == 1) || !iter->getType()->isVectorType()) {
1317 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1318 return;
1319 }
1320 count++;
1321 }
1322
Chandler Carruth87c44602011-07-01 23:49:12 +00001323 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001324}
1325
Chandler Carruth1b03c872011-07-02 00:01:44 +00001326static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001327 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001328 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001329 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001330 return;
1331 }
1332 // FIXME: Actually store the attribute on the declaration
1333}
1334
Chandler Carruth1b03c872011-07-02 00:01:44 +00001335static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001336 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001337 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001339 return;
1340 }
Mike Stumpbf916502009-07-24 19:02:52 +00001341
Chandler Carruth87c44602011-07-01 23:49:12 +00001342 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1343 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001345 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001346 return;
1347 }
Mike Stumpbf916502009-07-24 19:02:52 +00001348
Chandler Carruth87c44602011-07-01 23:49:12 +00001349 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001350}
1351
Chandler Carruth1b03c872011-07-02 00:01:44 +00001352static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001353 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001354 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1356 return;
1357 }
Mike Stumpbf916502009-07-24 19:02:52 +00001358
Chandler Carruth87c44602011-07-01 23:49:12 +00001359 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001360 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001361 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1362 return;
1363 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001364 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001365 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001366 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001367 return;
1368 }
Mike Stumpbf916502009-07-24 19:02:52 +00001369
Chandler Carruth87c44602011-07-01 23:49:12 +00001370 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001371}
1372
Chandler Carruth1b03c872011-07-02 00:01:44 +00001373static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001374 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001375 if (Attr.getNumArgs() > 1) {
1376 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001377 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001378 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001379
1380 int priority = 65535; // FIXME: Do not hardcode such constants.
1381 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001382 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001383 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001384 if (E->isTypeDependent() || E->isValueDependent() ||
1385 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001387 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001388 return;
1389 }
1390 priority = Idx.getZExtValue();
1391 }
Mike Stumpbf916502009-07-24 19:02:52 +00001392
Chandler Carruth87c44602011-07-01 23:49:12 +00001393 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001394 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001395 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001396 return;
1397 }
1398
Chandler Carruth87c44602011-07-01 23:49:12 +00001399 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001400 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001401}
1402
Chandler Carruth1b03c872011-07-02 00:01:44 +00001403static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001404 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001405 if (Attr.getNumArgs() > 1) {
1406 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001407 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001408 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001409
1410 int priority = 65535; // FIXME: Do not hardcode such constants.
1411 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001412 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001413 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001414 if (E->isTypeDependent() || E->isValueDependent() ||
1415 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001416 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001417 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001418 return;
1419 }
1420 priority = Idx.getZExtValue();
1421 }
Mike Stumpbf916502009-07-24 19:02:52 +00001422
Chandler Carruth87c44602011-07-01 23:49:12 +00001423 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001424 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001425 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001426 return;
1427 }
1428
Chandler Carruth87c44602011-07-01 23:49:12 +00001429 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001430 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001431}
1432
Chandler Carruth1b03c872011-07-02 00:01:44 +00001433static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001434 unsigned NumArgs = Attr.getNumArgs();
1435 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001436 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001437 return;
1438 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001439
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001440 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001441 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001442 if (NumArgs == 1) {
1443 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001444 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001445 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1446 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001447 return;
1448 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001449 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001450 }
Mike Stumpbf916502009-07-24 19:02:52 +00001451
Chandler Carruth87c44602011-07-01 23:49:12 +00001452 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453}
1454
Chandler Carruth1b03c872011-07-02 00:01:44 +00001455static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001456 unsigned NumArgs = Attr.getNumArgs();
1457 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001459 return;
1460 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001461
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001462 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001463 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001464 if (NumArgs == 1) {
1465 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001466 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001467 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001468 diag::err_attribute_not_string) << "unavailable";
1469 return;
1470 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001471 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001472 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001473 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001474}
1475
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001476static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1477 const AttributeList &Attr) {
1478 unsigned NumArgs = Attr.getNumArgs();
1479 if (NumArgs > 0) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1481 return;
1482 }
1483
1484 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1485 Attr.getLoc(), S.Context));
1486}
1487
Chandler Carruth1b03c872011-07-02 00:01:44 +00001488static void handleAvailabilityAttr(Sema &S, Decl *D,
1489 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001490 IdentifierInfo *Platform = Attr.getParameterName();
1491 SourceLocation PlatformLoc = Attr.getParameterLoc();
1492
Chris Lattner5f9e2722011-07-23 10:55:15 +00001493 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001494 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1495 if (PlatformName.empty()) {
1496 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1497 << Platform;
1498
1499 PlatformName = Platform->getName();
1500 }
1501
1502 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1503 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1504 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001505 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001506
1507 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1508 // of these steps are needed).
1509 if (Introduced.isValid() && Deprecated.isValid() &&
1510 !(Introduced.Version < Deprecated.Version)) {
1511 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1512 << 1 << PlatformName << Deprecated.Version.getAsString()
1513 << 0 << Introduced.Version.getAsString();
1514 return;
1515 }
1516
1517 if (Introduced.isValid() && Obsoleted.isValid() &&
1518 !(Introduced.Version < Obsoleted.Version)) {
1519 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1520 << 2 << PlatformName << Obsoleted.Version.getAsString()
1521 << 0 << Introduced.Version.getAsString();
1522 return;
1523 }
1524
1525 if (Deprecated.isValid() && Obsoleted.isValid() &&
1526 !(Deprecated.Version < Obsoleted.Version)) {
1527 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1528 << 2 << PlatformName << Obsoleted.Version.getAsString()
1529 << 1 << Deprecated.Version.getAsString();
1530 return;
1531 }
1532
Chandler Carruth87c44602011-07-01 23:49:12 +00001533 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001534 Platform,
1535 Introduced.Version,
1536 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001537 Obsoleted.Version,
1538 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001539}
1540
Chandler Carruth1b03c872011-07-02 00:01:44 +00001541static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001542 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001543 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001544 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001545
Peter Collingbourne7a730022010-11-23 20:45:58 +00001546 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001547 Arg = Arg->IgnoreParenCasts();
1548 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001549
Douglas Gregor5cee1192011-07-27 05:40:30 +00001550 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001552 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001553 return;
1554 }
Mike Stumpbf916502009-07-24 19:02:52 +00001555
Chris Lattner5f9e2722011-07-23 10:55:15 +00001556 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001557 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001558
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001559 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001560 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001561 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001562 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001563 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001564 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001565 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001566 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001567 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001568 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001569 return;
1570 }
Mike Stumpbf916502009-07-24 19:02:52 +00001571
Chandler Carruth87c44602011-07-01 23:49:12 +00001572 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001573}
1574
Chandler Carruth1b03c872011-07-02 00:01:44 +00001575static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1576 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001577 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1578 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001579 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001580 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001581 return;
1582 }
1583
Chandler Carruth87c44602011-07-01 23:49:12 +00001584 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1585 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001587 << "objc_method_family" << 1;
1588 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001590 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001591 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001592 return;
1593 }
1594
Chris Lattner5f9e2722011-07-23 10:55:15 +00001595 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001596 ObjCMethodFamilyAttr::FamilyKind family;
1597 if (param == "none")
1598 family = ObjCMethodFamilyAttr::OMF_None;
1599 else if (param == "alloc")
1600 family = ObjCMethodFamilyAttr::OMF_alloc;
1601 else if (param == "copy")
1602 family = ObjCMethodFamilyAttr::OMF_copy;
1603 else if (param == "init")
1604 family = ObjCMethodFamilyAttr::OMF_init;
1605 else if (param == "mutableCopy")
1606 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1607 else if (param == "new")
1608 family = ObjCMethodFamilyAttr::OMF_new;
1609 else {
1610 // Just warn and ignore it. This is future-proof against new
1611 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001612 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001613 return;
1614 }
1615
John McCallf85e1932011-06-15 23:02:42 +00001616 if (family == ObjCMethodFamilyAttr::OMF_init &&
1617 !method->getResultType()->isObjCObjectPointerType()) {
1618 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1619 << method->getResultType();
1620 // Ignore the attribute.
1621 return;
1622 }
1623
Chandler Carruth87c44602011-07-01 23:49:12 +00001624 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCallf85e1932011-06-15 23:02:42 +00001625 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001626}
1627
Chandler Carruth1b03c872011-07-02 00:01:44 +00001628static void handleObjCExceptionAttr(Sema &S, Decl *D,
1629 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001630 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001631 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001632
Chris Lattner0db29ec2009-02-14 08:09:34 +00001633 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1634 if (OCI == 0) {
1635 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1636 return;
1637 }
Mike Stumpbf916502009-07-24 19:02:52 +00001638
Sean Huntcf807c42010-08-18 23:23:40 +00001639 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001640}
1641
Chandler Carruth1b03c872011-07-02 00:01:44 +00001642static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001643 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001645 return;
1646 }
Richard Smith162e1c12011-04-15 14:24:37 +00001647 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001648 QualType T = TD->getUnderlyingType();
1649 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001650 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001651 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1652 return;
1653 }
1654 }
Sean Huntcf807c42010-08-18 23:23:40 +00001655 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001656}
1657
Mike Stumpbf916502009-07-24 19:02:52 +00001658static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001659handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001660 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001662 return;
1663 }
1664
1665 if (!isa<FunctionDecl>(D)) {
1666 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1667 return;
1668 }
1669
Sean Huntcf807c42010-08-18 23:23:40 +00001670 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001671}
1672
Chandler Carruth1b03c872011-07-02 00:01:44 +00001673static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001674 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001675 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001676 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001677 return;
1678 }
Mike Stumpbf916502009-07-24 19:02:52 +00001679
Steve Naroff9eae5762008-09-18 16:44:58 +00001680 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001682 return;
1683 }
Mike Stumpbf916502009-07-24 19:02:52 +00001684
Sean Huntcf807c42010-08-18 23:23:40 +00001685 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001686 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001687 type = BlocksAttr::ByRef;
1688 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001689 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001690 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001691 return;
1692 }
Mike Stumpbf916502009-07-24 19:02:52 +00001693
Chandler Carruth87c44602011-07-01 23:49:12 +00001694 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001695}
1696
Chandler Carruth1b03c872011-07-02 00:01:44 +00001697static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001698 // check the attribute arguments.
1699 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001700 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001701 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001702 }
1703
Anders Carlsson77091822008-10-05 18:05:59 +00001704 int sentinel = 0;
1705 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001706 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001707 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001708 if (E->isTypeDependent() || E->isValueDependent() ||
1709 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001711 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001712 return;
1713 }
1714 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001715
Anders Carlsson77091822008-10-05 18:05:59 +00001716 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001717 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1718 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001719 return;
1720 }
1721 }
1722
1723 int nullPos = 0;
1724 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001725 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001726 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001727 if (E->isTypeDependent() || E->isValueDependent() ||
1728 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001729 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001730 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001731 return;
1732 }
1733 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001734
Anders Carlsson77091822008-10-05 18:05:59 +00001735 if (nullPos > 1 || nullPos < 0) {
1736 // FIXME: This error message could be improved, it would be nice
1737 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001738 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1739 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001740 return;
1741 }
1742 }
1743
Chandler Carruth87c44602011-07-01 23:49:12 +00001744 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall183700f2009-09-21 23:43:11 +00001745 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001746 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001747
Chris Lattner897cd902009-03-17 23:03:47 +00001748 if (isa<FunctionNoProtoType>(FT)) {
1749 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1750 return;
1751 }
Mike Stumpbf916502009-07-24 19:02:52 +00001752
Chris Lattner897cd902009-03-17 23:03:47 +00001753 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001754 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001755 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001756 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001757 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001758 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001759 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001760 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001761 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001762 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001763 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1764 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001765 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001766 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001767 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001768 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001769 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001770 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001771 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001772 int m = Ty->isFunctionPointerType() ? 0 : 1;
1773 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001774 return;
1775 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001776 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001777 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001778 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001779 return;
1780 }
Anders Carlsson77091822008-10-05 18:05:59 +00001781 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001783 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001784 return;
1785 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001786 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001787 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001788}
1789
Chandler Carruth1b03c872011-07-02 00:01:44 +00001790static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001791 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001792 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001793 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001794
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001795 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001797 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001798 return;
1799 }
Mike Stumpbf916502009-07-24 19:02:52 +00001800
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001801 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1802 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1803 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001804 return;
1805 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001806 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1807 if (MD->getResultType()->isVoidType()) {
1808 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1809 << Attr.getName() << 1;
1810 return;
1811 }
1812
Sean Huntcf807c42010-08-18 23:23:40 +00001813 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001814}
1815
Chandler Carruth1b03c872011-07-02 00:01:44 +00001816static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001817 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001818 if (Attr.hasParameterOrArguments()) {
1819 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001820 return;
1821 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001822
Chandler Carruth87c44602011-07-01 23:49:12 +00001823 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1824 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1825 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001826 return;
1827 }
1828
Chandler Carruth87c44602011-07-01 23:49:12 +00001829 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001830
1831 // 'weak' only applies to declarations with external linkage.
1832 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001833 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001834 return;
1835 }
Mike Stumpbf916502009-07-24 19:02:52 +00001836
Chandler Carruth87c44602011-07-01 23:49:12 +00001837 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001838}
1839
Chandler Carruth1b03c872011-07-02 00:01:44 +00001840static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001841 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001842 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001843 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001844
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001845
1846 // weak_import only applies to variable & function declarations.
1847 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001848 if (!D->canBeWeakImported(isDef)) {
1849 if (isDef)
1850 S.Diag(Attr.getLoc(),
1851 diag::warn_attribute_weak_import_invalid_on_definition)
1852 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001853 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001854 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001855 isa<ObjCInterfaceDecl>(D))) {
1856 // Nothing to warn about here.
1857 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001859 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001860
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001861 return;
1862 }
1863
Sean Huntcf807c42010-08-18 23:23:40 +00001864 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001865}
1866
Chandler Carruth1b03c872011-07-02 00:01:44 +00001867static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1868 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001869 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001870 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001871 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001872
1873 unsigned WGSize[3];
1874 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001875 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001876 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001877 if (E->isTypeDependent() || E->isValueDependent() ||
1878 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1880 << "reqd_work_group_size" << E->getSourceRange();
1881 return;
1882 }
1883 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1884 }
Sean Huntcf807c42010-08-18 23:23:40 +00001885 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1886 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001887 WGSize[2]));
1888}
1889
Chandler Carruth1b03c872011-07-02 00:01:44 +00001890static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001891 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001892 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001893 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001894
1895 // Make sure that there is a string literal as the sections's single
1896 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001897 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001898 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001899 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001900 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001901 return;
1902 }
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Chris Lattner797c3c42009-08-10 19:03:04 +00001904 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001905 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001906 if (!Error.empty()) {
1907 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1908 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001909 return;
1910 }
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001912 // This attribute cannot be applied to local variables.
1913 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1914 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1915 return;
1916 }
1917
Eric Christopherf48f3672010-12-01 22:13:54 +00001918 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1919 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001920}
1921
Chris Lattner6b6b5372008-06-26 18:38:35 +00001922
Chandler Carruth1b03c872011-07-02 00:01:44 +00001923static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001924 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001925 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001927 return;
1928 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001929
Chandler Carruth87c44602011-07-01 23:49:12 +00001930 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001931 if (Existing->getLocation().isInvalid())
1932 Existing->setLocation(Attr.getLoc());
1933 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001934 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001935 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001936}
1937
Chandler Carruth1b03c872011-07-02 00:01:44 +00001938static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001939 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001940 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001942 return;
1943 }
Mike Stumpbf916502009-07-24 19:02:52 +00001944
Chandler Carruth87c44602011-07-01 23:49:12 +00001945 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001946 if (Existing->getLocation().isInvalid())
1947 Existing->setLocation(Attr.getLoc());
1948 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001949 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001950 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001951}
1952
Chandler Carruth1b03c872011-07-02 00:01:44 +00001953static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001954 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001955 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001956 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001957
Chandler Carruth87c44602011-07-01 23:49:12 +00001958 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001959}
1960
Chandler Carruth1b03c872011-07-02 00:01:44 +00001961static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001962 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001963 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1964 return;
1965 }
Mike Stumpbf916502009-07-24 19:02:52 +00001966
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001967 if (Attr.getNumArgs() != 0) {
1968 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1969 return;
1970 }
Mike Stumpbf916502009-07-24 19:02:52 +00001971
Chandler Carruth87c44602011-07-01 23:49:12 +00001972 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00001973
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001974 if (!VD || !VD->hasLocalStorage()) {
1975 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1976 return;
1977 }
Mike Stumpbf916502009-07-24 19:02:52 +00001978
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001979 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001980 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001981 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001982 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1983 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001984 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001985 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001986 Attr.getParameterName();
1987 return;
1988 }
Mike Stumpbf916502009-07-24 19:02:52 +00001989
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001990 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1991 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001992 S.Diag(Attr.getParameterLoc(),
1993 diag::err_attribute_cleanup_arg_not_function)
1994 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001995 return;
1996 }
1997
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001998 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001999 S.Diag(Attr.getParameterLoc(),
2000 diag::err_attribute_cleanup_func_must_take_one_arg)
2001 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002002 return;
2003 }
Mike Stumpbf916502009-07-24 19:02:52 +00002004
Anders Carlsson89941c12009-02-07 23:16:50 +00002005 // We're currently more strict than GCC about what function types we accept.
2006 // If this ever proves to be a problem it should be easy to fix.
2007 QualType Ty = S.Context.getPointerType(VD->getType());
2008 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002009 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2010 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002011 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002012 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2013 Attr.getParameterName() << ParamTy << Ty;
2014 return;
2015 }
Mike Stumpbf916502009-07-24 19:02:52 +00002016
Chandler Carruth87c44602011-07-01 23:49:12 +00002017 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00002018 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002019}
2020
Mike Stumpbf916502009-07-24 19:02:52 +00002021/// Handle __attribute__((format_arg((idx)))) attribute based on
2022/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002023static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002024 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002025 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002026
Chandler Carruth87c44602011-07-01 23:49:12 +00002027 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002028 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002029 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002030 return;
2031 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002032
2033 // In C++ the implicit 'this' function parameter also counts, and they are
2034 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002035 bool HasImplicitThisParam = isInstanceMethod(D);
2036 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002037 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002038
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002039 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002040 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002041 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002042 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2043 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002044 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2045 << "format" << 2 << IdxExpr->getSourceRange();
2046 return;
2047 }
Mike Stumpbf916502009-07-24 19:02:52 +00002048
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002049 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2051 << "format" << 2 << IdxExpr->getSourceRange();
2052 return;
2053 }
Mike Stumpbf916502009-07-24 19:02:52 +00002054
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002055 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002056
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002057 if (HasImplicitThisParam) {
2058 if (ArgIdx == 0) {
2059 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2060 << "format_arg" << IdxExpr->getSourceRange();
2061 return;
2062 }
2063 ArgIdx--;
2064 }
2065
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002066 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002067 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002068
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002069 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2070 if (not_nsstring_type &&
2071 !isCFStringType(Ty, S.Context) &&
2072 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002073 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002074 // FIXME: Should highlight the actual expression that has the wrong type.
2075 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002076 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002077 << IdxExpr->getSourceRange();
2078 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002079 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002080 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002081 if (!isNSStringType(Ty, S.Context) &&
2082 !isCFStringType(Ty, S.Context) &&
2083 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002084 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002085 // FIXME: Should highlight the actual expression that has the wrong type.
2086 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002087 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002088 << IdxExpr->getSourceRange();
2089 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002090 }
2091
Chandler Carruth87c44602011-07-01 23:49:12 +00002092 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002093 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002094}
2095
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002096enum FormatAttrKind {
2097 CFStringFormat,
2098 NSStringFormat,
2099 StrftimeFormat,
2100 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002101 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002102 InvalidFormat
2103};
2104
2105/// getFormatAttrKind - Map from format attribute names to supported format
2106/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002107static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002108 // Check for formats that get handled specially.
2109 if (Format == "NSString")
2110 return NSStringFormat;
2111 if (Format == "CFString")
2112 return CFStringFormat;
2113 if (Format == "strftime")
2114 return StrftimeFormat;
2115
2116 // Otherwise, check for supported formats.
2117 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2118 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
2119 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002120 Format == "zcmn_err" ||
2121 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002122 return SupportedFormat;
2123
Duncan Sandsbc525952010-03-23 14:44:19 +00002124 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2125 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002126 return IgnoredFormat;
2127
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002128 return InvalidFormat;
2129}
2130
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002131/// Handle __attribute__((init_priority(priority))) attributes based on
2132/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002133static void handleInitPriorityAttr(Sema &S, Decl *D,
2134 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002135 if (!S.getLangOptions().CPlusPlus) {
2136 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2137 return;
2138 }
2139
Chandler Carruth87c44602011-07-01 23:49:12 +00002140 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002141 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2142 Attr.setInvalid();
2143 return;
2144 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002145 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002146 if (S.Context.getAsArrayType(T))
2147 T = S.Context.getBaseElementType(T);
2148 if (!T->getAs<RecordType>()) {
2149 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2150 Attr.setInvalid();
2151 return;
2152 }
2153
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002154 if (Attr.getNumArgs() != 1) {
2155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2156 Attr.setInvalid();
2157 return;
2158 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002159 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002160
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002161 llvm::APSInt priority(32);
2162 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2163 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2164 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2165 << "init_priority" << priorityExpr->getSourceRange();
2166 Attr.setInvalid();
2167 return;
2168 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002169 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002170 if (prioritynum < 101 || prioritynum > 65535) {
2171 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2172 << priorityExpr->getSourceRange();
2173 Attr.setInvalid();
2174 return;
2175 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002176 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002177 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002178}
2179
Mike Stumpbf916502009-07-24 19:02:52 +00002180/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2181/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002182static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002183
Chris Lattner545dd342008-06-28 23:36:30 +00002184 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002186 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002187 return;
2188 }
2189
Chris Lattner545dd342008-06-28 23:36:30 +00002190 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002191 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002192 return;
2193 }
2194
Chandler Carruth87c44602011-07-01 23:49:12 +00002195 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002197 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002198 return;
2199 }
2200
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002201 // In C++ the implicit 'this' function parameter also counts, and they are
2202 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002203 bool HasImplicitThisParam = isInstanceMethod(D);
2204 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002205 unsigned FirstIdx = 1;
2206
Chris Lattner5f9e2722011-07-23 10:55:15 +00002207 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002208
2209 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002210 if (Format.startswith("__") && Format.endswith("__"))
2211 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002212
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002213 // Check for supported formats.
2214 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002215
2216 if (Kind == IgnoredFormat)
2217 return;
2218
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002219 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002220 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002221 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002222 return;
2223 }
2224
2225 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002226 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002227 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002228 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2229 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002231 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002232 return;
2233 }
2234
2235 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002236 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002237 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002238 return;
2239 }
2240
2241 // FIXME: Do we need to bounds check?
2242 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002243
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002244 if (HasImplicitThisParam) {
2245 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002246 S.Diag(Attr.getLoc(),
2247 diag::err_format_attribute_implicit_this_format_string)
2248 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002249 return;
2250 }
2251 ArgIdx--;
2252 }
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Chris Lattner6b6b5372008-06-26 18:38:35 +00002254 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002255 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002256
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002257 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002258 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002259 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2260 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002261 return;
2262 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002263 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002264 // FIXME: do we need to check if the type is NSString*? What are the
2265 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002266 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002267 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002268 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2269 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002270 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002271 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002272 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002273 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002274 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002275 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2276 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002277 return;
2278 }
2279
2280 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002281 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002282 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002283 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2284 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002285 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002286 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002287 return;
2288 }
2289
2290 // check if the function is variadic if the 3rd argument non-zero
2291 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002292 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002293 ++NumArgs; // +1 for ...
2294 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002295 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002296 return;
2297 }
2298 }
2299
Chris Lattner3c73c412008-11-19 08:23:25 +00002300 // strftime requires FirstArg to be 0 because it doesn't read from any
2301 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002302 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002303 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002304 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2305 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002306 return;
2307 }
2308 // if 0 it disables parameter checking (to use with e.g. va_list)
2309 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002310 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002311 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002312 return;
2313 }
2314
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002315 // Check whether we already have an equivalent format attribute.
2316 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002317 i = D->specific_attr_begin<FormatAttr>(),
2318 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002319 i != e ; ++i) {
2320 FormatAttr *f = *i;
2321 if (f->getType() == Format &&
2322 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2323 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2324 // If we don't have a valid location for this attribute, adopt the
2325 // location.
2326 if (f->getLocation().isInvalid())
2327 f->setLocation(Attr.getLoc());
2328 return;
2329 }
2330 }
2331
Chandler Carruth87c44602011-07-01 23:49:12 +00002332 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002333 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002334 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002335}
2336
Chandler Carruth1b03c872011-07-02 00:01:44 +00002337static void handleTransparentUnionAttr(Sema &S, Decl *D,
2338 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002339 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002340 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002341 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002342
Chris Lattner6b6b5372008-06-26 18:38:35 +00002343
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002344 // Try to find the underlying union declaration.
2345 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002346 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002347 if (TD && TD->getUnderlyingType()->isUnionType())
2348 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2349 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002350 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002351
2352 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002353 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002354 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002355 return;
2356 }
2357
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002358 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002359 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002360 diag::warn_transparent_union_attribute_not_definition);
2361 return;
2362 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002363
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002364 RecordDecl::field_iterator Field = RD->field_begin(),
2365 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002366 if (Field == FieldEnd) {
2367 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2368 return;
2369 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002370
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002371 FieldDecl *FirstField = *Field;
2372 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002373 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002374 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002375 diag::warn_transparent_union_attribute_floating)
2376 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002377 return;
2378 }
2379
2380 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2381 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2382 for (; Field != FieldEnd; ++Field) {
2383 QualType FieldType = Field->getType();
2384 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2385 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2386 // Warn if we drop the attribute.
2387 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002388 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002389 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002390 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002391 diag::warn_transparent_union_attribute_field_size_align)
2392 << isSize << Field->getDeclName() << FieldBits;
2393 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002394 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002395 diag::note_transparent_union_first_field_size_align)
2396 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002397 return;
2398 }
2399 }
2400
Sean Huntcf807c42010-08-18 23:23:40 +00002401 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002402}
2403
Chandler Carruth1b03c872011-07-02 00:01:44 +00002404static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002405 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002406 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002407 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002408
Peter Collingbourne7a730022010-11-23 20:45:58 +00002409 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002410 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002411
Chris Lattner6b6b5372008-06-26 18:38:35 +00002412 // Make sure that there is a string literal as the annotation's single
2413 // argument.
2414 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002415 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002416 return;
2417 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002418 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002419 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002420}
2421
Chandler Carruth1b03c872011-07-02 00:01:44 +00002422static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002423 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002424 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002426 return;
2427 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002428
2429 //FIXME: The C++0x version of this attribute has more limited applicabilty
2430 // than GNU's, and should error out when it is used to specify a
2431 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002432
Chris Lattner545dd342008-06-28 23:36:30 +00002433 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002434 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002435 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002436 }
Mike Stumpbf916502009-07-24 19:02:52 +00002437
Peter Collingbourne7a730022010-11-23 20:45:58 +00002438 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002439}
2440
2441void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2442 if (E->isTypeDependent() || E->isValueDependent()) {
2443 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002444 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002445 return;
2446 }
2447
Sean Huntcf807c42010-08-18 23:23:40 +00002448 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002449 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002450 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2451 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2452 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002453 return;
2454 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002455 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002456 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2457 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002458 return;
2459 }
2460
Sean Huntcf807c42010-08-18 23:23:40 +00002461 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2462}
2463
2464void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2465 // FIXME: Cache the number on the Attr object if non-dependent?
2466 // FIXME: Perform checking of type validity
2467 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2468 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002469}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002470
Chandler Carruthd309c812011-07-01 23:49:16 +00002471/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002472/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002473///
Mike Stumpbf916502009-07-24 19:02:52 +00002474/// Despite what would be logical, the mode attribute is a decl attribute, not a
2475/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2476/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002477static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002478 // This attribute isn't documented, but glibc uses it. It changes
2479 // the width of an int or unsigned int to the specified size.
2480
2481 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002482 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002483 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002484
Chris Lattnerfbf13472008-06-27 22:18:37 +00002485
2486 IdentifierInfo *Name = Attr.getParameterName();
2487 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002489 return;
2490 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002491
Chris Lattner5f9e2722011-07-23 10:55:15 +00002492 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002493
2494 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002495 if (Str.startswith("__") && Str.endswith("__"))
2496 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002497
2498 unsigned DestWidth = 0;
2499 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002500 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002501 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002502 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002503 switch (Str[0]) {
2504 case 'Q': DestWidth = 8; break;
2505 case 'H': DestWidth = 16; break;
2506 case 'S': DestWidth = 32; break;
2507 case 'D': DestWidth = 64; break;
2508 case 'X': DestWidth = 96; break;
2509 case 'T': DestWidth = 128; break;
2510 }
2511 if (Str[1] == 'F') {
2512 IntegerMode = false;
2513 } else if (Str[1] == 'C') {
2514 IntegerMode = false;
2515 ComplexMode = true;
2516 } else if (Str[1] != 'I') {
2517 DestWidth = 0;
2518 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002519 break;
2520 case 4:
2521 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2522 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002523 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002524 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002525 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002526 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002527 break;
2528 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002529 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002530 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002531 break;
2532 }
2533
2534 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002535 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002536 OldTy = TD->getUnderlyingType();
2537 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2538 OldTy = VD->getType();
2539 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002540 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2541 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002542 return;
2543 }
Eli Friedman73397492009-03-03 06:41:03 +00002544
John McCall183700f2009-09-21 23:43:11 +00002545 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002546 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2547 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002548 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002549 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2550 } else if (ComplexMode) {
2551 if (!OldTy->isComplexType())
2552 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2553 } else {
2554 if (!OldTy->isFloatingType())
2555 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2556 }
2557
Mike Stump390b4cc2009-05-16 07:39:55 +00002558 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2559 // and friends, at least with glibc.
2560 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2561 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002562 // FIXME: Make sure floating-point mappings are accurate
2563 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002564 QualType NewTy;
2565 switch (DestWidth) {
2566 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002567 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002568 return;
2569 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002570 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002571 return;
2572 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002573 if (!IntegerMode) {
2574 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2575 return;
2576 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002577 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002578 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002579 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002580 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002581 break;
2582 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002583 if (!IntegerMode) {
2584 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2585 return;
2586 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002587 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002588 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002589 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002590 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002591 break;
2592 case 32:
2593 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002594 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002595 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002596 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002597 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002598 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002599 break;
2600 case 64:
2601 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002602 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002603 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002604 if (S.Context.Target.getLongWidth() == 64)
2605 NewTy = S.Context.LongTy;
2606 else
2607 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002608 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002609 if (S.Context.Target.getLongWidth() == 64)
2610 NewTy = S.Context.UnsignedLongTy;
2611 else
2612 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002613 break;
Eli Friedman73397492009-03-03 06:41:03 +00002614 case 96:
2615 NewTy = S.Context.LongDoubleTy;
2616 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002617 case 128:
2618 if (!IntegerMode) {
2619 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2620 return;
2621 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002622 if (OldTy->isSignedIntegerType())
2623 NewTy = S.Context.Int128Ty;
2624 else
2625 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002626 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002627 }
2628
Eli Friedman73397492009-03-03 06:41:03 +00002629 if (ComplexMode) {
2630 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002631 }
2632
2633 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002634 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002635 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002636 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002637 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002638 cast<ValueDecl>(D)->setType(NewTy);
2639}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002640
Chandler Carruth1b03c872011-07-02 00:01:44 +00002641static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002642 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002643 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002644 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002645
Chandler Carruth87c44602011-07-01 23:49:12 +00002646 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002648 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002649 return;
2650 }
Mike Stumpbf916502009-07-24 19:02:52 +00002651
Chandler Carruth87c44602011-07-01 23:49:12 +00002652 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002653}
2654
Chandler Carruth1b03c872011-07-02 00:01:44 +00002655static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002656 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002657 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002658 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002659
Mike Stumpbf916502009-07-24 19:02:52 +00002660
Chandler Carruth87c44602011-07-01 23:49:12 +00002661 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002663 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002664 return;
2665 }
Mike Stumpbf916502009-07-24 19:02:52 +00002666
Chandler Carruth87c44602011-07-01 23:49:12 +00002667 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002668}
2669
Chandler Carruth1b03c872011-07-02 00:01:44 +00002670static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2671 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002672 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002673 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002674 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002675
Chris Lattner7255a2d2010-06-22 00:03:40 +00002676
Chandler Carruth87c44602011-07-01 23:49:12 +00002677 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002679 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002680 return;
2681 }
2682
Chandler Carruth87c44602011-07-01 23:49:12 +00002683 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002684 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002685}
2686
Chandler Carruth1b03c872011-07-02 00:01:44 +00002687static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002688 if (S.LangOpts.CUDA) {
2689 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002690 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002691 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2692 return;
2693 }
2694
Chandler Carruth87c44602011-07-01 23:49:12 +00002695 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002696 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002697 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002698 return;
2699 }
2700
Chandler Carruth87c44602011-07-01 23:49:12 +00002701 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002702 } else {
2703 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2704 }
2705}
2706
Chandler Carruth1b03c872011-07-02 00:01:44 +00002707static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002708 if (S.LangOpts.CUDA) {
2709 // check the attribute arguments.
2710 if (Attr.getNumArgs() != 0) {
2711 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2712 return;
2713 }
2714
Chandler Carruth87c44602011-07-01 23:49:12 +00002715 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002717 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002718 return;
2719 }
2720
Chandler Carruth87c44602011-07-01 23:49:12 +00002721 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002722 } else {
2723 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2724 }
2725}
2726
Chandler Carruth1b03c872011-07-02 00:01:44 +00002727static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002728 if (S.LangOpts.CUDA) {
2729 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002730 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002731 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002732
Chandler Carruth87c44602011-07-01 23:49:12 +00002733 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002735 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002736 return;
2737 }
2738
Chandler Carruth87c44602011-07-01 23:49:12 +00002739 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002740 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002741 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002742 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2743 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2744 << FD->getType()
2745 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2746 "void");
2747 } else {
2748 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2749 << FD->getType();
2750 }
2751 return;
2752 }
2753
Chandler Carruth87c44602011-07-01 23:49:12 +00002754 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002755 } else {
2756 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2757 }
2758}
2759
Chandler Carruth1b03c872011-07-02 00:01:44 +00002760static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002761 if (S.LangOpts.CUDA) {
2762 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002763 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002764 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002765
Peter Collingbourneced76712010-12-01 03:15:31 +00002766
Chandler Carruth87c44602011-07-01 23:49:12 +00002767 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002769 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002770 return;
2771 }
2772
Chandler Carruth87c44602011-07-01 23:49:12 +00002773 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002774 } else {
2775 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2776 }
2777}
2778
Chandler Carruth1b03c872011-07-02 00:01:44 +00002779static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002780 if (S.LangOpts.CUDA) {
2781 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002782 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002783 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002784
Peter Collingbourneced76712010-12-01 03:15:31 +00002785
Chandler Carruth87c44602011-07-01 23:49:12 +00002786 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002787 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002788 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002789 return;
2790 }
2791
Chandler Carruth87c44602011-07-01 23:49:12 +00002792 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002793 } else {
2794 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2795 }
2796}
2797
Chandler Carruth1b03c872011-07-02 00:01:44 +00002798static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002799 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002800 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002801 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002802
Chandler Carruth87c44602011-07-01 23:49:12 +00002803 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002804 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002805 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002806 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002807 return;
2808 }
Mike Stumpbf916502009-07-24 19:02:52 +00002809
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002810 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002811 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002812 return;
2813 }
Mike Stumpbf916502009-07-24 19:02:52 +00002814
Chandler Carruth87c44602011-07-01 23:49:12 +00002815 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002816}
2817
Chandler Carruth1b03c872011-07-02 00:01:44 +00002818static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002819 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002820
Chandler Carruth87c44602011-07-01 23:49:12 +00002821 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002822 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2823 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002824 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002825 return;
2826
Chandler Carruth87c44602011-07-01 23:49:12 +00002827 if (!isa<ObjCMethodDecl>(D)) {
2828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2829 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002830 return;
2831 }
2832
Chandler Carruth87c44602011-07-01 23:49:12 +00002833 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002834 case AttributeList::AT_fastcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002835 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002836 return;
2837 case AttributeList::AT_stdcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002838 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002839 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002840 case AttributeList::AT_thiscall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002841 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002842 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002843 case AttributeList::AT_cdecl:
Chandler Carruth87c44602011-07-01 23:49:12 +00002844 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002845 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002846 case AttributeList::AT_pascal:
Chandler Carruth87c44602011-07-01 23:49:12 +00002847 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002848 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002849 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002850 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002851 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002852 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002854 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002855 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002856 return;
2857 }
2858
Chris Lattner5f9e2722011-07-23 10:55:15 +00002859 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002860 PcsAttr::PCSType PCS;
2861 if (StrRef == "aapcs")
2862 PCS = PcsAttr::AAPCS;
2863 else if (StrRef == "aapcs-vfp")
2864 PCS = PcsAttr::AAPCS_VFP;
2865 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002866 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2867 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002868 return;
2869 }
2870
Chandler Carruth87c44602011-07-01 23:49:12 +00002871 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002872 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002873 default:
2874 llvm_unreachable("unexpected attribute kind");
2875 return;
2876 }
2877}
2878
Chandler Carruth1b03c872011-07-02 00:01:44 +00002879static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00002880 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00002881 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002882}
2883
John McCall711c52b2011-01-05 12:14:39 +00002884bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2885 if (attr.isInvalid())
2886 return true;
2887
Ted Kremenek831efae2011-04-15 05:49:29 +00002888 if ((attr.getNumArgs() != 0 &&
2889 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2890 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002891 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2892 attr.setInvalid();
2893 return true;
2894 }
2895
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002896 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2897 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002898 switch (attr.getKind()) {
2899 case AttributeList::AT_cdecl: CC = CC_C; break;
2900 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2901 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2902 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2903 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002904 case AttributeList::AT_pcs: {
2905 Expr *Arg = attr.getArg(0);
2906 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002907 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002908 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2909 << "pcs" << 1;
2910 attr.setInvalid();
2911 return true;
2912 }
2913
Chris Lattner5f9e2722011-07-23 10:55:15 +00002914 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002915 if (StrRef == "aapcs") {
2916 CC = CC_AAPCS;
2917 break;
2918 } else if (StrRef == "aapcs-vfp") {
2919 CC = CC_AAPCS_VFP;
2920 break;
2921 }
2922 // FALLS THROUGH
2923 }
John McCall711c52b2011-01-05 12:14:39 +00002924 default: llvm_unreachable("unexpected attribute kind"); return true;
2925 }
2926
2927 return false;
2928}
2929
Chandler Carruth1b03c872011-07-02 00:01:44 +00002930static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002931 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00002932
2933 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00002934 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00002935 return;
2936
Chandler Carruth87c44602011-07-01 23:49:12 +00002937 if (!isa<ObjCMethodDecl>(D)) {
2938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2939 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002940 return;
2941 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002942
Chandler Carruth87c44602011-07-01 23:49:12 +00002943 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00002944}
2945
2946/// Checks a regparm attribute, returning true if it is ill-formed and
2947/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00002948bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2949 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00002950 return true;
2951
Chandler Carruth87c44602011-07-01 23:49:12 +00002952 if (Attr.getNumArgs() != 1) {
2953 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2954 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002955 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002956 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002957
Chandler Carruth87c44602011-07-01 23:49:12 +00002958 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002959 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002960 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002961 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002962 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002963 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002964 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002965 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002966 }
2967
John McCall711c52b2011-01-05 12:14:39 +00002968 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002969 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002970 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002971 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002972 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002973 }
2974
John McCall711c52b2011-01-05 12:14:39 +00002975 numParams = NumParams.getZExtValue();
2976 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002977 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall711c52b2011-01-05 12:14:39 +00002978 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002979 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002980 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002981 }
2982
John McCall711c52b2011-01-05 12:14:39 +00002983 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002984}
2985
Chandler Carruth1b03c872011-07-02 00:01:44 +00002986static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00002987 if (S.LangOpts.CUDA) {
2988 // check the attribute arguments.
2989 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002990 // FIXME: 0 is not okay.
2991 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002992 return;
2993 }
2994
Chandler Carruth87c44602011-07-01 23:49:12 +00002995 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00002996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002997 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002998 return;
2999 }
3000
3001 Expr *MaxThreadsExpr = Attr.getArg(0);
3002 llvm::APSInt MaxThreads(32);
3003 if (MaxThreadsExpr->isTypeDependent() ||
3004 MaxThreadsExpr->isValueDependent() ||
3005 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3007 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3008 return;
3009 }
3010
3011 llvm::APSInt MinBlocks(32);
3012 if (Attr.getNumArgs() > 1) {
3013 Expr *MinBlocksExpr = Attr.getArg(1);
3014 if (MinBlocksExpr->isTypeDependent() ||
3015 MinBlocksExpr->isValueDependent() ||
3016 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3018 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3019 return;
3020 }
3021 }
3022
Chandler Carruth87c44602011-07-01 23:49:12 +00003023 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003024 MaxThreads.getZExtValue(),
3025 MinBlocks.getZExtValue()));
3026 } else {
3027 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3028 }
3029}
3030
Chris Lattner0744e5f2008-06-29 00:23:49 +00003031//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003032// Checker-specific attribute handlers.
3033//===----------------------------------------------------------------------===//
3034
John McCallc7ad3812011-01-25 03:31:58 +00003035static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
3036 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
3037}
3038static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
3039 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
3040}
3041
Chandler Carruth1b03c872011-07-02 00:01:44 +00003042static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003043 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003044 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003045 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3046 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003047 return;
3048 }
3049
3050 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003051 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003052 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3053 cf = false;
3054 } else {
3055 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3056 cf = true;
3057 }
3058
3059 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003060 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3061 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003062 return;
3063 }
3064
3065 if (cf)
Chandler Carruth87c44602011-07-01 23:49:12 +00003066 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003067 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003068 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003069}
3070
Chandler Carruth1b03c872011-07-02 00:01:44 +00003071static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3072 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003073 if (!isa<ObjCMethodDecl>(D)) {
3074 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3075 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003076 return;
3077 }
3078
Chandler Carruth87c44602011-07-01 23:49:12 +00003079 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003080}
3081
Chandler Carruth1b03c872011-07-02 00:01:44 +00003082static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3083 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003084
John McCallc7ad3812011-01-25 03:31:58 +00003085 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003086
Chandler Carruth87c44602011-07-01 23:49:12 +00003087 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003088 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003089 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003090 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003091 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
3092 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003093 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003094 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003095 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003096 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003097 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3098 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003099 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003100 return;
3101 }
Mike Stumpbf916502009-07-24 19:02:52 +00003102
John McCallc7ad3812011-01-25 03:31:58 +00003103 bool typeOK;
3104 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003105 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00003106 default: llvm_unreachable("invalid ownership attribute"); return;
3107 case AttributeList::AT_ns_returns_autoreleased:
3108 case AttributeList::AT_ns_returns_retained:
3109 case AttributeList::AT_ns_returns_not_retained:
3110 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3111 cf = false;
3112 break;
3113
3114 case AttributeList::AT_cf_returns_retained:
3115 case AttributeList::AT_cf_returns_not_retained:
3116 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3117 cf = true;
3118 break;
3119 }
3120
3121 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003122 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3123 << SourceRange(Attr.getLoc())
3124 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003125 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003126 }
Mike Stumpbf916502009-07-24 19:02:52 +00003127
Chandler Carruth87c44602011-07-01 23:49:12 +00003128 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003129 default:
3130 assert(0 && "invalid ownership attribute");
3131 return;
John McCallc7ad3812011-01-25 03:31:58 +00003132 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruth87c44602011-07-01 23:49:12 +00003133 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCallc7ad3812011-01-25 03:31:58 +00003134 S.Context));
3135 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003136 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003137 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003138 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003139 return;
3140 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003141 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003142 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003143 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003144 case AttributeList::AT_cf_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003145 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003146 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003147 return;
3148 case AttributeList::AT_ns_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003149 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003150 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003151 return;
3152 };
3153}
3154
John McCalldc7c5ad2011-07-22 08:53:00 +00003155static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3156 const AttributeList &attr) {
3157 SourceLocation loc = attr.getLoc();
3158
3159 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3160
3161 if (!isa<ObjCMethodDecl>(method)) {
3162 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3163 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
3164 return;
3165 }
3166
3167 // Check that the method returns a normal pointer.
3168 QualType resultType = method->getResultType();
3169 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
3170 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3171 << SourceRange(loc)
3172 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3173
3174 // Drop the attribute.
3175 return;
3176 }
3177
3178 method->addAttr(
3179 ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context));
3180}
3181
Chandler Carruth1b03c872011-07-02 00:01:44 +00003182static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3183 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003184 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003185
Chandler Carruth87c44602011-07-01 23:49:12 +00003186 SourceLocation L = Attr.getLoc();
3187 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3188 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003189}
3190
Chandler Carruth1b03c872011-07-02 00:01:44 +00003191static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3192 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003193 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
3194 SourceLocation L = Attr.getLoc();
3195 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3196 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003197 return;
3198 }
3199
Chandler Carruth87c44602011-07-01 23:49:12 +00003200 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003201 QualType type = vd->getType();
3202
3203 if (!type->isDependentType() &&
3204 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003205 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003206 << type;
3207 return;
3208 }
3209
3210 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3211
3212 // If we have no lifetime yet, check the lifetime we're presumably
3213 // going to infer.
3214 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3215 lifetime = type->getObjCARCImplicitLifetime();
3216
3217 switch (lifetime) {
3218 case Qualifiers::OCL_None:
3219 assert(type->isDependentType() &&
3220 "didn't infer lifetime for non-dependent type?");
3221 break;
3222
3223 case Qualifiers::OCL_Weak: // meaningful
3224 case Qualifiers::OCL_Strong: // meaningful
3225 break;
3226
3227 case Qualifiers::OCL_ExplicitNone:
3228 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003229 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003230 << (lifetime == Qualifiers::OCL_Autoreleasing);
3231 break;
3232 }
3233
Chandler Carruth87c44602011-07-01 23:49:12 +00003234 D->addAttr(::new (S.Context)
3235 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003236}
3237
Charles Davisf0122fe2010-02-16 18:27:26 +00003238static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3239 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00003240 Attr.getKind() == AttributeList::AT_dllexport ||
3241 Attr.getKind() == AttributeList::AT_uuid;
3242}
3243
3244//===----------------------------------------------------------------------===//
3245// Microsoft specific attribute handlers.
3246//===----------------------------------------------------------------------===//
3247
Chandler Carruth1b03c872011-07-02 00:01:44 +00003248static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet11542142010-12-19 06:50:37 +00003249 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
3250 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003251 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003252 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003253
Francois Pichet11542142010-12-19 06:50:37 +00003254 Expr *Arg = Attr.getArg(0);
3255 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003256 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003257 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3258 << "uuid" << 1;
3259 return;
3260 }
3261
Chris Lattner5f9e2722011-07-23 10:55:15 +00003262 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003263
3264 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3265 StrRef.back() == '}';
3266
3267 // Validate GUID length.
3268 if (IsCurly && StrRef.size() != 38) {
3269 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3270 return;
3271 }
3272 if (!IsCurly && StrRef.size() != 36) {
3273 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3274 return;
3275 }
3276
3277 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3278 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003279 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003280 if (IsCurly) // Skip the optional '{'
3281 ++I;
3282
3283 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003284 if (i == 8 || i == 13 || i == 18 || i == 23) {
3285 if (*I != '-') {
3286 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3287 return;
3288 }
3289 } else if (!isxdigit(*I)) {
3290 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3291 return;
3292 }
3293 I++;
3294 }
Francois Pichet11542142010-12-19 06:50:37 +00003295
Chandler Carruth87c44602011-07-01 23:49:12 +00003296 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003297 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003298 } else
Francois Pichet11542142010-12-19 06:50:37 +00003299 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003300}
3301
Ted Kremenekb71368d2009-05-09 02:44:38 +00003302//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003303// Top Level Sema Entry Points
3304//===----------------------------------------------------------------------===//
3305
Chandler Carruth1b03c872011-07-02 00:01:44 +00003306static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3307 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003308 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003309 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3310 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3311 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003312 default:
3313 break;
3314 }
3315}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003316
Chandler Carruth1b03c872011-07-02 00:01:44 +00003317static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3318 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003319 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003320 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3321 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00003322 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003323 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003324 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003325 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003326 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003327 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003328 case AttributeList::AT_neon_vector_type:
3329 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003330 // Ignore these, these are type attributes, handled by
3331 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003332 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003333 case AttributeList::AT_device:
3334 case AttributeList::AT_host:
3335 case AttributeList::AT_overloadable:
3336 // Ignore, this is a non-inheritable attribute, handled
3337 // by ProcessNonInheritableDeclAttr.
3338 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003339 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3340 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003341 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003342 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003343 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003344 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3345 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3346 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003347 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003348 handleDependencyAttr (S, D, Attr); break;
3349 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3350 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3351 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3352 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3353 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003354 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003355 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003356 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003357 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3358 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3359 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3360 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003361 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003362 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003363 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003364 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3365 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3366 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3367 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3368 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003369 case AttributeList::AT_ownership_returns:
3370 case AttributeList::AT_ownership_takes:
3371 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003372 handleOwnershipAttr (S, D, Attr); break;
3373 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3374 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3375 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3376 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3377 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003378
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003379 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003380 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003381 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003382 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003383
John McCalldc7c5ad2011-07-22 08:53:00 +00003384 case AttributeList::AT_objc_returns_inner_pointer:
3385 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3386
Ted Kremenekb71368d2009-05-09 02:44:38 +00003387 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003388 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003389 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003390 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003391 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003392
3393 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003394 case AttributeList::AT_ns_returns_not_retained:
3395 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003396 case AttributeList::AT_ns_returns_retained:
3397 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003398 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003399
Nate Begeman6f3d8382009-06-26 06:32:41 +00003400 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003401 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003402
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003403 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003404 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003405
Chandler Carruth1b03c872011-07-02 00:01:44 +00003406 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3407 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3408 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3409 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003410 case AttributeList::AT_arc_weakref_unavailable:
3411 handleArcWeakrefUnavailableAttr (S, D, Attr);
3412 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003413 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3414 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3415 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3416 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003417 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003418 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3419 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3420 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003421 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003422 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003423 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003424 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003425 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003426 break;
John McCalld5313b02011-03-02 11:33:24 +00003427 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003428 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003429 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003430 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3431 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3432 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3433 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3434 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3435 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3436 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3437 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3438 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003439 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003440 // Just ignore
3441 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003442 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003443 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003444 break;
John McCall04a67a62010-02-05 21:31:56 +00003445 case AttributeList::AT_stdcall:
3446 case AttributeList::AT_cdecl:
3447 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003448 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003449 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003450 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003451 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003452 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003453 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003454 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003455 break;
Francois Pichet11542142010-12-19 06:50:37 +00003456 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003457 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003458 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003459
3460 // Thread safety attributes:
3461 case AttributeList::AT_guarded_var:
3462 handleGuardedVarAttr(S, D, Attr);
3463 break;
3464 case AttributeList::AT_pt_guarded_var:
3465 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3466 break;
3467 case AttributeList::AT_scoped_lockable:
3468 handleLockableAttr(S, D, Attr, /*scoped = */true);
3469 break;
3470 case AttributeList::AT_no_thread_safety_analysis:
3471 handleNoThreadSafetyAttr(S, D, Attr);
3472 break;
3473 case AttributeList::AT_lockable:
3474 handleLockableAttr(S, D, Attr);
3475 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003476 case AttributeList::AT_guarded_by:
3477 handleGuardedByAttr(S, D, Attr);
3478 break;
3479 case AttributeList::AT_pt_guarded_by:
3480 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3481 break;
3482 case AttributeList::AT_exclusive_lock_function:
3483 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3484 break;
3485 case AttributeList::AT_exclusive_locks_required:
3486 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3487 break;
3488 case AttributeList::AT_exclusive_trylock_function:
3489 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3490 break;
3491 case AttributeList::AT_lock_returned:
3492 handleLockReturnedAttr(S, D, Attr);
3493 break;
3494 case AttributeList::AT_locks_excluded:
3495 handleLocksExcludedAttr(S, D, Attr);
3496 break;
3497 case AttributeList::AT_shared_lock_function:
3498 handleLockFunAttr(S, D, Attr);
3499 break;
3500 case AttributeList::AT_shared_locks_required:
3501 handleLocksRequiredAttr(S, D, Attr);
3502 break;
3503 case AttributeList::AT_shared_trylock_function:
3504 handleTrylockFunAttr(S, D, Attr);
3505 break;
3506 case AttributeList::AT_unlock_function:
3507 handleUnlockFunAttr(S, D, Attr);
3508 break;
3509 case AttributeList::AT_acquired_before:
3510 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3511 break;
3512 case AttributeList::AT_acquired_after:
3513 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3514 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003515
Chris Lattner803d0802008-06-29 00:43:07 +00003516 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003517 // Ask target about the attribute.
3518 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3519 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003520 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3521 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003522 break;
3523 }
3524}
3525
Peter Collingbourne60700392011-01-21 02:08:45 +00003526/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3527/// the attribute applies to decls. If the attribute is a type attribute, just
3528/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3529/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003530static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3531 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003532 bool NonInheritable, bool Inheritable) {
3533 if (Attr.isInvalid())
3534 return;
3535
3536 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3537 // FIXME: Try to deal with other __declspec attributes!
3538 return;
3539
3540 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003541 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003542
3543 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003544 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003545}
3546
Chris Lattner803d0802008-06-29 00:43:07 +00003547/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3548/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003549void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003550 const AttributeList *AttrList,
3551 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003552 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003553 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003554 }
3555
3556 // GCC accepts
3557 // static int a9 __attribute__((weakref));
3558 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003559 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003560 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003561 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003562 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003563 }
3564}
3565
Ryan Flynne25ff832009-07-30 03:15:39 +00003566/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3567/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003568NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003569 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003570 NamedDecl *NewD = 0;
3571 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3572 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003573 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003574 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003575 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003576 if (FD->getQualifier()) {
3577 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003578 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003579 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003580 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3581 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003582 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003583 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003584 VD->getStorageClass(),
3585 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003586 if (VD->getQualifier()) {
3587 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003588 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003589 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003590 }
3591 return NewD;
3592}
3593
3594/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3595/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003596void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003597 if (W.getUsed()) return; // only do this once
3598 W.setUsed(true);
3599 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3600 IdentifierInfo *NDId = ND->getIdentifier();
3601 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003602 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3603 NDId->getName()));
3604 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003605 WeakTopLevelDecl.push_back(NewD);
3606 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3607 // to insert Decl at TU scope, sorry.
3608 DeclContext *SavedContext = CurContext;
3609 CurContext = Context.getTranslationUnitDecl();
3610 PushOnScopeChains(NewD, S);
3611 CurContext = SavedContext;
3612 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003613 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003614 }
3615}
3616
Chris Lattner0744e5f2008-06-29 00:23:49 +00003617/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3618/// it, apply them to D. This is a bit tricky because PD can have attributes
3619/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003620void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3621 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003622 // It's valid to "forward-declare" #pragma weak, in which case we
3623 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003624 if (Inheritable) {
3625 LoadExternalWeakUndeclaredIdentifiers();
3626 if (!WeakUndeclaredIdentifiers.empty()) {
3627 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3628 if (IdentifierInfo *Id = ND->getIdentifier()) {
3629 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3630 = WeakUndeclaredIdentifiers.find(Id);
3631 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3632 WeakInfo W = I->second;
3633 DeclApplyPragmaWeak(S, ND, W);
3634 WeakUndeclaredIdentifiers[Id] = W;
3635 }
John McCalld4aff0e2010-10-27 00:59:00 +00003636 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003637 }
3638 }
3639 }
3640
Chris Lattner0744e5f2008-06-29 00:23:49 +00003641 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003642 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003643 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003644
Chris Lattner0744e5f2008-06-29 00:23:49 +00003645 // Walk the declarator structure, applying decl attributes that were in a type
3646 // position to the decl itself. This handles cases like:
3647 // int *__attr__(x)** D;
3648 // when X is a decl attribute.
3649 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3650 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003651 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003652
Chris Lattner0744e5f2008-06-29 00:23:49 +00003653 // Finally, apply any attributes on the decl itself.
3654 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003655 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003656}
John McCall54abf7d2009-11-04 02:18:39 +00003657
John McCallf85e1932011-06-15 23:02:42 +00003658/// Is the given declaration allowed to use a forbidden type?
3659static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3660 // Private ivars are always okay. Unfortunately, people don't
3661 // always properly make their ivars private, even in system headers.
3662 // Plus we need to make fields okay, too.
3663 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3664 return false;
3665
3666 // Require it to be declared in a system header.
3667 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3668}
3669
3670/// Handle a delayed forbidden-type diagnostic.
3671static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3672 Decl *decl) {
3673 if (decl && isForbiddenTypeAllowed(S, decl)) {
3674 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3675 "this system declaration uses an unsupported type"));
3676 return;
3677 }
3678
3679 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3680 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3681 diag.Triggered = true;
3682}
3683
John McCalleee1d542011-02-14 07:13:47 +00003684// This duplicates a vector push_back but hides the need to know the
3685// size of the type.
3686void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3687 assert(StackSize <= StackCapacity);
3688
3689 // Grow the stack if necessary.
3690 if (StackSize == StackCapacity) {
3691 unsigned newCapacity = 2 * StackCapacity + 2;
3692 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3693 const char *oldBuffer = (const char*) Stack;
3694
3695 if (StackCapacity)
3696 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3697
3698 delete[] oldBuffer;
3699 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3700 StackCapacity = newCapacity;
3701 }
3702
3703 assert(StackSize < StackCapacity);
3704 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003705}
3706
John McCalleee1d542011-02-14 07:13:47 +00003707void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3708 Decl *decl) {
3709 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003710
John McCalleee1d542011-02-14 07:13:47 +00003711 // Check the invariants.
3712 assert(DD.StackSize >= state.SavedStackSize);
3713 assert(state.SavedStackSize >= DD.ActiveStackBase);
3714 assert(DD.ParsingDepth > 0);
3715
3716 // Drop the parsing depth.
3717 DD.ParsingDepth--;
3718
3719 // If there are no active diagnostics, we're done.
3720 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003721 return;
3722
John McCall2f514482010-01-27 03:50:35 +00003723 // We only want to actually emit delayed diagnostics when we
3724 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003725 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003726 // We emit all the active diagnostics, not just those starting
3727 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003728 // decl spec and another for each declarator; in a decl group like:
3729 // deprecated_typedef foo, *bar, baz();
3730 // only the declarator pops will be passed decls. This is correct;
3731 // we really do need to consider delayed diagnostics from the decl spec
3732 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003733 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3734 DelayedDiagnostic &diag = DD.Stack[i];
3735 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003736 continue;
3737
John McCalleee1d542011-02-14 07:13:47 +00003738 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003739 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003740 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003741 break;
3742
3743 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003744 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003745 break;
John McCallf85e1932011-06-15 23:02:42 +00003746
3747 case DelayedDiagnostic::ForbiddenType:
3748 handleDelayedForbiddenType(S, diag, decl);
3749 break;
John McCall2f514482010-01-27 03:50:35 +00003750 }
3751 }
3752 }
3753
John McCall58e6f342010-03-16 05:22:47 +00003754 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003755 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003756 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003757
John McCalleee1d542011-02-14 07:13:47 +00003758 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003759}
3760
3761static bool isDeclDeprecated(Decl *D) {
3762 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003763 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003764 return true;
3765 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3766 return false;
3767}
3768
John McCall9c3087b2010-08-26 02:13:20 +00003769void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003770 Decl *Ctx) {
3771 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003772 return;
3773
John McCall2f514482010-01-27 03:50:35 +00003774 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003775 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003776 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003777 << DD.getDeprecationDecl()->getDeclName()
3778 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003779 else
3780 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003781 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003782}
3783
Chris Lattner5f9e2722011-07-23 10:55:15 +00003784void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003785 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003786 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003787 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003788 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3789 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003790 return;
3791 }
3792
3793 // Otherwise, don't warn if our current context is deprecated.
3794 if (isDeclDeprecated(cast<Decl>(CurContext)))
3795 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003796 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003797 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3798 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003799 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003800 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003801 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003802 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003803 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003804 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3805 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003806 }
John McCall54abf7d2009-11-04 02:18:39 +00003807}