blob: 9e20bc90184efe1338b4823c94c19a83aaf8e357 [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"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000021#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000023#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000025using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000026using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000027
John McCall883cc2c2011-03-02 12:29:23 +000028/// These constants match the enumerated choices of
29/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
30enum {
31 ExpectedFunction,
32 ExpectedUnion,
33 ExpectedVariableOrFunction,
34 ExpectedFunctionOrMethod,
35 ExpectedParameter,
36 ExpectedParameterOrMethod,
37 ExpectedFunctionMethodOrBlock,
38 ExpectedClassOrVirtualMethod,
39 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
41 ExpectedVirtualMethod,
42 ExpectedClassMember,
43 ExpectedVariable,
44 ExpectedMethod,
45 ExpectedVariableFunctionOrLabel
46};
47
Chris Lattnere5c5ee12008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruth87c44602011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000063
Chris Lattner6b6b5372008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000068
John McCall183700f2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000070}
71
Daniel Dunbar35682492008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopesd20254f2009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000086}
87
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruth87c44602011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100}
101
John McCall711c52b2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000108}
109
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000131}
132
Chandler Carruth87c44602011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000138
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000140}
141
Chandler Carruth87c44602011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146}
147
Chandler Carruth87c44602011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000154 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000169
John McCall506b57e2010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000173
John McCall506b57e2010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000175
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000189
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Chandler Carruth1731e202011-07-11 23:30:35 +0000197static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
198 unsigned int Num) {
199 if (Attr.getNumArgs() != Num) {
200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
201 return false;
202 }
203
204 return true;
205}
206
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000207//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000208// Attribute Implementations
209//===----------------------------------------------------------------------===//
210
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000211// FIXME: All this manual attribute parsing code is gross. At the
212// least add some helper functions to check most argument patterns (#
213// and types of args).
214
Chandler Carruth1b03c872011-07-02 00:01:44 +0000215static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
216 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000217 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000218 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000220 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000221 }
Mike Stumpbf916502009-07-24 19:02:52 +0000222
Chris Lattner6b6b5372008-06-26 18:38:35 +0000223 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000224
225 Expr *sizeExpr;
226
227 // Special case where the argument is a template id.
228 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000229 CXXScopeSpec SS;
230 UnqualifiedId id;
231 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000232
233 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
234 if (Size.isInvalid())
235 return;
236
237 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000238 } else {
239 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000240 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000241 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000242
Peter Collingbourne7a730022010-11-23 20:45:58 +0000243 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000244 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000245
246 // Instantiate/Install the vector type, and let Sema build the type for us.
247 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000248 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000249 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000250 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000251 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000252
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000253 // Remember this typedef decl, we will need it later for diagnostics.
254 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000255 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000256}
257
Chandler Carruth1b03c872011-07-02 00:01:44 +0000258static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000259 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000260 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000261 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000262
Chandler Carruth87c44602011-07-01 23:49:12 +0000263 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Sean Huntcf807c42010-08-18 23:23:40 +0000264 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000265 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000266 // If the alignment is less than or equal to 8 bits, the packed attribute
267 // has no effect.
268 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000269 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000270 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000271 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000272 else
Sean Huntcf807c42010-08-18 23:23:40 +0000273 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000274 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000275 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000276}
277
Chandler Carruth1b03c872011-07-02 00:01:44 +0000278static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000279 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000280 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
281 else
282 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
283}
284
Chandler Carruth1b03c872011-07-02 00:01:44 +0000285static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000286 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000287 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000288 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000289
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000290 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000291 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000292 if (MD->isInstanceMethod()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000293 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000294 return;
295 }
296
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000297 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000298}
299
Chandler Carruth1b03c872011-07-02 00:01:44 +0000300static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000301 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000302 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000303 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000304
305 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000306 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000307 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
308 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000309 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000310 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000311
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000312 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000313}
314
Chandler Carruth1b03c872011-07-02 00:01:44 +0000315static void handleIBOutletCollection(Sema &S, Decl *D,
316 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000317
318 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000319 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000320 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
321 return;
322 }
323
324 // The IBOutletCollection attributes only apply to instance variables of
325 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000326 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000327 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000328 return;
329 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000330 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000331 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
332 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
333 << VD->getType() << 0;
334 return;
335 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000336 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000337 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
338 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
339 << PD->getType() << 1;
340 return;
341 }
342
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000343 IdentifierInfo *II = Attr.getParameterName();
344 if (!II)
345 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000346
John McCallb3d87482010-08-24 05:47:05 +0000347 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000348 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000349 if (!TypeRep) {
350 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
351 return;
352 }
John McCallb3d87482010-08-24 05:47:05 +0000353 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000354 // Diagnose use of non-object type in iboutletcollection attribute.
355 // FIXME. Gnu attribute extension ignores use of builtin types in
356 // attributes. So, __attribute__((iboutletcollection(char))) will be
357 // treated as __attribute__((iboutletcollection())).
358 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
359 !QT->isObjCObjectType()) {
360 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
361 return;
362 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000363 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000364 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000365}
366
Chandler Carruthd309c812011-07-01 23:49:16 +0000367static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000368 if (const RecordType *UT = T->getAsUnionType())
369 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
370 RecordDecl *UD = UT->getDecl();
371 for (RecordDecl::field_iterator it = UD->field_begin(),
372 itend = UD->field_end(); it != itend; ++it) {
373 QualType QT = it->getType();
374 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
375 T = QT;
376 return;
377 }
378 }
379 }
380}
381
Chandler Carruth1b03c872011-07-02 00:01:44 +0000382static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000383 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
384 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000385 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000386 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000387 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000388 return;
389 }
Mike Stumpbf916502009-07-24 19:02:52 +0000390
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000391 // In C++ the implicit 'this' function parameter also counts, and they are
392 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000393 bool HasImplicitThisParam = isInstanceMethod(D);
394 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000395
396 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000397 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000398
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000399 for (AttributeList::arg_iterator I=Attr.arg_begin(),
400 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000401
402
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000403 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000404 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000405 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000406 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
407 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000408 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
409 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000410 return;
411 }
Mike Stumpbf916502009-07-24 19:02:52 +0000412
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000413 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000414
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000415 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000416 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000417 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000418 return;
419 }
Mike Stumpbf916502009-07-24 19:02:52 +0000420
Ted Kremenek465172f2008-07-21 22:09:15 +0000421 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000422 if (HasImplicitThisParam) {
423 if (x == 0) {
424 S.Diag(Attr.getLoc(),
425 diag::err_attribute_invalid_implicit_this_argument)
426 << "nonnull" << Ex->getSourceRange();
427 return;
428 }
429 --x;
430 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000431
432 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000433 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000434 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000435
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000436 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000437 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000438 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000439 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000440 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000441 }
Mike Stumpbf916502009-07-24 19:02:52 +0000442
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000443 NonNullArgs.push_back(x);
444 }
Mike Stumpbf916502009-07-24 19:02:52 +0000445
446 // If no arguments were specified to __attribute__((nonnull)) then all pointer
447 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000448 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000449 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
450 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000451 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000452 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000453 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000454 }
Mike Stumpbf916502009-07-24 19:02:52 +0000455
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000456 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000457 if (NonNullArgs.empty()) {
458 // Warn the trivial case only if attribute is not coming from a
459 // macro instantiation.
460 if (Attr.getLoc().isFileID())
461 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000462 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000463 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000464 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000465
466 unsigned* start = &NonNullArgs[0];
467 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000468 llvm::array_pod_sort(start, start + size);
Chandler Carruth87c44602011-07-01 23:49:12 +0000469 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000470 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000471}
472
Chandler Carruth1b03c872011-07-02 00:01:44 +0000473static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000474 // This attribute must be applied to a function declaration.
475 // The first argument to the attribute must be a string,
476 // the name of the resource, for example "malloc".
477 // The following arguments must be argument indexes, the arguments must be
478 // of integer type for Returns, otherwise of pointer type.
479 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000480 // after being held. free() should be __attribute((ownership_takes)), whereas
481 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000482
483 if (!AL.getParameterName()) {
484 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
485 << AL.getName()->getName() << 1;
486 return;
487 }
488 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000489 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000490 switch (AL.getKind()) {
491 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000492 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000493 if (AL.getNumArgs() < 1) {
494 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
495 return;
496 }
Jordy Rose2a479922010-08-12 08:54:03 +0000497 break;
498 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000499 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000500 if (AL.getNumArgs() < 1) {
501 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
502 return;
503 }
Jordy Rose2a479922010-08-12 08:54:03 +0000504 break;
505 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000506 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000507 if (AL.getNumArgs() > 1) {
508 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
509 << AL.getNumArgs() + 1;
510 return;
511 }
Jordy Rose2a479922010-08-12 08:54:03 +0000512 break;
513 default:
514 // This should never happen given how we are called.
515 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000516 }
517
Chandler Carruth87c44602011-07-01 23:49:12 +0000518 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000519 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
520 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000521 return;
522 }
523
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000524 // In C++ the implicit 'this' function parameter also counts, and they are
525 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000526 bool HasImplicitThisParam = isInstanceMethod(D);
527 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000528
Chris Lattner5f9e2722011-07-23 10:55:15 +0000529 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000530
531 // Normalize the argument, __foo__ becomes foo.
532 if (Module.startswith("__") && Module.endswith("__"))
533 Module = Module.substr(2, Module.size() - 4);
534
Chris Lattner5f9e2722011-07-23 10:55:15 +0000535 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000536
Jordy Rose2a479922010-08-12 08:54:03 +0000537 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
538 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000539
Peter Collingbourne7a730022010-11-23 20:45:58 +0000540 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000541 llvm::APSInt ArgNum(32);
542 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
543 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
544 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
545 << AL.getName()->getName() << IdxExpr->getSourceRange();
546 continue;
547 }
548
549 unsigned x = (unsigned) ArgNum.getZExtValue();
550
551 if (x > NumArgs || x < 1) {
552 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
553 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
554 continue;
555 }
556 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000557 if (HasImplicitThisParam) {
558 if (x == 0) {
559 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
560 << "ownership" << IdxExpr->getSourceRange();
561 return;
562 }
563 --x;
564 }
565
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000566 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000567 case OwnershipAttr::Takes:
568 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000569 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000570 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000571 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
572 // FIXME: Should also highlight argument in decl.
573 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000574 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000575 << "pointer"
576 << IdxExpr->getSourceRange();
577 continue;
578 }
579 break;
580 }
Sean Huntcf807c42010-08-18 23:23:40 +0000581 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000582 if (AL.getNumArgs() > 1) {
583 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000584 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000585 llvm::APSInt ArgNum(32);
586 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
587 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
588 S.Diag(AL.getLoc(), diag::err_ownership_type)
589 << "ownership_returns" << "integer"
590 << IdxExpr->getSourceRange();
591 return;
592 }
593 }
594 break;
595 }
Jordy Rose2a479922010-08-12 08:54:03 +0000596 default:
597 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000598 } // switch
599
600 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000601 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +0000602 i = D->specific_attr_begin<OwnershipAttr>(),
603 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +0000604 i != e; ++i) {
605 if ((*i)->getOwnKind() != K) {
606 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
607 I!=E; ++I) {
608 if (x == *I) {
609 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
610 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000611 }
612 }
613 }
614 }
615 OwnershipArgs.push_back(x);
616 }
617
618 unsigned* start = OwnershipArgs.data();
619 unsigned size = OwnershipArgs.size();
620 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000621
622 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
623 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
624 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000625 }
Sean Huntcf807c42010-08-18 23:23:40 +0000626
Chandler Carruth87c44602011-07-01 23:49:12 +0000627 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +0000628 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000629}
630
John McCall332bb2a2011-02-08 22:35:49 +0000631/// Whether this declaration has internal linkage for the purposes of
632/// things that want to complain about things not have internal linkage.
633static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
634 switch (D->getLinkage()) {
635 case NoLinkage:
636 case InternalLinkage:
637 return true;
638
639 // Template instantiations that go from external to unique-external
640 // shouldn't get diagnosed.
641 case UniqueExternalLinkage:
642 return true;
643
644 case ExternalLinkage:
645 return false;
646 }
647 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000648 return false;
649}
650
Chandler Carruth1b03c872011-07-02 00:01:44 +0000651static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000652 // Check the attribute arguments.
653 if (Attr.getNumArgs() > 1) {
654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
655 return;
656 }
657
Chandler Carruth87c44602011-07-01 23:49:12 +0000658 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000660 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +0000661 return;
662 }
663
Chandler Carruth87c44602011-07-01 23:49:12 +0000664 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +0000665
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000666 // gcc rejects
667 // class c {
668 // static int a __attribute__((weakref ("v2")));
669 // static int b() __attribute__((weakref ("f3")));
670 // };
671 // and ignores the attributes of
672 // void f(void) {
673 // static int a __attribute__((weakref ("v2")));
674 // }
675 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +0000676 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000677 if (!Ctx->isFileContext()) {
678 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +0000679 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000680 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000681 }
682
683 // The GCC manual says
684 //
685 // At present, a declaration to which `weakref' is attached can only
686 // be `static'.
687 //
688 // It also says
689 //
690 // Without a TARGET,
691 // given as an argument to `weakref' or to `alias', `weakref' is
692 // equivalent to `weak'.
693 //
694 // gcc 4.4.1 will accept
695 // int a7 __attribute__((weakref));
696 // as
697 // int a7 __attribute__((weak));
698 // This looks like a bug in gcc. We reject that for now. We should revisit
699 // it if this behaviour is actually used.
700
John McCall332bb2a2011-02-08 22:35:49 +0000701 if (!hasEffectivelyInternalLinkage(nd)) {
702 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000703 return;
704 }
705
706 // GCC rejects
707 // static ((alias ("y"), weakref)).
708 // Should we? How to check that weakref is before or after alias?
709
710 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000711 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000712 Arg = Arg->IgnoreParenCasts();
713 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
714
715 if (Str == 0 || Str->isWide()) {
716 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
717 << "weakref" << 1;
718 return;
719 }
720 // GCC will accept anything as the argument of weakref. Should we
721 // check for an existing decl?
Chandler Carruth87c44602011-07-01 23:49:12 +0000722 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000723 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000724 }
725
Chandler Carruth87c44602011-07-01 23:49:12 +0000726 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000727}
728
Chandler Carruth1b03c872011-07-02 00:01:44 +0000729static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000730 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000731 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000732 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000733 return;
734 }
Mike Stumpbf916502009-07-24 19:02:52 +0000735
Peter Collingbourne7a730022010-11-23 20:45:58 +0000736 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000737 Arg = Arg->IgnoreParenCasts();
738 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000739
Chris Lattner6b6b5372008-06-26 18:38:35 +0000740 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000741 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000742 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000743 return;
744 }
Mike Stumpbf916502009-07-24 19:02:52 +0000745
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000746 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +0000747 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
748 return;
749 }
750
Chris Lattner6b6b5372008-06-26 18:38:35 +0000751 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000752
Chandler Carruth87c44602011-07-01 23:49:12 +0000753 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000754 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000755}
756
Chandler Carruth1b03c872011-07-02 00:01:44 +0000757static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000758 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000759 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000760 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000761
Chandler Carruth87c44602011-07-01 23:49:12 +0000762 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000763 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000764 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000765 return;
766 }
767
Chandler Carruth87c44602011-07-01 23:49:12 +0000768 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000769}
770
Chandler Carruth1b03c872011-07-02 00:01:44 +0000771static void handleAlwaysInlineAttr(Sema &S, Decl *D,
772 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000773 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000774 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
776 return;
777 }
778
Chandler Carruth87c44602011-07-01 23:49:12 +0000779 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000780 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000781 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000782 return;
783 }
Mike Stumpbf916502009-07-24 19:02:52 +0000784
Chandler Carruth87c44602011-07-01 23:49:12 +0000785 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000786}
787
Chandler Carruth1b03c872011-07-02 00:01:44 +0000788static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000789 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000790 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +0000791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
792 return;
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chandler Carruth87c44602011-07-01 23:49:12 +0000795 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000796 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000797 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000798 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000799 return;
800 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000801 }
802
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000803 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000804}
805
Chandler Carruth1b03c872011-07-02 00:01:44 +0000806static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +0000807 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000808 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +0000809 return;
Dan Gohman34c26302010-11-17 00:03:07 +0000810
Chandler Carruth87c44602011-07-01 23:49:12 +0000811 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +0000812}
813
Chandler Carruth1b03c872011-07-02 00:01:44 +0000814static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +0000815 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +0000816 if (isa<VarDecl>(D))
817 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +0000818 else
819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000820 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000821}
822
Chandler Carruth1b03c872011-07-02 00:01:44 +0000823static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +0000824 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +0000825 if (isa<VarDecl>(D))
826 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +0000827 else
828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000829 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000830}
831
Chandler Carruth1b03c872011-07-02 00:01:44 +0000832static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000833 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +0000834
835 if (S.CheckNoReturnAttr(attr)) return;
836
Chandler Carruth87c44602011-07-01 23:49:12 +0000837 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +0000838 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000839 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +0000840 return;
841 }
842
Chandler Carruth87c44602011-07-01 23:49:12 +0000843 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +0000844}
845
846bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +0000847 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +0000848 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
849 attr.setInvalid();
850 return true;
851 }
852
853 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +0000854}
855
Chandler Carruth1b03c872011-07-02 00:01:44 +0000856static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
857 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000858
859 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
860 // because 'analyzer_noreturn' does not impact the type.
861
Chandler Carruth1731e202011-07-11 23:30:35 +0000862 if(!checkAttributeNumArgs(S, Attr, 0))
863 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000864
Chandler Carruth87c44602011-07-01 23:49:12 +0000865 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
866 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000867 if (VD == 0 || (!VD->getType()->isBlockPointerType()
868 && !VD->getType()->isFunctionPointerType())) {
869 S.Diag(Attr.getLoc(),
870 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
871 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000872 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000873 return;
874 }
875 }
876
Chandler Carruth87c44602011-07-01 23:49:12 +0000877 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000878}
879
John Thompson35cc9622010-08-09 21:53:52 +0000880// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +0000881static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +0000882/*
883 Returning a Vector Class in Registers
884
Eric Christopherf48f3672010-12-01 22:13:54 +0000885 According to the PPU ABI specifications, a class with a single member of
886 vector type is returned in memory when used as the return value of a function.
887 This results in inefficient code when implementing vector classes. To return
888 the value in a single vector register, add the vecreturn attribute to the
889 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000890
891 Example:
892
893 struct Vector
894 {
895 __vector float xyzw;
896 } __attribute__((vecreturn));
897
898 Vector Add(Vector lhs, Vector rhs)
899 {
900 Vector result;
901 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
902 return result; // This will be returned in a register
903 }
904*/
Chandler Carruth87c44602011-07-01 23:49:12 +0000905 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +0000906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000907 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +0000908 return;
909 }
910
Chandler Carruth87c44602011-07-01 23:49:12 +0000911 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +0000912 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
913 return;
914 }
915
Chandler Carruth87c44602011-07-01 23:49:12 +0000916 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +0000917 int count = 0;
918
919 if (!isa<CXXRecordDecl>(record)) {
920 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
921 return;
922 }
923
924 if (!cast<CXXRecordDecl>(record)->isPOD()) {
925 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
926 return;
927 }
928
Eric Christopherf48f3672010-12-01 22:13:54 +0000929 for (RecordDecl::field_iterator iter = record->field_begin();
930 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +0000931 if ((count == 1) || !iter->getType()->isVectorType()) {
932 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
933 return;
934 }
935 count++;
936 }
937
Chandler Carruth87c44602011-07-01 23:49:12 +0000938 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000939}
940
Chandler Carruth1b03c872011-07-02 00:01:44 +0000941static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000942 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000943 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000944 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +0000945 return;
946 }
947 // FIXME: Actually store the attribute on the declaration
948}
949
Chandler Carruth1b03c872011-07-02 00:01:44 +0000950static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +0000951 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000952 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000953 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000954 return;
955 }
Mike Stumpbf916502009-07-24 19:02:52 +0000956
Chandler Carruth87c44602011-07-01 23:49:12 +0000957 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
958 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000959 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000960 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +0000961 return;
962 }
Mike Stumpbf916502009-07-24 19:02:52 +0000963
Chandler Carruth87c44602011-07-01 23:49:12 +0000964 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000965}
966
Chandler Carruth1b03c872011-07-02 00:01:44 +0000967static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000968 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000969 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000970 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
971 return;
972 }
Mike Stumpbf916502009-07-24 19:02:52 +0000973
Chandler Carruth87c44602011-07-01 23:49:12 +0000974 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000975 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
977 return;
978 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000979 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000981 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000982 return;
983 }
Mike Stumpbf916502009-07-24 19:02:52 +0000984
Chandler Carruth87c44602011-07-01 23:49:12 +0000985 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000986}
987
Chandler Carruth1b03c872011-07-02 00:01:44 +0000988static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000989 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +0000990 if (Attr.getNumArgs() > 1) {
991 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000992 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000993 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000994
995 int priority = 65535; // FIXME: Do not hardcode such constants.
996 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000997 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000998 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000999 if (E->isTypeDependent() || E->isValueDependent() ||
1000 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001002 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001003 return;
1004 }
1005 priority = Idx.getZExtValue();
1006 }
Mike Stumpbf916502009-07-24 19:02:52 +00001007
Chandler Carruth87c44602011-07-01 23:49:12 +00001008 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001010 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001011 return;
1012 }
1013
Chandler Carruth87c44602011-07-01 23:49:12 +00001014 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001015 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001016}
1017
Chandler Carruth1b03c872011-07-02 00:01:44 +00001018static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001019 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001020 if (Attr.getNumArgs() > 1) {
1021 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001022 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001023 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001024
1025 int priority = 65535; // FIXME: Do not hardcode such constants.
1026 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001027 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001028 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001029 if (E->isTypeDependent() || E->isValueDependent() ||
1030 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001031 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001032 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001033 return;
1034 }
1035 priority = Idx.getZExtValue();
1036 }
Mike Stumpbf916502009-07-24 19:02:52 +00001037
Chandler Carruth87c44602011-07-01 23:49:12 +00001038 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001040 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001041 return;
1042 }
1043
Chandler Carruth87c44602011-07-01 23:49:12 +00001044 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001045 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001046}
1047
Chandler Carruth1b03c872011-07-02 00:01:44 +00001048static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001049 unsigned NumArgs = Attr.getNumArgs();
1050 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001051 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001052 return;
1053 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001054
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001055 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001056 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001057 if (NumArgs == 1) {
1058 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001059 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001060 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1061 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001062 return;
1063 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001064 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001065 }
Mike Stumpbf916502009-07-24 19:02:52 +00001066
Chandler Carruth87c44602011-07-01 23:49:12 +00001067 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001068}
1069
Chandler Carruth1b03c872011-07-02 00:01:44 +00001070static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001071 unsigned NumArgs = Attr.getNumArgs();
1072 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001074 return;
1075 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001076
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001077 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001078 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001079 if (NumArgs == 1) {
1080 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001081 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001082 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001083 diag::err_attribute_not_string) << "unavailable";
1084 return;
1085 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001086 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001087 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001088 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001089}
1090
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001091static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1092 const AttributeList &Attr) {
1093 unsigned NumArgs = Attr.getNumArgs();
1094 if (NumArgs > 0) {
1095 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1096 return;
1097 }
1098
1099 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1100 Attr.getLoc(), S.Context));
1101}
1102
Chandler Carruth1b03c872011-07-02 00:01:44 +00001103static void handleAvailabilityAttr(Sema &S, Decl *D,
1104 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001105 IdentifierInfo *Platform = Attr.getParameterName();
1106 SourceLocation PlatformLoc = Attr.getParameterLoc();
1107
Chris Lattner5f9e2722011-07-23 10:55:15 +00001108 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001109 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1110 if (PlatformName.empty()) {
1111 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1112 << Platform;
1113
1114 PlatformName = Platform->getName();
1115 }
1116
1117 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1118 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1119 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001120 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001121
1122 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1123 // of these steps are needed).
1124 if (Introduced.isValid() && Deprecated.isValid() &&
1125 !(Introduced.Version < Deprecated.Version)) {
1126 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1127 << 1 << PlatformName << Deprecated.Version.getAsString()
1128 << 0 << Introduced.Version.getAsString();
1129 return;
1130 }
1131
1132 if (Introduced.isValid() && Obsoleted.isValid() &&
1133 !(Introduced.Version < Obsoleted.Version)) {
1134 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1135 << 2 << PlatformName << Obsoleted.Version.getAsString()
1136 << 0 << Introduced.Version.getAsString();
1137 return;
1138 }
1139
1140 if (Deprecated.isValid() && Obsoleted.isValid() &&
1141 !(Deprecated.Version < Obsoleted.Version)) {
1142 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1143 << 2 << PlatformName << Obsoleted.Version.getAsString()
1144 << 1 << Deprecated.Version.getAsString();
1145 return;
1146 }
1147
Chandler Carruth87c44602011-07-01 23:49:12 +00001148 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001149 Platform,
1150 Introduced.Version,
1151 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001152 Obsoleted.Version,
1153 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001154}
1155
Chandler Carruth1b03c872011-07-02 00:01:44 +00001156static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001157 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001158 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001159 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001160
Peter Collingbourne7a730022010-11-23 20:45:58 +00001161 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001162 Arg = Arg->IgnoreParenCasts();
1163 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001164
Chris Lattner6b6b5372008-06-26 18:38:35 +00001165 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001166 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001167 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001168 return;
1169 }
Mike Stumpbf916502009-07-24 19:02:52 +00001170
Chris Lattner5f9e2722011-07-23 10:55:15 +00001171 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001172 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001173
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001174 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001175 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001176 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001177 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001178 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001179 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001180 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001181 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001183 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001184 return;
1185 }
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Chandler Carruth87c44602011-07-01 23:49:12 +00001187 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188}
1189
Chandler Carruth1b03c872011-07-02 00:01:44 +00001190static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1191 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001192 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1193 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001194 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001195 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001196 return;
1197 }
1198
Chandler Carruth87c44602011-07-01 23:49:12 +00001199 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1200 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001202 << "objc_method_family" << 1;
1203 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001205 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001206 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001207 return;
1208 }
1209
Chris Lattner5f9e2722011-07-23 10:55:15 +00001210 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001211 ObjCMethodFamilyAttr::FamilyKind family;
1212 if (param == "none")
1213 family = ObjCMethodFamilyAttr::OMF_None;
1214 else if (param == "alloc")
1215 family = ObjCMethodFamilyAttr::OMF_alloc;
1216 else if (param == "copy")
1217 family = ObjCMethodFamilyAttr::OMF_copy;
1218 else if (param == "init")
1219 family = ObjCMethodFamilyAttr::OMF_init;
1220 else if (param == "mutableCopy")
1221 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1222 else if (param == "new")
1223 family = ObjCMethodFamilyAttr::OMF_new;
1224 else {
1225 // Just warn and ignore it. This is future-proof against new
1226 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001227 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001228 return;
1229 }
1230
John McCallf85e1932011-06-15 23:02:42 +00001231 if (family == ObjCMethodFamilyAttr::OMF_init &&
1232 !method->getResultType()->isObjCObjectPointerType()) {
1233 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1234 << method->getResultType();
1235 // Ignore the attribute.
1236 return;
1237 }
1238
Chandler Carruth87c44602011-07-01 23:49:12 +00001239 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCallf85e1932011-06-15 23:02:42 +00001240 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001241}
1242
Chandler Carruth1b03c872011-07-02 00:01:44 +00001243static void handleObjCExceptionAttr(Sema &S, Decl *D,
1244 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001245 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001246 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001247
Chris Lattner0db29ec2009-02-14 08:09:34 +00001248 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1249 if (OCI == 0) {
1250 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1251 return;
1252 }
Mike Stumpbf916502009-07-24 19:02:52 +00001253
Sean Huntcf807c42010-08-18 23:23:40 +00001254 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001255}
1256
Chandler Carruth1b03c872011-07-02 00:01:44 +00001257static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001258 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001259 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001260 return;
1261 }
Richard Smith162e1c12011-04-15 14:24:37 +00001262 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001263 QualType T = TD->getUnderlyingType();
1264 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001265 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001266 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1267 return;
1268 }
1269 }
Sean Huntcf807c42010-08-18 23:23:40 +00001270 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001271}
1272
Mike Stumpbf916502009-07-24 19:02:52 +00001273static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001274handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001275 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001276 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001277 return;
1278 }
1279
1280 if (!isa<FunctionDecl>(D)) {
1281 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1282 return;
1283 }
1284
Sean Huntcf807c42010-08-18 23:23:40 +00001285 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001286}
1287
Chandler Carruth1b03c872011-07-02 00:01:44 +00001288static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001289 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001290 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001291 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001292 return;
1293 }
Mike Stumpbf916502009-07-24 19:02:52 +00001294
Steve Naroff9eae5762008-09-18 16:44:58 +00001295 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001296 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001297 return;
1298 }
Mike Stumpbf916502009-07-24 19:02:52 +00001299
Sean Huntcf807c42010-08-18 23:23:40 +00001300 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001301 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001302 type = BlocksAttr::ByRef;
1303 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001304 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001305 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001306 return;
1307 }
Mike Stumpbf916502009-07-24 19:02:52 +00001308
Chandler Carruth87c44602011-07-01 23:49:12 +00001309 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001310}
1311
Chandler Carruth1b03c872011-07-02 00:01:44 +00001312static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001313 // check the attribute arguments.
1314 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001315 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001316 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001317 }
1318
Anders Carlsson77091822008-10-05 18:05:59 +00001319 int sentinel = 0;
1320 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001321 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001322 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001323 if (E->isTypeDependent() || E->isValueDependent() ||
1324 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001326 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001327 return;
1328 }
1329 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001330
Anders Carlsson77091822008-10-05 18:05:59 +00001331 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1333 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001334 return;
1335 }
1336 }
1337
1338 int nullPos = 0;
1339 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001340 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001341 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001342 if (E->isTypeDependent() || E->isValueDependent() ||
1343 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001345 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001346 return;
1347 }
1348 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001349
Anders Carlsson77091822008-10-05 18:05:59 +00001350 if (nullPos > 1 || nullPos < 0) {
1351 // FIXME: This error message could be improved, it would be nice
1352 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1354 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001355 return;
1356 }
1357 }
1358
Chandler Carruth87c44602011-07-01 23:49:12 +00001359 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall183700f2009-09-21 23:43:11 +00001360 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001361 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001362
Chris Lattner897cd902009-03-17 23:03:47 +00001363 if (isa<FunctionNoProtoType>(FT)) {
1364 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1365 return;
1366 }
Mike Stumpbf916502009-07-24 19:02:52 +00001367
Chris Lattner897cd902009-03-17 23:03:47 +00001368 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001369 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001370 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001371 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001372 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001373 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001374 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001375 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001376 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001377 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001378 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1379 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001380 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001381 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001382 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001383 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001384 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001385 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001386 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001387 int m = Ty->isFunctionPointerType() ? 0 : 1;
1388 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001389 return;
1390 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001391 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001392 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001393 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001394 return;
1395 }
Anders Carlsson77091822008-10-05 18:05:59 +00001396 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001397 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001398 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001399 return;
1400 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001401 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001402 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001403}
1404
Chandler Carruth1b03c872011-07-02 00:01:44 +00001405static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001406 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001407 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001408 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001409
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001410 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001411 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001412 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001413 return;
1414 }
Mike Stumpbf916502009-07-24 19:02:52 +00001415
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001416 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1417 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1418 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001419 return;
1420 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001421 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1422 if (MD->getResultType()->isVoidType()) {
1423 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1424 << Attr.getName() << 1;
1425 return;
1426 }
1427
Sean Huntcf807c42010-08-18 23:23:40 +00001428 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001429}
1430
Chandler Carruth1b03c872011-07-02 00:01:44 +00001431static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001432 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001433 if (Attr.hasParameterOrArguments()) {
1434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435 return;
1436 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001437
Chandler Carruth87c44602011-07-01 23:49:12 +00001438 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1440 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001441 return;
1442 }
1443
Chandler Carruth87c44602011-07-01 23:49:12 +00001444 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001445
1446 // 'weak' only applies to declarations with external linkage.
1447 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001448 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001449 return;
1450 }
Mike Stumpbf916502009-07-24 19:02:52 +00001451
Chandler Carruth87c44602011-07-01 23:49:12 +00001452 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453}
1454
Chandler Carruth1b03c872011-07-02 00:01:44 +00001455static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001456 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001457 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001458 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001459
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001460
1461 // weak_import only applies to variable & function declarations.
1462 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001463 if (!D->canBeWeakImported(isDef)) {
1464 if (isDef)
1465 S.Diag(Attr.getLoc(),
1466 diag::warn_attribute_weak_import_invalid_on_definition)
1467 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001468 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001469 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001470 isa<ObjCInterfaceDecl>(D))) {
1471 // Nothing to warn about here.
1472 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001473 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001474 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001475
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001476 return;
1477 }
1478
Sean Huntcf807c42010-08-18 23:23:40 +00001479 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001480}
1481
Chandler Carruth1b03c872011-07-02 00:01:44 +00001482static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1483 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001484 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001485 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001486 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001487
1488 unsigned WGSize[3];
1489 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001490 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001491 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001492 if (E->isTypeDependent() || E->isValueDependent() ||
1493 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001494 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1495 << "reqd_work_group_size" << E->getSourceRange();
1496 return;
1497 }
1498 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1499 }
Sean Huntcf807c42010-08-18 23:23:40 +00001500 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1501 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001502 WGSize[2]));
1503}
1504
Chandler Carruth1b03c872011-07-02 00:01:44 +00001505static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001506 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001507 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001508 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001509
1510 // Make sure that there is a string literal as the sections's single
1511 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001512 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001513 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001514 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001515 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001516 return;
1517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Chris Lattner797c3c42009-08-10 19:03:04 +00001519 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001520 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001521 if (!Error.empty()) {
1522 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1523 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001524 return;
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001527 // This attribute cannot be applied to local variables.
1528 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1529 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1530 return;
1531 }
1532
Eric Christopherf48f3672010-12-01 22:13:54 +00001533 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1534 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001535}
1536
Chris Lattner6b6b5372008-06-26 18:38:35 +00001537
Chandler Carruth1b03c872011-07-02 00:01:44 +00001538static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001539 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001540 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001541 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001542 return;
1543 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001544
Chandler Carruth87c44602011-07-01 23:49:12 +00001545 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001546 if (Existing->getLocation().isInvalid())
1547 Existing->setLocation(Attr.getLoc());
1548 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001549 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001550 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001551}
1552
Chandler Carruth1b03c872011-07-02 00:01:44 +00001553static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001554 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001555 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001556 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001557 return;
1558 }
Mike Stumpbf916502009-07-24 19:02:52 +00001559
Chandler Carruth87c44602011-07-01 23:49:12 +00001560 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001561 if (Existing->getLocation().isInvalid())
1562 Existing->setLocation(Attr.getLoc());
1563 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001564 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001565 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001566}
1567
Chandler Carruth1b03c872011-07-02 00:01:44 +00001568static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001569 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001570 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001571 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001572
Chandler Carruth87c44602011-07-01 23:49:12 +00001573 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001574}
1575
Chandler Carruth1b03c872011-07-02 00:01:44 +00001576static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001577 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001578 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1579 return;
1580 }
Mike Stumpbf916502009-07-24 19:02:52 +00001581
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001582 if (Attr.getNumArgs() != 0) {
1583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1584 return;
1585 }
Mike Stumpbf916502009-07-24 19:02:52 +00001586
Chandler Carruth87c44602011-07-01 23:49:12 +00001587 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00001588
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001589 if (!VD || !VD->hasLocalStorage()) {
1590 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1591 return;
1592 }
Mike Stumpbf916502009-07-24 19:02:52 +00001593
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001594 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001595 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001596 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001597 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1598 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001599 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001600 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001601 Attr.getParameterName();
1602 return;
1603 }
Mike Stumpbf916502009-07-24 19:02:52 +00001604
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001605 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1606 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001607 S.Diag(Attr.getParameterLoc(),
1608 diag::err_attribute_cleanup_arg_not_function)
1609 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001610 return;
1611 }
1612
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001613 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001614 S.Diag(Attr.getParameterLoc(),
1615 diag::err_attribute_cleanup_func_must_take_one_arg)
1616 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001617 return;
1618 }
Mike Stumpbf916502009-07-24 19:02:52 +00001619
Anders Carlsson89941c12009-02-07 23:16:50 +00001620 // We're currently more strict than GCC about what function types we accept.
1621 // If this ever proves to be a problem it should be easy to fix.
1622 QualType Ty = S.Context.getPointerType(VD->getType());
1623 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001624 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1625 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001626 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001627 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1628 Attr.getParameterName() << ParamTy << Ty;
1629 return;
1630 }
Mike Stumpbf916502009-07-24 19:02:52 +00001631
Chandler Carruth87c44602011-07-01 23:49:12 +00001632 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001633 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001634}
1635
Mike Stumpbf916502009-07-24 19:02:52 +00001636/// Handle __attribute__((format_arg((idx)))) attribute based on
1637/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001638static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001639 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001640 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001641
Chandler Carruth87c44602011-07-01 23:49:12 +00001642 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001643 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001644 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001645 return;
1646 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001647
1648 // In C++ the implicit 'this' function parameter also counts, and they are
1649 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001650 bool HasImplicitThisParam = isInstanceMethod(D);
1651 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001652 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001653
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001654 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001655 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001656 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001657 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1658 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001659 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1660 << "format" << 2 << IdxExpr->getSourceRange();
1661 return;
1662 }
Mike Stumpbf916502009-07-24 19:02:52 +00001663
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001664 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1666 << "format" << 2 << IdxExpr->getSourceRange();
1667 return;
1668 }
Mike Stumpbf916502009-07-24 19:02:52 +00001669
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001670 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001671
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001672 if (HasImplicitThisParam) {
1673 if (ArgIdx == 0) {
1674 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1675 << "format_arg" << IdxExpr->getSourceRange();
1676 return;
1677 }
1678 ArgIdx--;
1679 }
1680
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001681 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001682 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001683
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001684 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1685 if (not_nsstring_type &&
1686 !isCFStringType(Ty, S.Context) &&
1687 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001688 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001689 // FIXME: Should highlight the actual expression that has the wrong type.
1690 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001691 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001692 << IdxExpr->getSourceRange();
1693 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001694 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001695 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001696 if (!isNSStringType(Ty, S.Context) &&
1697 !isCFStringType(Ty, S.Context) &&
1698 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001699 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001700 // FIXME: Should highlight the actual expression that has the wrong type.
1701 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001702 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001703 << IdxExpr->getSourceRange();
1704 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001705 }
1706
Chandler Carruth87c44602011-07-01 23:49:12 +00001707 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001708 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001709}
1710
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001711enum FormatAttrKind {
1712 CFStringFormat,
1713 NSStringFormat,
1714 StrftimeFormat,
1715 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001716 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001717 InvalidFormat
1718};
1719
1720/// getFormatAttrKind - Map from format attribute names to supported format
1721/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001722static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001723 // Check for formats that get handled specially.
1724 if (Format == "NSString")
1725 return NSStringFormat;
1726 if (Format == "CFString")
1727 return CFStringFormat;
1728 if (Format == "strftime")
1729 return StrftimeFormat;
1730
1731 // Otherwise, check for supported formats.
1732 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1733 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1734 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001735 Format == "zcmn_err" ||
1736 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001737 return SupportedFormat;
1738
Duncan Sandsbc525952010-03-23 14:44:19 +00001739 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1740 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001741 return IgnoredFormat;
1742
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001743 return InvalidFormat;
1744}
1745
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001746/// Handle __attribute__((init_priority(priority))) attributes based on
1747/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001748static void handleInitPriorityAttr(Sema &S, Decl *D,
1749 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001750 if (!S.getLangOptions().CPlusPlus) {
1751 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1752 return;
1753 }
1754
Chandler Carruth87c44602011-07-01 23:49:12 +00001755 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001756 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1757 Attr.setInvalid();
1758 return;
1759 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001760 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001761 if (S.Context.getAsArrayType(T))
1762 T = S.Context.getBaseElementType(T);
1763 if (!T->getAs<RecordType>()) {
1764 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1765 Attr.setInvalid();
1766 return;
1767 }
1768
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001769 if (Attr.getNumArgs() != 1) {
1770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1771 Attr.setInvalid();
1772 return;
1773 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001774 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001775
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001776 llvm::APSInt priority(32);
1777 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1778 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1779 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1780 << "init_priority" << priorityExpr->getSourceRange();
1781 Attr.setInvalid();
1782 return;
1783 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001784 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001785 if (prioritynum < 101 || prioritynum > 65535) {
1786 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1787 << priorityExpr->getSourceRange();
1788 Attr.setInvalid();
1789 return;
1790 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001791 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001792 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001793}
1794
Mike Stumpbf916502009-07-24 19:02:52 +00001795/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1796/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001797static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001798
Chris Lattner545dd342008-06-28 23:36:30 +00001799 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001800 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001801 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001802 return;
1803 }
1804
Chris Lattner545dd342008-06-28 23:36:30 +00001805 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001807 return;
1808 }
1809
Chandler Carruth87c44602011-07-01 23:49:12 +00001810 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001811 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001812 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001813 return;
1814 }
1815
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001816 // In C++ the implicit 'this' function parameter also counts, and they are
1817 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001818 bool HasImplicitThisParam = isInstanceMethod(D);
1819 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001820 unsigned FirstIdx = 1;
1821
Chris Lattner5f9e2722011-07-23 10:55:15 +00001822 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001823
1824 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001825 if (Format.startswith("__") && Format.endswith("__"))
1826 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001827
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001828 // Check for supported formats.
1829 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001830
1831 if (Kind == IgnoredFormat)
1832 return;
1833
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001834 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001835 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001836 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001837 return;
1838 }
1839
1840 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001841 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001842 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001843 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1844 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001846 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001847 return;
1848 }
1849
1850 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001851 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001852 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001853 return;
1854 }
1855
1856 // FIXME: Do we need to bounds check?
1857 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001858
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001859 if (HasImplicitThisParam) {
1860 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001861 S.Diag(Attr.getLoc(),
1862 diag::err_format_attribute_implicit_this_format_string)
1863 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001864 return;
1865 }
1866 ArgIdx--;
1867 }
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Chris Lattner6b6b5372008-06-26 18:38:35 +00001869 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001870 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001871
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001872 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001873 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001874 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1875 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001876 return;
1877 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001878 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001879 // FIXME: do we need to check if the type is NSString*? What are the
1880 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001881 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001882 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001883 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1884 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001885 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001886 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001887 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001888 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001889 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001890 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1891 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001892 return;
1893 }
1894
1895 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001896 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001897 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001898 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1899 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001901 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001902 return;
1903 }
1904
1905 // check if the function is variadic if the 3rd argument non-zero
1906 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001907 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001908 ++NumArgs; // +1 for ...
1909 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001910 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001911 return;
1912 }
1913 }
1914
Chris Lattner3c73c412008-11-19 08:23:25 +00001915 // strftime requires FirstArg to be 0 because it doesn't read from any
1916 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001917 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001918 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001919 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1920 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001921 return;
1922 }
1923 // if 0 it disables parameter checking (to use with e.g. va_list)
1924 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001925 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001926 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001927 return;
1928 }
1929
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001930 // Check whether we already have an equivalent format attribute.
1931 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001932 i = D->specific_attr_begin<FormatAttr>(),
1933 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001934 i != e ; ++i) {
1935 FormatAttr *f = *i;
1936 if (f->getType() == Format &&
1937 f->getFormatIdx() == (int)Idx.getZExtValue() &&
1938 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
1939 // If we don't have a valid location for this attribute, adopt the
1940 // location.
1941 if (f->getLocation().isInvalid())
1942 f->setLocation(Attr.getLoc());
1943 return;
1944 }
1945 }
1946
Chandler Carruth87c44602011-07-01 23:49:12 +00001947 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00001948 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001949 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001950}
1951
Chandler Carruth1b03c872011-07-02 00:01:44 +00001952static void handleTransparentUnionAttr(Sema &S, Decl *D,
1953 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001954 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001955 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001956 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001957
Chris Lattner6b6b5372008-06-26 18:38:35 +00001958
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001959 // Try to find the underlying union declaration.
1960 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00001961 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001962 if (TD && TD->getUnderlyingType()->isUnionType())
1963 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1964 else
Chandler Carruth87c44602011-07-01 23:49:12 +00001965 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001966
1967 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001968 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001969 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001970 return;
1971 }
1972
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001973 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001974 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001975 diag::warn_transparent_union_attribute_not_definition);
1976 return;
1977 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001978
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001979 RecordDecl::field_iterator Field = RD->field_begin(),
1980 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001981 if (Field == FieldEnd) {
1982 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1983 return;
1984 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001985
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001986 FieldDecl *FirstField = *Field;
1987 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001988 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001989 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001990 diag::warn_transparent_union_attribute_floating)
1991 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001992 return;
1993 }
1994
1995 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1996 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1997 for (; Field != FieldEnd; ++Field) {
1998 QualType FieldType = Field->getType();
1999 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2000 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2001 // Warn if we drop the attribute.
2002 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002003 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002004 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002005 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002006 diag::warn_transparent_union_attribute_field_size_align)
2007 << isSize << Field->getDeclName() << FieldBits;
2008 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002009 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002010 diag::note_transparent_union_first_field_size_align)
2011 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002012 return;
2013 }
2014 }
2015
Sean Huntcf807c42010-08-18 23:23:40 +00002016 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002017}
2018
Chandler Carruth1b03c872011-07-02 00:01:44 +00002019static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002020 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002021 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002022 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002023
Peter Collingbourne7a730022010-11-23 20:45:58 +00002024 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002025 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002026
Chris Lattner6b6b5372008-06-26 18:38:35 +00002027 // Make sure that there is a string literal as the annotation's single
2028 // argument.
2029 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002030 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002031 return;
2032 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002033 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002034 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002035}
2036
Chandler Carruth1b03c872011-07-02 00:01:44 +00002037static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002038 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002039 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002041 return;
2042 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002043
2044 //FIXME: The C++0x version of this attribute has more limited applicabilty
2045 // than GNU's, and should error out when it is used to specify a
2046 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002047
Chris Lattner545dd342008-06-28 23:36:30 +00002048 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002049 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002050 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002051 }
Mike Stumpbf916502009-07-24 19:02:52 +00002052
Peter Collingbourne7a730022010-11-23 20:45:58 +00002053 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002054}
2055
2056void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2057 if (E->isTypeDependent() || E->isValueDependent()) {
2058 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002059 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002060 return;
2061 }
2062
Sean Huntcf807c42010-08-18 23:23:40 +00002063 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002064 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002065 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2066 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2067 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002068 return;
2069 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002070 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002071 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2072 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002073 return;
2074 }
2075
Sean Huntcf807c42010-08-18 23:23:40 +00002076 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2077}
2078
2079void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2080 // FIXME: Cache the number on the Attr object if non-dependent?
2081 // FIXME: Perform checking of type validity
2082 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2083 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002084}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002085
Chandler Carruthd309c812011-07-01 23:49:16 +00002086/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002087/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002088///
Mike Stumpbf916502009-07-24 19:02:52 +00002089/// Despite what would be logical, the mode attribute is a decl attribute, not a
2090/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2091/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002092static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002093 // This attribute isn't documented, but glibc uses it. It changes
2094 // the width of an int or unsigned int to the specified size.
2095
2096 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002097 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002098 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002099
Chris Lattnerfbf13472008-06-27 22:18:37 +00002100
2101 IdentifierInfo *Name = Attr.getParameterName();
2102 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002103 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002104 return;
2105 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002106
Chris Lattner5f9e2722011-07-23 10:55:15 +00002107 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002108
2109 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002110 if (Str.startswith("__") && Str.endswith("__"))
2111 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002112
2113 unsigned DestWidth = 0;
2114 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002115 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002116 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002117 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002118 switch (Str[0]) {
2119 case 'Q': DestWidth = 8; break;
2120 case 'H': DestWidth = 16; break;
2121 case 'S': DestWidth = 32; break;
2122 case 'D': DestWidth = 64; break;
2123 case 'X': DestWidth = 96; break;
2124 case 'T': DestWidth = 128; break;
2125 }
2126 if (Str[1] == 'F') {
2127 IntegerMode = false;
2128 } else if (Str[1] == 'C') {
2129 IntegerMode = false;
2130 ComplexMode = true;
2131 } else if (Str[1] != 'I') {
2132 DestWidth = 0;
2133 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002134 break;
2135 case 4:
2136 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2137 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002138 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002139 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002140 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002141 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002142 break;
2143 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002144 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002145 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002146 break;
2147 }
2148
2149 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002150 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002151 OldTy = TD->getUnderlyingType();
2152 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2153 OldTy = VD->getType();
2154 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002155 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2156 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002157 return;
2158 }
Eli Friedman73397492009-03-03 06:41:03 +00002159
John McCall183700f2009-09-21 23:43:11 +00002160 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002161 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2162 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002163 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002164 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2165 } else if (ComplexMode) {
2166 if (!OldTy->isComplexType())
2167 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2168 } else {
2169 if (!OldTy->isFloatingType())
2170 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2171 }
2172
Mike Stump390b4cc2009-05-16 07:39:55 +00002173 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2174 // and friends, at least with glibc.
2175 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2176 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002177 // FIXME: Make sure floating-point mappings are accurate
2178 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002179 QualType NewTy;
2180 switch (DestWidth) {
2181 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002182 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002183 return;
2184 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002185 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002186 return;
2187 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002188 if (!IntegerMode) {
2189 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2190 return;
2191 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002192 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002193 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002194 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002195 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002196 break;
2197 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002198 if (!IntegerMode) {
2199 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2200 return;
2201 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002202 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002203 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002204 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002205 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002206 break;
2207 case 32:
2208 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002209 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002210 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002211 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002212 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002213 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002214 break;
2215 case 64:
2216 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002217 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002218 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002219 if (S.Context.Target.getLongWidth() == 64)
2220 NewTy = S.Context.LongTy;
2221 else
2222 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002223 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002224 if (S.Context.Target.getLongWidth() == 64)
2225 NewTy = S.Context.UnsignedLongTy;
2226 else
2227 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002228 break;
Eli Friedman73397492009-03-03 06:41:03 +00002229 case 96:
2230 NewTy = S.Context.LongDoubleTy;
2231 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002232 case 128:
2233 if (!IntegerMode) {
2234 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2235 return;
2236 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002237 if (OldTy->isSignedIntegerType())
2238 NewTy = S.Context.Int128Ty;
2239 else
2240 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002241 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002242 }
2243
Eli Friedman73397492009-03-03 06:41:03 +00002244 if (ComplexMode) {
2245 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002246 }
2247
2248 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002249 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002250 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002251 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002252 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002253 cast<ValueDecl>(D)->setType(NewTy);
2254}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002255
Chandler Carruth1b03c872011-07-02 00:01:44 +00002256static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002257 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002258 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002259 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002260
Chandler Carruth87c44602011-07-01 23:49:12 +00002261 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002262 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002263 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002264 return;
2265 }
Mike Stumpbf916502009-07-24 19:02:52 +00002266
Chandler Carruth87c44602011-07-01 23:49:12 +00002267 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002268}
2269
Chandler Carruth1b03c872011-07-02 00:01:44 +00002270static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002271 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002272 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002273 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002274
Mike Stumpbf916502009-07-24 19:02:52 +00002275
Chandler Carruth87c44602011-07-01 23:49:12 +00002276 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002277 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002278 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002279 return;
2280 }
Mike Stumpbf916502009-07-24 19:02:52 +00002281
Chandler Carruth87c44602011-07-01 23:49:12 +00002282 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002283}
2284
Chandler Carruth1b03c872011-07-02 00:01:44 +00002285static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2286 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002287 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002288 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002289 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002290
Chris Lattner7255a2d2010-06-22 00:03:40 +00002291
Chandler Carruth87c44602011-07-01 23:49:12 +00002292 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002293 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002294 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002295 return;
2296 }
2297
Chandler Carruth87c44602011-07-01 23:49:12 +00002298 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002299 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002300}
2301
Chandler Carruth1b03c872011-07-02 00:01:44 +00002302static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002303 if (S.LangOpts.CUDA) {
2304 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002305 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2307 return;
2308 }
2309
Chandler Carruth87c44602011-07-01 23:49:12 +00002310 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002311 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002312 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002313 return;
2314 }
2315
Chandler Carruth87c44602011-07-01 23:49:12 +00002316 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002317 } else {
2318 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2319 }
2320}
2321
Chandler Carruth1b03c872011-07-02 00:01:44 +00002322static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002323 if (S.LangOpts.CUDA) {
2324 // check the attribute arguments.
2325 if (Attr.getNumArgs() != 0) {
2326 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2327 return;
2328 }
2329
Chandler Carruth87c44602011-07-01 23:49:12 +00002330 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002331 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002332 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002333 return;
2334 }
2335
Chandler Carruth87c44602011-07-01 23:49:12 +00002336 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002337 } else {
2338 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2339 }
2340}
2341
Chandler Carruth1b03c872011-07-02 00:01:44 +00002342static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002343 if (S.LangOpts.CUDA) {
2344 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002345 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002346 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002347
Chandler Carruth87c44602011-07-01 23:49:12 +00002348 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002349 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002350 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002351 return;
2352 }
2353
Chandler Carruth87c44602011-07-01 23:49:12 +00002354 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002355 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002356 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002357 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2358 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2359 << FD->getType()
2360 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2361 "void");
2362 } else {
2363 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2364 << FD->getType();
2365 }
2366 return;
2367 }
2368
Chandler Carruth87c44602011-07-01 23:49:12 +00002369 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002370 } else {
2371 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2372 }
2373}
2374
Chandler Carruth1b03c872011-07-02 00:01:44 +00002375static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002376 if (S.LangOpts.CUDA) {
2377 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002378 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002379 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002380
Peter Collingbourneced76712010-12-01 03:15:31 +00002381
Chandler Carruth87c44602011-07-01 23:49:12 +00002382 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002384 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002385 return;
2386 }
2387
Chandler Carruth87c44602011-07-01 23:49:12 +00002388 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002389 } else {
2390 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2391 }
2392}
2393
Chandler Carruth1b03c872011-07-02 00:01:44 +00002394static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002395 if (S.LangOpts.CUDA) {
2396 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002397 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002398 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002399
Peter Collingbourneced76712010-12-01 03:15:31 +00002400
Chandler Carruth87c44602011-07-01 23:49:12 +00002401 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002402 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002403 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002404 return;
2405 }
2406
Chandler Carruth87c44602011-07-01 23:49:12 +00002407 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002408 } else {
2409 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2410 }
2411}
2412
Chandler Carruth1b03c872011-07-02 00:01:44 +00002413static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002414 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002415 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002416 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002417
Chandler Carruth87c44602011-07-01 23:49:12 +00002418 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002419 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002421 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002422 return;
2423 }
Mike Stumpbf916502009-07-24 19:02:52 +00002424
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002425 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002426 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002427 return;
2428 }
Mike Stumpbf916502009-07-24 19:02:52 +00002429
Chandler Carruth87c44602011-07-01 23:49:12 +00002430 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002431}
2432
Chandler Carruth1b03c872011-07-02 00:01:44 +00002433static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002434 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002435
Chandler Carruth87c44602011-07-01 23:49:12 +00002436 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002437 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2438 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002439 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002440 return;
2441
Chandler Carruth87c44602011-07-01 23:49:12 +00002442 if (!isa<ObjCMethodDecl>(D)) {
2443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2444 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002445 return;
2446 }
2447
Chandler Carruth87c44602011-07-01 23:49:12 +00002448 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002449 case AttributeList::AT_fastcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002450 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002451 return;
2452 case AttributeList::AT_stdcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002453 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002454 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002455 case AttributeList::AT_thiscall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002456 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002457 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002458 case AttributeList::AT_cdecl:
Chandler Carruth87c44602011-07-01 23:49:12 +00002459 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002460 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002461 case AttributeList::AT_pascal:
Chandler Carruth87c44602011-07-01 23:49:12 +00002462 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002463 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002464 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002465 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002466 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2467 if (Str == 0 || Str->isWide()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002468 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002469 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002470 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002471 return;
2472 }
2473
Chris Lattner5f9e2722011-07-23 10:55:15 +00002474 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002475 PcsAttr::PCSType PCS;
2476 if (StrRef == "aapcs")
2477 PCS = PcsAttr::AAPCS;
2478 else if (StrRef == "aapcs-vfp")
2479 PCS = PcsAttr::AAPCS_VFP;
2480 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002481 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2482 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002483 return;
2484 }
2485
Chandler Carruth87c44602011-07-01 23:49:12 +00002486 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002487 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002488 default:
2489 llvm_unreachable("unexpected attribute kind");
2490 return;
2491 }
2492}
2493
Chandler Carruth1b03c872011-07-02 00:01:44 +00002494static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00002495 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00002496 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002497}
2498
John McCall711c52b2011-01-05 12:14:39 +00002499bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2500 if (attr.isInvalid())
2501 return true;
2502
Ted Kremenek831efae2011-04-15 05:49:29 +00002503 if ((attr.getNumArgs() != 0 &&
2504 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2505 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002506 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2507 attr.setInvalid();
2508 return true;
2509 }
2510
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002511 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2512 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002513 switch (attr.getKind()) {
2514 case AttributeList::AT_cdecl: CC = CC_C; break;
2515 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2516 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2517 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2518 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002519 case AttributeList::AT_pcs: {
2520 Expr *Arg = attr.getArg(0);
2521 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2522 if (Str == 0 || Str->isWide()) {
2523 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2524 << "pcs" << 1;
2525 attr.setInvalid();
2526 return true;
2527 }
2528
Chris Lattner5f9e2722011-07-23 10:55:15 +00002529 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002530 if (StrRef == "aapcs") {
2531 CC = CC_AAPCS;
2532 break;
2533 } else if (StrRef == "aapcs-vfp") {
2534 CC = CC_AAPCS_VFP;
2535 break;
2536 }
2537 // FALLS THROUGH
2538 }
John McCall711c52b2011-01-05 12:14:39 +00002539 default: llvm_unreachable("unexpected attribute kind"); return true;
2540 }
2541
2542 return false;
2543}
2544
Chandler Carruth1b03c872011-07-02 00:01:44 +00002545static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002546 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00002547
2548 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00002549 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00002550 return;
2551
Chandler Carruth87c44602011-07-01 23:49:12 +00002552 if (!isa<ObjCMethodDecl>(D)) {
2553 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2554 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002555 return;
2556 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002557
Chandler Carruth87c44602011-07-01 23:49:12 +00002558 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00002559}
2560
2561/// Checks a regparm attribute, returning true if it is ill-formed and
2562/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00002563bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2564 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00002565 return true;
2566
Chandler Carruth87c44602011-07-01 23:49:12 +00002567 if (Attr.getNumArgs() != 1) {
2568 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2569 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002570 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002571 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002572
Chandler Carruth87c44602011-07-01 23:49:12 +00002573 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002574 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002575 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002576 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002577 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002578 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002579 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002580 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002581 }
2582
John McCall711c52b2011-01-05 12:14:39 +00002583 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002584 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002585 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002586 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002587 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002588 }
2589
John McCall711c52b2011-01-05 12:14:39 +00002590 numParams = NumParams.getZExtValue();
2591 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002592 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall711c52b2011-01-05 12:14:39 +00002593 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002594 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002595 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002596 }
2597
John McCall711c52b2011-01-05 12:14:39 +00002598 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002599}
2600
Chandler Carruth1b03c872011-07-02 00:01:44 +00002601static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00002602 if (S.LangOpts.CUDA) {
2603 // check the attribute arguments.
2604 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002605 // FIXME: 0 is not okay.
2606 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002607 return;
2608 }
2609
Chandler Carruth87c44602011-07-01 23:49:12 +00002610 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00002611 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002612 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002613 return;
2614 }
2615
2616 Expr *MaxThreadsExpr = Attr.getArg(0);
2617 llvm::APSInt MaxThreads(32);
2618 if (MaxThreadsExpr->isTypeDependent() ||
2619 MaxThreadsExpr->isValueDependent() ||
2620 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2621 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2622 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2623 return;
2624 }
2625
2626 llvm::APSInt MinBlocks(32);
2627 if (Attr.getNumArgs() > 1) {
2628 Expr *MinBlocksExpr = Attr.getArg(1);
2629 if (MinBlocksExpr->isTypeDependent() ||
2630 MinBlocksExpr->isValueDependent() ||
2631 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2632 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2633 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2634 return;
2635 }
2636 }
2637
Chandler Carruth87c44602011-07-01 23:49:12 +00002638 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00002639 MaxThreads.getZExtValue(),
2640 MinBlocks.getZExtValue()));
2641 } else {
2642 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2643 }
2644}
2645
Chris Lattner0744e5f2008-06-29 00:23:49 +00002646//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002647// Checker-specific attribute handlers.
2648//===----------------------------------------------------------------------===//
2649
John McCallc7ad3812011-01-25 03:31:58 +00002650static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2651 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2652}
2653static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2654 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2655}
2656
Chandler Carruth1b03c872011-07-02 00:01:44 +00002657static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002658 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00002659 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002660 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2661 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002662 return;
2663 }
2664
2665 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002666 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00002667 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2668 cf = false;
2669 } else {
2670 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2671 cf = true;
2672 }
2673
2674 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002675 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2676 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00002677 return;
2678 }
2679
2680 if (cf)
Chandler Carruth87c44602011-07-01 23:49:12 +00002681 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002682 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002683 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002684}
2685
Chandler Carruth1b03c872011-07-02 00:01:44 +00002686static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
2687 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002688 if (!isa<ObjCMethodDecl>(D)) {
2689 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2690 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002691 return;
2692 }
2693
Chandler Carruth87c44602011-07-01 23:49:12 +00002694 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002695}
2696
Chandler Carruth1b03c872011-07-02 00:01:44 +00002697static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
2698 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002699
John McCallc7ad3812011-01-25 03:31:58 +00002700 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002701
Chandler Carruth87c44602011-07-01 23:49:12 +00002702 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002703 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002704 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00002705 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002706 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2707 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00002708 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00002709 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002710 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002711 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002712 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2713 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002714 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002715 return;
2716 }
Mike Stumpbf916502009-07-24 19:02:52 +00002717
John McCallc7ad3812011-01-25 03:31:58 +00002718 bool typeOK;
2719 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002720 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00002721 default: llvm_unreachable("invalid ownership attribute"); return;
2722 case AttributeList::AT_ns_returns_autoreleased:
2723 case AttributeList::AT_ns_returns_retained:
2724 case AttributeList::AT_ns_returns_not_retained:
2725 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2726 cf = false;
2727 break;
2728
2729 case AttributeList::AT_cf_returns_retained:
2730 case AttributeList::AT_cf_returns_not_retained:
2731 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2732 cf = true;
2733 break;
2734 }
2735
2736 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002737 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2738 << SourceRange(Attr.getLoc())
2739 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002740 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002741 }
Mike Stumpbf916502009-07-24 19:02:52 +00002742
Chandler Carruth87c44602011-07-01 23:49:12 +00002743 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002744 default:
2745 assert(0 && "invalid ownership attribute");
2746 return;
John McCallc7ad3812011-01-25 03:31:58 +00002747 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruth87c44602011-07-01 23:49:12 +00002748 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCallc7ad3812011-01-25 03:31:58 +00002749 S.Context));
2750 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002751 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002752 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002753 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002754 return;
2755 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002756 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002757 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002758 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002759 case AttributeList::AT_cf_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002760 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002761 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002762 return;
2763 case AttributeList::AT_ns_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002764 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002765 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002766 return;
2767 };
2768}
2769
John McCalldc7c5ad2011-07-22 08:53:00 +00002770static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
2771 const AttributeList &attr) {
2772 SourceLocation loc = attr.getLoc();
2773
2774 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
2775
2776 if (!isa<ObjCMethodDecl>(method)) {
2777 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
2778 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
2779 return;
2780 }
2781
2782 // Check that the method returns a normal pointer.
2783 QualType resultType = method->getResultType();
2784 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
2785 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2786 << SourceRange(loc)
2787 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
2788
2789 // Drop the attribute.
2790 return;
2791 }
2792
2793 method->addAttr(
2794 ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context));
2795}
2796
Chandler Carruth1b03c872011-07-02 00:01:44 +00002797static void handleObjCOwnershipAttr(Sema &S, Decl *D,
2798 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002799 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00002800
Chandler Carruth87c44602011-07-01 23:49:12 +00002801 SourceLocation L = Attr.getLoc();
2802 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2803 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00002804}
2805
Chandler Carruth1b03c872011-07-02 00:01:44 +00002806static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
2807 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002808 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
2809 SourceLocation L = Attr.getLoc();
2810 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2811 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00002812 return;
2813 }
2814
Chandler Carruth87c44602011-07-01 23:49:12 +00002815 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00002816 QualType type = vd->getType();
2817
2818 if (!type->isDependentType() &&
2819 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002820 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00002821 << type;
2822 return;
2823 }
2824
2825 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
2826
2827 // If we have no lifetime yet, check the lifetime we're presumably
2828 // going to infer.
2829 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
2830 lifetime = type->getObjCARCImplicitLifetime();
2831
2832 switch (lifetime) {
2833 case Qualifiers::OCL_None:
2834 assert(type->isDependentType() &&
2835 "didn't infer lifetime for non-dependent type?");
2836 break;
2837
2838 case Qualifiers::OCL_Weak: // meaningful
2839 case Qualifiers::OCL_Strong: // meaningful
2840 break;
2841
2842 case Qualifiers::OCL_ExplicitNone:
2843 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00002844 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00002845 << (lifetime == Qualifiers::OCL_Autoreleasing);
2846 break;
2847 }
2848
Chandler Carruth87c44602011-07-01 23:49:12 +00002849 D->addAttr(::new (S.Context)
2850 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00002851}
2852
Charles Davisf0122fe2010-02-16 18:27:26 +00002853static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2854 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002855 Attr.getKind() == AttributeList::AT_dllexport ||
2856 Attr.getKind() == AttributeList::AT_uuid;
2857}
2858
2859//===----------------------------------------------------------------------===//
2860// Microsoft specific attribute handlers.
2861//===----------------------------------------------------------------------===//
2862
Chandler Carruth1b03c872011-07-02 00:01:44 +00002863static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet11542142010-12-19 06:50:37 +00002864 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2865 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002866 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00002867 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002868
Francois Pichet11542142010-12-19 06:50:37 +00002869 Expr *Arg = Attr.getArg(0);
2870 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002871 if (Str == 0 || Str->isWide()) {
2872 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2873 << "uuid" << 1;
2874 return;
2875 }
2876
Chris Lattner5f9e2722011-07-23 10:55:15 +00002877 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00002878
2879 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2880 StrRef.back() == '}';
2881
2882 // Validate GUID length.
2883 if (IsCurly && StrRef.size() != 38) {
2884 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2885 return;
2886 }
2887 if (!IsCurly && StrRef.size() != 36) {
2888 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2889 return;
2890 }
2891
2892 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2893 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00002894 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00002895 if (IsCurly) // Skip the optional '{'
2896 ++I;
2897
2898 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002899 if (i == 8 || i == 13 || i == 18 || i == 23) {
2900 if (*I != '-') {
2901 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2902 return;
2903 }
2904 } else if (!isxdigit(*I)) {
2905 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2906 return;
2907 }
2908 I++;
2909 }
Francois Pichet11542142010-12-19 06:50:37 +00002910
Chandler Carruth87c44602011-07-01 23:49:12 +00002911 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00002912 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002913 } else
Francois Pichet11542142010-12-19 06:50:37 +00002914 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002915}
2916
Ted Kremenekb71368d2009-05-09 02:44:38 +00002917//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002918// Top Level Sema Entry Points
2919//===----------------------------------------------------------------------===//
2920
Chandler Carruth1b03c872011-07-02 00:01:44 +00002921static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
2922 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00002923 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00002924 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
2925 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
2926 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002927 default:
2928 break;
2929 }
2930}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002931
Chandler Carruth1b03c872011-07-02 00:01:44 +00002932static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
2933 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00002934 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00002935 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
2936 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002937 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002938 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002939 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002940 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002941 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002942 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002943 case AttributeList::AT_neon_vector_type:
2944 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002945 // Ignore these, these are type attributes, handled by
2946 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002947 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002948 case AttributeList::AT_device:
2949 case AttributeList::AT_host:
2950 case AttributeList::AT_overloadable:
2951 // Ignore, this is a non-inheritable attribute, handled
2952 // by ProcessNonInheritableDeclAttr.
2953 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00002954 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
2955 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002956 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002957 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002958 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002959 handleAnalyzerNoReturnAttr (S, D, Attr); break;
2960 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
2961 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002962 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002963 handleDependencyAttr (S, D, Attr); break;
2964 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
2965 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
2966 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
2967 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
2968 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002969 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002970 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00002971 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00002972 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
2973 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
2974 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
2975 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002976 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002977 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00002978 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00002979 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
2980 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
2981 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
2982 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
2983 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002984 case AttributeList::AT_ownership_returns:
2985 case AttributeList::AT_ownership_takes:
2986 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002987 handleOwnershipAttr (S, D, Attr); break;
2988 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
2989 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
2990 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
2991 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
2992 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002993
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00002994 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002995 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00002996 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00002997 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00002998
John McCalldc7c5ad2011-07-22 08:53:00 +00002999 case AttributeList::AT_objc_returns_inner_pointer:
3000 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3001
Ted Kremenekb71368d2009-05-09 02:44:38 +00003002 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003003 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003004 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003005 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003006 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003007
3008 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003009 case AttributeList::AT_ns_returns_not_retained:
3010 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003011 case AttributeList::AT_ns_returns_retained:
3012 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003013 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003014
Nate Begeman6f3d8382009-06-26 06:32:41 +00003015 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003016 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003017
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003018 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003019 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003020
Chandler Carruth1b03c872011-07-02 00:01:44 +00003021 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3022 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3023 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3024 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003025 case AttributeList::AT_arc_weakref_unavailable:
3026 handleArcWeakrefUnavailableAttr (S, D, Attr);
3027 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003028 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3029 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3030 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3031 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003032 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003033 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3034 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3035 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003036 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003037 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003038 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003039 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003040 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003041 break;
John McCalld5313b02011-03-02 11:33:24 +00003042 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003043 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003044 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003045 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3046 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3047 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3048 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3049 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3050 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3051 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3052 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3053 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003054 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003055 // Just ignore
3056 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003057 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003058 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003059 break;
John McCall04a67a62010-02-05 21:31:56 +00003060 case AttributeList::AT_stdcall:
3061 case AttributeList::AT_cdecl:
3062 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003063 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003064 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003065 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003066 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003067 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003068 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003069 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003070 break;
Francois Pichet11542142010-12-19 06:50:37 +00003071 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003072 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003073 break;
Chris Lattner803d0802008-06-29 00:43:07 +00003074 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003075 // Ask target about the attribute.
3076 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3077 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003078 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3079 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003080 break;
3081 }
3082}
3083
Peter Collingbourne60700392011-01-21 02:08:45 +00003084/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3085/// the attribute applies to decls. If the attribute is a type attribute, just
3086/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3087/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003088static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3089 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003090 bool NonInheritable, bool Inheritable) {
3091 if (Attr.isInvalid())
3092 return;
3093
3094 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3095 // FIXME: Try to deal with other __declspec attributes!
3096 return;
3097
3098 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003099 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003100
3101 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003102 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003103}
3104
Chris Lattner803d0802008-06-29 00:43:07 +00003105/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3106/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003107void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003108 const AttributeList *AttrList,
3109 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003110 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003111 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003112 }
3113
3114 // GCC accepts
3115 // static int a9 __attribute__((weakref));
3116 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003117 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003118 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003119 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003120 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003121 }
3122}
3123
Ryan Flynne25ff832009-07-30 03:15:39 +00003124/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3125/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003126NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003127 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003128 NamedDecl *NewD = 0;
3129 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3130 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003131 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003132 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003133 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003134 if (FD->getQualifier()) {
3135 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003136 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003137 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003138 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3139 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003140 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003141 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003142 VD->getStorageClass(),
3143 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003144 if (VD->getQualifier()) {
3145 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003146 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003147 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003148 }
3149 return NewD;
3150}
3151
3152/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3153/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003154void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003155 if (W.getUsed()) return; // only do this once
3156 W.setUsed(true);
3157 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3158 IdentifierInfo *NDId = ND->getIdentifier();
3159 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003160 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3161 NDId->getName()));
3162 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003163 WeakTopLevelDecl.push_back(NewD);
3164 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3165 // to insert Decl at TU scope, sorry.
3166 DeclContext *SavedContext = CurContext;
3167 CurContext = Context.getTranslationUnitDecl();
3168 PushOnScopeChains(NewD, S);
3169 CurContext = SavedContext;
3170 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003171 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003172 }
3173}
3174
Chris Lattner0744e5f2008-06-29 00:23:49 +00003175/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3176/// it, apply them to D. This is a bit tricky because PD can have attributes
3177/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003178void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3179 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003180 // It's valid to "forward-declare" #pragma weak, in which case we
3181 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00003182 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00003183 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3184 if (IdentifierInfo *Id = ND->getIdentifier()) {
3185 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3186 = WeakUndeclaredIdentifiers.find(Id);
3187 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3188 WeakInfo W = I->second;
3189 DeclApplyPragmaWeak(S, ND, W);
3190 WeakUndeclaredIdentifiers[Id] = W;
3191 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003192 }
3193 }
3194 }
3195
Chris Lattner0744e5f2008-06-29 00:23:49 +00003196 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003197 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003198 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003199
Chris Lattner0744e5f2008-06-29 00:23:49 +00003200 // Walk the declarator structure, applying decl attributes that were in a type
3201 // position to the decl itself. This handles cases like:
3202 // int *__attr__(x)** D;
3203 // when X is a decl attribute.
3204 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3205 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003206 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003207
Chris Lattner0744e5f2008-06-29 00:23:49 +00003208 // Finally, apply any attributes on the decl itself.
3209 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003210 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003211}
John McCall54abf7d2009-11-04 02:18:39 +00003212
John McCallf85e1932011-06-15 23:02:42 +00003213/// Is the given declaration allowed to use a forbidden type?
3214static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3215 // Private ivars are always okay. Unfortunately, people don't
3216 // always properly make their ivars private, even in system headers.
3217 // Plus we need to make fields okay, too.
3218 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3219 return false;
3220
3221 // Require it to be declared in a system header.
3222 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3223}
3224
3225/// Handle a delayed forbidden-type diagnostic.
3226static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3227 Decl *decl) {
3228 if (decl && isForbiddenTypeAllowed(S, decl)) {
3229 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3230 "this system declaration uses an unsupported type"));
3231 return;
3232 }
3233
3234 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3235 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3236 diag.Triggered = true;
3237}
3238
John McCalleee1d542011-02-14 07:13:47 +00003239// This duplicates a vector push_back but hides the need to know the
3240// size of the type.
3241void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3242 assert(StackSize <= StackCapacity);
3243
3244 // Grow the stack if necessary.
3245 if (StackSize == StackCapacity) {
3246 unsigned newCapacity = 2 * StackCapacity + 2;
3247 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3248 const char *oldBuffer = (const char*) Stack;
3249
3250 if (StackCapacity)
3251 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3252
3253 delete[] oldBuffer;
3254 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3255 StackCapacity = newCapacity;
3256 }
3257
3258 assert(StackSize < StackCapacity);
3259 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003260}
3261
John McCalleee1d542011-02-14 07:13:47 +00003262void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3263 Decl *decl) {
3264 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003265
John McCalleee1d542011-02-14 07:13:47 +00003266 // Check the invariants.
3267 assert(DD.StackSize >= state.SavedStackSize);
3268 assert(state.SavedStackSize >= DD.ActiveStackBase);
3269 assert(DD.ParsingDepth > 0);
3270
3271 // Drop the parsing depth.
3272 DD.ParsingDepth--;
3273
3274 // If there are no active diagnostics, we're done.
3275 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003276 return;
3277
John McCall2f514482010-01-27 03:50:35 +00003278 // We only want to actually emit delayed diagnostics when we
3279 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003280 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003281 // We emit all the active diagnostics, not just those starting
3282 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003283 // decl spec and another for each declarator; in a decl group like:
3284 // deprecated_typedef foo, *bar, baz();
3285 // only the declarator pops will be passed decls. This is correct;
3286 // we really do need to consider delayed diagnostics from the decl spec
3287 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003288 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3289 DelayedDiagnostic &diag = DD.Stack[i];
3290 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003291 continue;
3292
John McCalleee1d542011-02-14 07:13:47 +00003293 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003294 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003295 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003296 break;
3297
3298 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003299 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003300 break;
John McCallf85e1932011-06-15 23:02:42 +00003301
3302 case DelayedDiagnostic::ForbiddenType:
3303 handleDelayedForbiddenType(S, diag, decl);
3304 break;
John McCall2f514482010-01-27 03:50:35 +00003305 }
3306 }
3307 }
3308
John McCall58e6f342010-03-16 05:22:47 +00003309 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003310 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003311 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003312
John McCalleee1d542011-02-14 07:13:47 +00003313 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003314}
3315
3316static bool isDeclDeprecated(Decl *D) {
3317 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003318 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003319 return true;
3320 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3321 return false;
3322}
3323
John McCall9c3087b2010-08-26 02:13:20 +00003324void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003325 Decl *Ctx) {
3326 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003327 return;
3328
John McCall2f514482010-01-27 03:50:35 +00003329 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003330 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003331 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003332 << DD.getDeprecationDecl()->getDeclName()
3333 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003334 else
3335 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003336 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003337}
3338
Chris Lattner5f9e2722011-07-23 10:55:15 +00003339void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003340 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003341 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003342 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003343 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3344 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003345 return;
3346 }
3347
3348 // Otherwise, don't warn if our current context is deprecated.
3349 if (isDeclDeprecated(cast<Decl>(CurContext)))
3350 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003351 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003352 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3353 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003354 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003355 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003356 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003357 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003358 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003359 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3360 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003361 }
John McCall54abf7d2009-11-04 02:18:39 +00003362}