blob: 8926d75bb0c4534fbf4c843cca08454f36cf092d [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
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000197//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000198// Attribute Implementations
199//===----------------------------------------------------------------------===//
200
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000201// FIXME: All this manual attribute parsing code is gross. At the
202// least add some helper functions to check most argument patterns (#
203// and types of args).
204
Chandler Carruth87c44602011-07-01 23:49:12 +0000205static void HandleExtVectorTypeAttr(Scope *scope, Decl *D,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000206 const AttributeList &Attr, Sema &S) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000207 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000209 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000210 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000214
215 Expr *sizeExpr;
216
217 // Special case where the argument is a template id.
218 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000219 CXXScopeSpec SS;
220 UnqualifiedId id;
221 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000222
223 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
224 if (Size.isInvalid())
225 return;
226
227 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000228 } else {
229 // check the attribute arguments.
230 if (Attr.getNumArgs() != 1) {
231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
232 return;
233 }
Peter Collingbourne7a730022010-11-23 20:45:58 +0000234 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000236
237 // Instantiate/Install the vector type, and let Sema build the type for us.
238 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000239 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000240 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000241 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000242 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000243
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000244 // Remember this typedef decl, we will need it later for diagnostics.
245 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000246 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000247}
248
Chandler Carruth87c44602011-07-01 23:49:12 +0000249static void HandlePackedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000250 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000251 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000252 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000253 return;
254 }
Mike Stumpbf916502009-07-24 19:02:52 +0000255
Chandler Carruth87c44602011-07-01 23:49:12 +0000256 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Sean Huntcf807c42010-08-18 23:23:40 +0000257 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000258 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000259 // If the alignment is less than or equal to 8 bits, the packed attribute
260 // has no effect.
261 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000262 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000263 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000264 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000265 else
Sean Huntcf807c42010-08-18 23:23:40 +0000266 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000267 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000268 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000269}
270
Chandler Carruth87c44602011-07-01 23:49:12 +0000271static void HandleMsStructAttr(Decl *D, const AttributeList &Attr, Sema &S) {
272 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000273 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
274 else
275 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
276}
277
Chandler Carruth87c44602011-07-01 23:49:12 +0000278static void HandleIBAction(Decl *D, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000279 // check the attribute arguments.
280 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000282 return;
283 }
Mike Stumpbf916502009-07-24 19:02:52 +0000284
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000285 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000286 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000287 if (MD->isInstanceMethod()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000288 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000289 return;
290 }
291
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000292 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000293}
294
Chandler Carruth87c44602011-07-01 23:49:12 +0000295static void HandleIBOutlet(Decl *D, const AttributeList &Attr, Sema &S) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000296 // check the attribute arguments.
297 if (Attr.getNumArgs() > 0) {
298 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
299 return;
300 }
301
302 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000303 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000304 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
305 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000306 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000307 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000308
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000309 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000310}
311
Chandler Carruth87c44602011-07-01 23:49:12 +0000312static void HandleIBOutletCollection(Decl *D, const AttributeList &Attr,
Ted Kremenek857e9182010-05-19 17:38:06 +0000313 Sema &S) {
314
315 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000316 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000317 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
318 return;
319 }
320
321 // The IBOutletCollection attributes only apply to instance variables of
322 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000323 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000324 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000325 return;
326 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000327 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000328 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
329 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
330 << VD->getType() << 0;
331 return;
332 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000333 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000334 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
335 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
336 << PD->getType() << 1;
337 return;
338 }
339
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000340 IdentifierInfo *II = Attr.getParameterName();
341 if (!II)
342 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000343
John McCallb3d87482010-08-24 05:47:05 +0000344 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000345 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000346 if (!TypeRep) {
347 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
348 return;
349 }
John McCallb3d87482010-08-24 05:47:05 +0000350 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000351 // Diagnose use of non-object type in iboutletcollection attribute.
352 // FIXME. Gnu attribute extension ignores use of builtin types in
353 // attributes. So, __attribute__((iboutletcollection(char))) will be
354 // treated as __attribute__((iboutletcollection())).
355 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
356 !QT->isObjCObjectType()) {
357 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
358 return;
359 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000360 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000361 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000362}
363
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000364static void PossibleTransparentUnionPointerType(QualType &T) {
365 if (const RecordType *UT = T->getAsUnionType())
366 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
367 RecordDecl *UD = UT->getDecl();
368 for (RecordDecl::field_iterator it = UD->field_begin(),
369 itend = UD->field_end(); it != itend; ++it) {
370 QualType QT = it->getType();
371 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
372 T = QT;
373 return;
374 }
375 }
376 }
377}
378
Chandler Carruth87c44602011-07-01 23:49:12 +0000379static void HandleNonNullAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000380 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
381 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000382 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000384 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000385 return;
386 }
Mike Stumpbf916502009-07-24 19:02:52 +0000387
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000388 // In C++ the implicit 'this' function parameter also counts, and they are
389 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000390 bool HasImplicitThisParam = isInstanceMethod(D);
391 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000392
393 // The nonnull attribute only applies to pointers.
394 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000395
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000396 for (AttributeList::arg_iterator I=Attr.arg_begin(),
397 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000398
399
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000400 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000401 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000402 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000403 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
404 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
406 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000407 return;
408 }
Mike Stumpbf916502009-07-24 19:02:52 +0000409
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000410 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000411
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000412 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000413 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000414 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000415 return;
416 }
Mike Stumpbf916502009-07-24 19:02:52 +0000417
Ted Kremenek465172f2008-07-21 22:09:15 +0000418 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000419 if (HasImplicitThisParam) {
420 if (x == 0) {
421 S.Diag(Attr.getLoc(),
422 diag::err_attribute_invalid_implicit_this_argument)
423 << "nonnull" << Ex->getSourceRange();
424 return;
425 }
426 --x;
427 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000428
429 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000430 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000431 PossibleTransparentUnionPointerType(T);
432
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000433 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000434 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000435 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000436 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000437 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000438 }
Mike Stumpbf916502009-07-24 19:02:52 +0000439
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000440 NonNullArgs.push_back(x);
441 }
Mike Stumpbf916502009-07-24 19:02:52 +0000442
443 // If no arguments were specified to __attribute__((nonnull)) then all pointer
444 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000445 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000446 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
447 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000448 PossibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000449 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000450 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000451 }
Mike Stumpbf916502009-07-24 19:02:52 +0000452
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000453 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000454 if (NonNullArgs.empty()) {
455 // Warn the trivial case only if attribute is not coming from a
456 // macro instantiation.
457 if (Attr.getLoc().isFileID())
458 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000459 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000460 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000461 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000462
463 unsigned* start = &NonNullArgs[0];
464 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000465 llvm::array_pod_sort(start, start + size);
Chandler Carruth87c44602011-07-01 23:49:12 +0000466 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000467 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000468}
469
Chandler Carruth87c44602011-07-01 23:49:12 +0000470static void HandleOwnershipAttr(Decl *D, const AttributeList &AL, Sema &S) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000471 // This attribute must be applied to a function declaration.
472 // The first argument to the attribute must be a string,
473 // the name of the resource, for example "malloc".
474 // The following arguments must be argument indexes, the arguments must be
475 // of integer type for Returns, otherwise of pointer type.
476 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000477 // after being held. free() should be __attribute((ownership_takes)), whereas
478 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000479
480 if (!AL.getParameterName()) {
481 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
482 << AL.getName()->getName() << 1;
483 return;
484 }
485 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000486 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000487 switch (AL.getKind()) {
488 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000489 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000490 if (AL.getNumArgs() < 1) {
491 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
492 return;
493 }
Jordy Rose2a479922010-08-12 08:54:03 +0000494 break;
495 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000496 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000497 if (AL.getNumArgs() < 1) {
498 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
499 return;
500 }
Jordy Rose2a479922010-08-12 08:54:03 +0000501 break;
502 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000503 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000504 if (AL.getNumArgs() > 1) {
505 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
506 << AL.getNumArgs() + 1;
507 return;
508 }
Jordy Rose2a479922010-08-12 08:54:03 +0000509 break;
510 default:
511 // This should never happen given how we are called.
512 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000513 }
514
Chandler Carruth87c44602011-07-01 23:49:12 +0000515 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000516 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
517 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000518 return;
519 }
520
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000521 // In C++ the implicit 'this' function parameter also counts, and they are
522 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000523 bool HasImplicitThisParam = isInstanceMethod(D);
524 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000525
526 llvm::StringRef Module = AL.getParameterName()->getName();
527
528 // Normalize the argument, __foo__ becomes foo.
529 if (Module.startswith("__") && Module.endswith("__"))
530 Module = Module.substr(2, Module.size() - 4);
531
532 llvm::SmallVector<unsigned, 10> OwnershipArgs;
533
Jordy Rose2a479922010-08-12 08:54:03 +0000534 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
535 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000536
Peter Collingbourne7a730022010-11-23 20:45:58 +0000537 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000538 llvm::APSInt ArgNum(32);
539 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
540 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
541 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
542 << AL.getName()->getName() << IdxExpr->getSourceRange();
543 continue;
544 }
545
546 unsigned x = (unsigned) ArgNum.getZExtValue();
547
548 if (x > NumArgs || x < 1) {
549 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
550 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
551 continue;
552 }
553 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000554 if (HasImplicitThisParam) {
555 if (x == 0) {
556 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
557 << "ownership" << IdxExpr->getSourceRange();
558 return;
559 }
560 --x;
561 }
562
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000563 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000564 case OwnershipAttr::Takes:
565 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000566 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000567 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000568 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
569 // FIXME: Should also highlight argument in decl.
570 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000571 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000572 << "pointer"
573 << IdxExpr->getSourceRange();
574 continue;
575 }
576 break;
577 }
Sean Huntcf807c42010-08-18 23:23:40 +0000578 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000579 if (AL.getNumArgs() > 1) {
580 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000581 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000582 llvm::APSInt ArgNum(32);
583 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
584 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
585 S.Diag(AL.getLoc(), diag::err_ownership_type)
586 << "ownership_returns" << "integer"
587 << IdxExpr->getSourceRange();
588 return;
589 }
590 }
591 break;
592 }
Jordy Rose2a479922010-08-12 08:54:03 +0000593 default:
594 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000595 } // switch
596
597 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000598 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +0000599 i = D->specific_attr_begin<OwnershipAttr>(),
600 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +0000601 i != e; ++i) {
602 if ((*i)->getOwnKind() != K) {
603 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
604 I!=E; ++I) {
605 if (x == *I) {
606 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
607 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000608 }
609 }
610 }
611 }
612 OwnershipArgs.push_back(x);
613 }
614
615 unsigned* start = OwnershipArgs.data();
616 unsigned size = OwnershipArgs.size();
617 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000618
619 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
620 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
621 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000622 }
Sean Huntcf807c42010-08-18 23:23:40 +0000623
Chandler Carruth87c44602011-07-01 23:49:12 +0000624 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +0000625 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000626}
627
John McCall332bb2a2011-02-08 22:35:49 +0000628/// Whether this declaration has internal linkage for the purposes of
629/// things that want to complain about things not have internal linkage.
630static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
631 switch (D->getLinkage()) {
632 case NoLinkage:
633 case InternalLinkage:
634 return true;
635
636 // Template instantiations that go from external to unique-external
637 // shouldn't get diagnosed.
638 case UniqueExternalLinkage:
639 return true;
640
641 case ExternalLinkage:
642 return false;
643 }
644 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000645 return false;
646}
647
Chandler Carruth87c44602011-07-01 23:49:12 +0000648static void HandleWeakRefAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000649 // Check the attribute arguments.
650 if (Attr.getNumArgs() > 1) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
652 return;
653 }
654
Chandler Carruth87c44602011-07-01 23:49:12 +0000655 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000657 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +0000658 return;
659 }
660
Chandler Carruth87c44602011-07-01 23:49:12 +0000661 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +0000662
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000663 // gcc rejects
664 // class c {
665 // static int a __attribute__((weakref ("v2")));
666 // static int b() __attribute__((weakref ("f3")));
667 // };
668 // and ignores the attributes of
669 // void f(void) {
670 // static int a __attribute__((weakref ("v2")));
671 // }
672 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +0000673 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000674 if (!Ctx->isFileContext()) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +0000676 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000677 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000678 }
679
680 // The GCC manual says
681 //
682 // At present, a declaration to which `weakref' is attached can only
683 // be `static'.
684 //
685 // It also says
686 //
687 // Without a TARGET,
688 // given as an argument to `weakref' or to `alias', `weakref' is
689 // equivalent to `weak'.
690 //
691 // gcc 4.4.1 will accept
692 // int a7 __attribute__((weakref));
693 // as
694 // int a7 __attribute__((weak));
695 // This looks like a bug in gcc. We reject that for now. We should revisit
696 // it if this behaviour is actually used.
697
John McCall332bb2a2011-02-08 22:35:49 +0000698 if (!hasEffectivelyInternalLinkage(nd)) {
699 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000700 return;
701 }
702
703 // GCC rejects
704 // static ((alias ("y"), weakref)).
705 // Should we? How to check that weakref is before or after alias?
706
707 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000708 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000709 Arg = Arg->IgnoreParenCasts();
710 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
711
712 if (Str == 0 || Str->isWide()) {
713 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
714 << "weakref" << 1;
715 return;
716 }
717 // GCC will accept anything as the argument of weakref. Should we
718 // check for an existing decl?
Chandler Carruth87c44602011-07-01 23:49:12 +0000719 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000720 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000721 }
722
Chandler Carruth87c44602011-07-01 23:49:12 +0000723 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000724}
725
Chandler Carruth87c44602011-07-01 23:49:12 +0000726static void HandleAliasAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000727 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000728 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000729 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000730 return;
731 }
Mike Stumpbf916502009-07-24 19:02:52 +0000732
Peter Collingbourne7a730022010-11-23 20:45:58 +0000733 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000734 Arg = Arg->IgnoreParenCasts();
735 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000736
Chris Lattner6b6b5372008-06-26 18:38:35 +0000737 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000738 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000739 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000740 return;
741 }
Mike Stumpbf916502009-07-24 19:02:52 +0000742
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000743 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +0000744 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
745 return;
746 }
747
Chris Lattner6b6b5372008-06-26 18:38:35 +0000748 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000749
Chandler Carruth87c44602011-07-01 23:49:12 +0000750 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000751 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000752}
753
Chandler Carruth87c44602011-07-01 23:49:12 +0000754static void HandleNakedAttr(Decl *D, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000755 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000756 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000757 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000759 return;
760 }
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 Carruth87c44602011-07-01 23:49:12 +0000771static void HandleAlwaysInlineAttr(Decl *D, const AttributeList &Attr,
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000772 Sema &S) {
773 // 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 Carruth87c44602011-07-01 23:49:12 +0000788static void HandleMallocAttr(Decl *D, const AttributeList &Attr, Sema &S) {
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 Carruth87c44602011-07-01 23:49:12 +0000806static void HandleMayAliasAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Dan Gohman34c26302010-11-17 00:03:07 +0000807 // check the attribute arguments.
808 if (Attr.getNumArgs() != 0) {
809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
810 return;
811 }
812
Chandler Carruth87c44602011-07-01 23:49:12 +0000813 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +0000814}
815
Chandler Carruth87c44602011-07-01 23:49:12 +0000816static void HandleNoCommonAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Eric Christophera6cf1e72010-12-02 02:45:55 +0000817 assert(Attr.isInvalid() == false);
Chandler Carruth87c44602011-07-01 23:49:12 +0000818 if (isa<VarDecl>(D))
819 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +0000820 else
821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000822 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000823}
824
Chandler Carruth87c44602011-07-01 23:49:12 +0000825static void HandleCommonAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Eric Christophera6cf1e72010-12-02 02:45:55 +0000826 assert(Attr.isInvalid() == false);
Chandler Carruth87c44602011-07-01 23:49:12 +0000827 if (isa<VarDecl>(D))
828 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +0000829 else
830 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000831 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000832}
833
Chandler Carruth87c44602011-07-01 23:49:12 +0000834static void HandleNoReturnAttr(Decl *D, const AttributeList &attr, Sema &S) {
835 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +0000836
837 if (S.CheckNoReturnAttr(attr)) return;
838
Chandler Carruth87c44602011-07-01 23:49:12 +0000839 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +0000840 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000841 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +0000842 return;
843 }
844
Chandler Carruth87c44602011-07-01 23:49:12 +0000845 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +0000846}
847
848bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +0000849 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +0000850 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
851 attr.setInvalid();
852 return true;
853 }
854
855 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +0000856}
857
Chandler Carruth87c44602011-07-01 23:49:12 +0000858static void HandleAnalyzerNoReturnAttr(Decl *D, const AttributeList &Attr,
Ted Kremenekb7252322009-04-10 00:01:14 +0000859 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000860
861 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
862 // because 'analyzer_noreturn' does not impact the type.
863
864 if (Attr.getNumArgs() != 0) {
865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
866 return;
867 }
868
Chandler Carruth87c44602011-07-01 23:49:12 +0000869 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
870 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000871 if (VD == 0 || (!VD->getType()->isBlockPointerType()
872 && !VD->getType()->isFunctionPointerType())) {
873 S.Diag(Attr.getLoc(),
874 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
875 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000876 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000877 return;
878 }
879 }
880
Chandler Carruth87c44602011-07-01 23:49:12 +0000881 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000882}
883
John Thompson35cc9622010-08-09 21:53:52 +0000884// PS3 PPU-specific.
Chandler Carruth87c44602011-07-01 23:49:12 +0000885static void HandleVecReturnAttr(Decl *D, const AttributeList &Attr,
John Thompson35cc9622010-08-09 21:53:52 +0000886 Sema &S) {
887/*
888 Returning a Vector Class in Registers
889
Eric Christopherf48f3672010-12-01 22:13:54 +0000890 According to the PPU ABI specifications, a class with a single member of
891 vector type is returned in memory when used as the return value of a function.
892 This results in inefficient code when implementing vector classes. To return
893 the value in a single vector register, add the vecreturn attribute to the
894 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000895
896 Example:
897
898 struct Vector
899 {
900 __vector float xyzw;
901 } __attribute__((vecreturn));
902
903 Vector Add(Vector lhs, Vector rhs)
904 {
905 Vector result;
906 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
907 return result; // This will be returned in a register
908 }
909*/
Chandler Carruth87c44602011-07-01 23:49:12 +0000910 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +0000911 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000912 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +0000913 return;
914 }
915
Chandler Carruth87c44602011-07-01 23:49:12 +0000916 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +0000917 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
918 return;
919 }
920
Chandler Carruth87c44602011-07-01 23:49:12 +0000921 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +0000922 int count = 0;
923
924 if (!isa<CXXRecordDecl>(record)) {
925 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
926 return;
927 }
928
929 if (!cast<CXXRecordDecl>(record)->isPOD()) {
930 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
931 return;
932 }
933
Eric Christopherf48f3672010-12-01 22:13:54 +0000934 for (RecordDecl::field_iterator iter = record->field_begin();
935 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +0000936 if ((count == 1) || !iter->getType()->isVectorType()) {
937 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
938 return;
939 }
940 count++;
941 }
942
Chandler Carruth87c44602011-07-01 23:49:12 +0000943 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000944}
945
Chandler Carruth87c44602011-07-01 23:49:12 +0000946static void HandleDependencyAttr(Decl *D, const AttributeList &Attr, Sema &S) {
947 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000948 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000949 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +0000950 return;
951 }
952 // FIXME: Actually store the attribute on the declaration
953}
954
Chandler Carruth87c44602011-07-01 23:49:12 +0000955static void HandleUnusedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Ted Kremenek73798892008-07-25 04:39:19 +0000956 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000957 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000959 return;
960 }
Mike Stumpbf916502009-07-24 19:02:52 +0000961
Chandler Carruth87c44602011-07-01 23:49:12 +0000962 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
963 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000965 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +0000966 return;
967 }
Mike Stumpbf916502009-07-24 19:02:52 +0000968
Chandler Carruth87c44602011-07-01 23:49:12 +0000969 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000970}
971
Chandler Carruth87c44602011-07-01 23:49:12 +0000972static void HandleUsedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000973 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000974 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
976 return;
977 }
Mike Stumpbf916502009-07-24 19:02:52 +0000978
Chandler Carruth87c44602011-07-01 23:49:12 +0000979 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000980 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000981 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
982 return;
983 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000984 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000985 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000986 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000987 return;
988 }
Mike Stumpbf916502009-07-24 19:02:52 +0000989
Chandler Carruth87c44602011-07-01 23:49:12 +0000990 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000991}
992
Chandler Carruth87c44602011-07-01 23:49:12 +0000993static void HandleConstructorAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000994 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +0000995 if (Attr.getNumArgs() > 1) {
996 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000997 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000998 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000999
1000 int priority = 65535; // FIXME: Do not hardcode such constants.
1001 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001002 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001003 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001004 if (E->isTypeDependent() || E->isValueDependent() ||
1005 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001007 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001008 return;
1009 }
1010 priority = Idx.getZExtValue();
1011 }
Mike Stumpbf916502009-07-24 19:02:52 +00001012
Chandler Carruth87c44602011-07-01 23:49:12 +00001013 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001015 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001016 return;
1017 }
1018
Chandler Carruth87c44602011-07-01 23:49:12 +00001019 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001020 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001021}
1022
Chandler Carruth87c44602011-07-01 23:49:12 +00001023static void HandleDestructorAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001024 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001025 if (Attr.getNumArgs() > 1) {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001027 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001028 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001029
1030 int priority = 65535; // FIXME: Do not hardcode such constants.
1031 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001032 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001033 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001034 if (E->isTypeDependent() || E->isValueDependent() ||
1035 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001036 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001037 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001038 return;
1039 }
1040 priority = Idx.getZExtValue();
1041 }
Mike Stumpbf916502009-07-24 19:02:52 +00001042
Chandler Carruth87c44602011-07-01 23:49:12 +00001043 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001044 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001045 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001046 return;
1047 }
1048
Chandler Carruth87c44602011-07-01 23:49:12 +00001049 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001050 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001051}
1052
Chandler Carruth87c44602011-07-01 23:49:12 +00001053static void HandleDeprecatedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001054 unsigned NumArgs = Attr.getNumArgs();
1055 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001056 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001057 return;
1058 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001059
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001060 // Handle the case where deprecated attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001061 llvm::StringRef Str;
1062 if (NumArgs == 1) {
1063 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001064 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001065 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1066 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001067 return;
1068 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001069 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001070 }
Mike Stumpbf916502009-07-24 19:02:52 +00001071
Chandler Carruth87c44602011-07-01 23:49:12 +00001072 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001073}
1074
Chandler Carruth87c44602011-07-01 23:49:12 +00001075static void HandleUnavailableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001076 unsigned NumArgs = Attr.getNumArgs();
1077 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001078 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001079 return;
1080 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001081
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001082 // Handle the case where unavailable attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001083 llvm::StringRef Str;
1084 if (NumArgs == 1) {
1085 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001086 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001087 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001088 diag::err_attribute_not_string) << "unavailable";
1089 return;
1090 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001091 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001092 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001093 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001094}
1095
Chandler Carruth87c44602011-07-01 23:49:12 +00001096static void HandleAvailabilityAttr(Decl *D, const AttributeList &Attr,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001097 Sema &S) {
1098 IdentifierInfo *Platform = Attr.getParameterName();
1099 SourceLocation PlatformLoc = Attr.getParameterLoc();
1100
1101 llvm::StringRef PlatformName
1102 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1103 if (PlatformName.empty()) {
1104 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1105 << Platform;
1106
1107 PlatformName = Platform->getName();
1108 }
1109
1110 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1111 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1112 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001113 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001114
1115 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1116 // of these steps are needed).
1117 if (Introduced.isValid() && Deprecated.isValid() &&
1118 !(Introduced.Version < Deprecated.Version)) {
1119 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1120 << 1 << PlatformName << Deprecated.Version.getAsString()
1121 << 0 << Introduced.Version.getAsString();
1122 return;
1123 }
1124
1125 if (Introduced.isValid() && Obsoleted.isValid() &&
1126 !(Introduced.Version < Obsoleted.Version)) {
1127 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1128 << 2 << PlatformName << Obsoleted.Version.getAsString()
1129 << 0 << Introduced.Version.getAsString();
1130 return;
1131 }
1132
1133 if (Deprecated.isValid() && Obsoleted.isValid() &&
1134 !(Deprecated.Version < Obsoleted.Version)) {
1135 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1136 << 2 << PlatformName << Obsoleted.Version.getAsString()
1137 << 1 << Deprecated.Version.getAsString();
1138 return;
1139 }
1140
Chandler Carruth87c44602011-07-01 23:49:12 +00001141 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001142 Platform,
1143 Introduced.Version,
1144 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001145 Obsoleted.Version,
1146 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001147}
1148
Chandler Carruth87c44602011-07-01 23:49:12 +00001149static void HandleVisibilityAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001150 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001151 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001152 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001153 return;
1154 }
Mike Stumpbf916502009-07-24 19:02:52 +00001155
Peter Collingbourne7a730022010-11-23 20:45:58 +00001156 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001157 Arg = Arg->IgnoreParenCasts();
1158 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001159
Chris Lattner6b6b5372008-06-26 18:38:35 +00001160 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001162 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001163 return;
1164 }
Mike Stumpbf916502009-07-24 19:02:52 +00001165
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001166 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001167 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001168
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001169 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001170 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001171 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001172 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001173 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001174 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001175 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001176 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001177 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001178 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001179 return;
1180 }
Mike Stumpbf916502009-07-24 19:02:52 +00001181
Chandler Carruth87c44602011-07-01 23:49:12 +00001182 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001183}
1184
Chandler Carruth87c44602011-07-01 23:49:12 +00001185static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &Attr,
John McCalld5313b02011-03-02 11:33:24 +00001186 Sema &S) {
1187 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1188 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001190 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001191 return;
1192 }
1193
Chandler Carruth87c44602011-07-01 23:49:12 +00001194 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1195 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1196 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001197 << "objc_method_family" << 1;
1198 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001200 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001201 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001202 return;
1203 }
1204
Chandler Carruth87c44602011-07-01 23:49:12 +00001205 llvm::StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001206 ObjCMethodFamilyAttr::FamilyKind family;
1207 if (param == "none")
1208 family = ObjCMethodFamilyAttr::OMF_None;
1209 else if (param == "alloc")
1210 family = ObjCMethodFamilyAttr::OMF_alloc;
1211 else if (param == "copy")
1212 family = ObjCMethodFamilyAttr::OMF_copy;
1213 else if (param == "init")
1214 family = ObjCMethodFamilyAttr::OMF_init;
1215 else if (param == "mutableCopy")
1216 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1217 else if (param == "new")
1218 family = ObjCMethodFamilyAttr::OMF_new;
1219 else {
1220 // Just warn and ignore it. This is future-proof against new
1221 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001222 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001223 return;
1224 }
1225
John McCallf85e1932011-06-15 23:02:42 +00001226 if (family == ObjCMethodFamilyAttr::OMF_init &&
1227 !method->getResultType()->isObjCObjectPointerType()) {
1228 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1229 << method->getResultType();
1230 // Ignore the attribute.
1231 return;
1232 }
1233
Chandler Carruth87c44602011-07-01 23:49:12 +00001234 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCallf85e1932011-06-15 23:02:42 +00001235 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001236}
1237
Chris Lattner0db29ec2009-02-14 08:09:34 +00001238static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1239 Sema &S) {
1240 if (Attr.getNumArgs() != 0) {
1241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1242 return;
1243 }
Mike Stumpbf916502009-07-24 19:02:52 +00001244
Chris Lattner0db29ec2009-02-14 08:09:34 +00001245 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1246 if (OCI == 0) {
1247 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1248 return;
1249 }
Mike Stumpbf916502009-07-24 19:02:52 +00001250
Sean Huntcf807c42010-08-18 23:23:40 +00001251 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001252}
1253
1254static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001255 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001256 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001257 return;
1258 }
Richard Smith162e1c12011-04-15 14:24:37 +00001259 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001260 QualType T = TD->getUnderlyingType();
1261 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001262 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001263 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1264 return;
1265 }
1266 }
Sean Huntcf807c42010-08-18 23:23:40 +00001267 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001268}
1269
Mike Stumpbf916502009-07-24 19:02:52 +00001270static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001271HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1272 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001274 return;
1275 }
1276
1277 if (!isa<FunctionDecl>(D)) {
1278 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1279 return;
1280 }
1281
Sean Huntcf807c42010-08-18 23:23:40 +00001282 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001283}
1284
Chandler Carruth87c44602011-07-01 23:49:12 +00001285static void HandleBlocksAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001286 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001287 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001288 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001289 return;
1290 }
Mike Stumpbf916502009-07-24 19:02:52 +00001291
Steve Naroff9eae5762008-09-18 16:44:58 +00001292 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001293 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001294 return;
1295 }
Mike Stumpbf916502009-07-24 19:02:52 +00001296
Sean Huntcf807c42010-08-18 23:23:40 +00001297 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001298 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001299 type = BlocksAttr::ByRef;
1300 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001301 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001302 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001303 return;
1304 }
Mike Stumpbf916502009-07-24 19:02:52 +00001305
Chandler Carruth87c44602011-07-01 23:49:12 +00001306 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001307}
1308
Chandler Carruth87c44602011-07-01 23:49:12 +00001309static void HandleSentinelAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Anders Carlsson77091822008-10-05 18:05:59 +00001310 // check the attribute arguments.
1311 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001312 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001313 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001314 }
1315
Anders Carlsson77091822008-10-05 18:05:59 +00001316 int sentinel = 0;
1317 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001318 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001319 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001320 if (E->isTypeDependent() || E->isValueDependent() ||
1321 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001323 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001324 return;
1325 }
1326 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001327
Anders Carlsson77091822008-10-05 18:05:59 +00001328 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001329 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1330 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001331 return;
1332 }
1333 }
1334
1335 int nullPos = 0;
1336 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001337 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001338 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001339 if (E->isTypeDependent() || E->isValueDependent() ||
1340 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001342 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001343 return;
1344 }
1345 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001346
Anders Carlsson77091822008-10-05 18:05:59 +00001347 if (nullPos > 1 || nullPos < 0) {
1348 // FIXME: This error message could be improved, it would be nice
1349 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001350 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1351 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001352 return;
1353 }
1354 }
1355
Chandler Carruth87c44602011-07-01 23:49:12 +00001356 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall183700f2009-09-21 23:43:11 +00001357 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001358 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001359
Chris Lattner897cd902009-03-17 23:03:47 +00001360 if (isa<FunctionNoProtoType>(FT)) {
1361 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1362 return;
1363 }
Mike Stumpbf916502009-07-24 19:02:52 +00001364
Chris Lattner897cd902009-03-17 23:03:47 +00001365 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001366 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001367 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001368 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001369 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001370 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001371 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001372 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001373 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001374 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001375 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1376 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001377 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001378 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001379 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001380 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001381 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001382 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001383 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001384 int m = Ty->isFunctionPointerType() ? 0 : 1;
1385 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001386 return;
1387 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001388 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001389 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001390 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001391 return;
1392 }
Anders Carlsson77091822008-10-05 18:05:59 +00001393 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001394 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001395 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001396 return;
1397 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001398 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001399 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001400}
1401
Chris Lattner026dc962009-02-14 07:37:35 +00001402static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1403 // check the attribute arguments.
1404 if (Attr.getNumArgs() != 0) {
1405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1406 return;
1407 }
1408
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001409 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001411 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001412 return;
1413 }
Mike Stumpbf916502009-07-24 19:02:52 +00001414
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001415 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1416 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1417 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001418 return;
1419 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001420 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1421 if (MD->getResultType()->isVoidType()) {
1422 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1423 << Attr.getName() << 1;
1424 return;
1425 }
1426
Sean Huntcf807c42010-08-18 23:23:40 +00001427 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001428}
1429
Chandler Carruth87c44602011-07-01 23:49:12 +00001430static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001431 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001432 if (Attr.hasParameterOrArguments()) {
1433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001434 return;
1435 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001436
Chandler Carruth87c44602011-07-01 23:49:12 +00001437 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1439 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001440 return;
1441 }
1442
Chandler Carruth87c44602011-07-01 23:49:12 +00001443 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001444
1445 // 'weak' only applies to declarations with external linkage.
1446 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001447 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001448 return;
1449 }
Mike Stumpbf916502009-07-24 19:02:52 +00001450
Chandler Carruth87c44602011-07-01 23:49:12 +00001451 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001452}
1453
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001454static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1455 // check the attribute arguments.
1456 if (Attr.getNumArgs() != 0) {
1457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1458 return;
Mike Stumpbf916502009-07-24 19:02:52 +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
Nate Begeman6f3d8382009-06-26 06:32:41 +00001482static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1483 Sema &S) {
1484 // Attribute has 3 arguments.
1485 if (Attr.getNumArgs() != 3) {
Chandler Carrutha8581b92011-06-30 08:14:54 +00001486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001487 return;
1488 }
1489
1490 unsigned WGSize[3];
1491 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001492 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001493 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001494 if (E->isTypeDependent() || E->isValueDependent() ||
1495 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001496 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1497 << "reqd_work_group_size" << E->getSourceRange();
1498 return;
1499 }
1500 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1501 }
Sean Huntcf807c42010-08-18 23:23:40 +00001502 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1503 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001504 WGSize[2]));
1505}
1506
Chris Lattner026dc962009-02-14 07:37:35 +00001507static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001508 // Attribute has no arguments.
1509 if (Attr.getNumArgs() != 1) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1511 return;
1512 }
1513
1514 // Make sure that there is a string literal as the sections's single
1515 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001516 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001517 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001518 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001519 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001520 return;
1521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Chris Lattner797c3c42009-08-10 19:03:04 +00001523 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001524 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001525 if (!Error.empty()) {
1526 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1527 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001528 return;
1529 }
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001531 // This attribute cannot be applied to local variables.
1532 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1533 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1534 return;
1535 }
1536
Eric Christopherf48f3672010-12-01 22:13:54 +00001537 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1538 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001539}
1540
Chris Lattner6b6b5372008-06-26 18:38:35 +00001541
Chandler Carruth87c44602011-07-01 23:49:12 +00001542static void HandleNothrowAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001543 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001544 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001546 return;
1547 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001548
Chandler Carruth87c44602011-07-01 23:49:12 +00001549 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001550 if (Existing->getLocation().isInvalid())
1551 Existing->setLocation(Attr.getLoc());
1552 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001553 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001554 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001555}
1556
Chandler Carruth87c44602011-07-01 23:49:12 +00001557static void HandleConstAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001558 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001559 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001560 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001561 return;
1562 }
Mike Stumpbf916502009-07-24 19:02:52 +00001563
Chandler Carruth87c44602011-07-01 23:49:12 +00001564 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001565 if (Existing->getLocation().isInvalid())
1566 Existing->setLocation(Attr.getLoc());
1567 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001568 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001569 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001570}
1571
Chandler Carruth87c44602011-07-01 23:49:12 +00001572static void HandlePureAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001573 // check the attribute arguments.
1574 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001575 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001576 return;
1577 }
Mike Stumpbf916502009-07-24 19:02:52 +00001578
Chandler Carruth87c44602011-07-01 23:49:12 +00001579 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001580}
1581
Chandler Carruth87c44602011-07-01 23:49:12 +00001582static void HandleCleanupAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001583 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001584 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1585 return;
1586 }
Mike Stumpbf916502009-07-24 19:02:52 +00001587
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001588 if (Attr.getNumArgs() != 0) {
1589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1590 return;
1591 }
Mike Stumpbf916502009-07-24 19:02:52 +00001592
Chandler Carruth87c44602011-07-01 23:49:12 +00001593 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00001594
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001595 if (!VD || !VD->hasLocalStorage()) {
1596 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1597 return;
1598 }
Mike Stumpbf916502009-07-24 19:02:52 +00001599
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001600 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001601 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001602 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001603 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1604 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001605 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001606 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001607 Attr.getParameterName();
1608 return;
1609 }
Mike Stumpbf916502009-07-24 19:02:52 +00001610
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001611 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1612 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001613 S.Diag(Attr.getParameterLoc(),
1614 diag::err_attribute_cleanup_arg_not_function)
1615 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001616 return;
1617 }
1618
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001619 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001620 S.Diag(Attr.getParameterLoc(),
1621 diag::err_attribute_cleanup_func_must_take_one_arg)
1622 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001623 return;
1624 }
Mike Stumpbf916502009-07-24 19:02:52 +00001625
Anders Carlsson89941c12009-02-07 23:16:50 +00001626 // We're currently more strict than GCC about what function types we accept.
1627 // If this ever proves to be a problem it should be easy to fix.
1628 QualType Ty = S.Context.getPointerType(VD->getType());
1629 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001630 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1631 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001632 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001633 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1634 Attr.getParameterName() << ParamTy << Ty;
1635 return;
1636 }
Mike Stumpbf916502009-07-24 19:02:52 +00001637
Chandler Carruth87c44602011-07-01 23:49:12 +00001638 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001639 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001640}
1641
Mike Stumpbf916502009-07-24 19:02:52 +00001642/// Handle __attribute__((format_arg((idx)))) attribute based on
1643/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth87c44602011-07-01 23:49:12 +00001644static void HandleFormatArgAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001645 if (Attr.getNumArgs() != 1) {
1646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1647 return;
1648 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001649 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001650 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001651 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001652 return;
1653 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001654
1655 // In C++ the implicit 'this' function parameter also counts, and they are
1656 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001657 bool HasImplicitThisParam = isInstanceMethod(D);
1658 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001659 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001660
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001661 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001662 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001663 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001664 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1665 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1667 << "format" << 2 << IdxExpr->getSourceRange();
1668 return;
1669 }
Mike Stumpbf916502009-07-24 19:02:52 +00001670
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001671 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1672 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1673 << "format" << 2 << IdxExpr->getSourceRange();
1674 return;
1675 }
Mike Stumpbf916502009-07-24 19:02:52 +00001676
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001677 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001678
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001679 if (HasImplicitThisParam) {
1680 if (ArgIdx == 0) {
1681 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1682 << "format_arg" << IdxExpr->getSourceRange();
1683 return;
1684 }
1685 ArgIdx--;
1686 }
1687
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001688 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001689 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001690
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001691 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1692 if (not_nsstring_type &&
1693 !isCFStringType(Ty, S.Context) &&
1694 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001695 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001696 // FIXME: Should highlight the actual expression that has the wrong type.
1697 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001698 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001699 << IdxExpr->getSourceRange();
1700 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001701 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001702 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001703 if (!isNSStringType(Ty, S.Context) &&
1704 !isCFStringType(Ty, S.Context) &&
1705 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001706 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001707 // FIXME: Should highlight the actual expression that has the wrong type.
1708 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001709 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001710 << IdxExpr->getSourceRange();
1711 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001712 }
1713
Chandler Carruth87c44602011-07-01 23:49:12 +00001714 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001715 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001716}
1717
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001718enum FormatAttrKind {
1719 CFStringFormat,
1720 NSStringFormat,
1721 StrftimeFormat,
1722 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001723 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001724 InvalidFormat
1725};
1726
1727/// getFormatAttrKind - Map from format attribute names to supported format
1728/// types.
1729static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1730 // Check for formats that get handled specially.
1731 if (Format == "NSString")
1732 return NSStringFormat;
1733 if (Format == "CFString")
1734 return CFStringFormat;
1735 if (Format == "strftime")
1736 return StrftimeFormat;
1737
1738 // Otherwise, check for supported formats.
1739 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1740 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1741 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001742 Format == "zcmn_err" ||
1743 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001744 return SupportedFormat;
1745
Duncan Sandsbc525952010-03-23 14:44:19 +00001746 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1747 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001748 return IgnoredFormat;
1749
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001750 return InvalidFormat;
1751}
1752
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001753/// Handle __attribute__((init_priority(priority))) attributes based on
1754/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth87c44602011-07-01 23:49:12 +00001755static void HandleInitPriorityAttr(Decl *D, const AttributeList &Attr,
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001756 Sema &S) {
1757 if (!S.getLangOptions().CPlusPlus) {
1758 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1759 return;
1760 }
1761
Chandler Carruth87c44602011-07-01 23:49:12 +00001762 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001763 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1764 Attr.setInvalid();
1765 return;
1766 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001767 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001768 if (S.Context.getAsArrayType(T))
1769 T = S.Context.getBaseElementType(T);
1770 if (!T->getAs<RecordType>()) {
1771 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1772 Attr.setInvalid();
1773 return;
1774 }
1775
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001776 if (Attr.getNumArgs() != 1) {
1777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1778 Attr.setInvalid();
1779 return;
1780 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001781 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001782
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001783 llvm::APSInt priority(32);
1784 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1785 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1786 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1787 << "init_priority" << priorityExpr->getSourceRange();
1788 Attr.setInvalid();
1789 return;
1790 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001791 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001792 if (prioritynum < 101 || prioritynum > 65535) {
1793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1794 << priorityExpr->getSourceRange();
1795 Attr.setInvalid();
1796 return;
1797 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001798 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001799 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001800}
1801
Mike Stumpbf916502009-07-24 19:02:52 +00001802/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1803/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth87c44602011-07-01 23:49:12 +00001804static void HandleFormatAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001805
Chris Lattner545dd342008-06-28 23:36:30 +00001806 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001807 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001808 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001809 return;
1810 }
1811
Chris Lattner545dd342008-06-28 23:36:30 +00001812 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001814 return;
1815 }
1816
Chandler Carruth87c44602011-07-01 23:49:12 +00001817 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001818 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001819 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001820 return;
1821 }
1822
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001823 // In C++ the implicit 'this' function parameter also counts, and they are
1824 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001825 bool HasImplicitThisParam = isInstanceMethod(D);
1826 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001827 unsigned FirstIdx = 1;
1828
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001829 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001830
1831 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001832 if (Format.startswith("__") && Format.endswith("__"))
1833 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001834
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001835 // Check for supported formats.
1836 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001837
1838 if (Kind == IgnoredFormat)
1839 return;
1840
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001841 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001842 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001843 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001844 return;
1845 }
1846
1847 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001848 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001849 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001850 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1851 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001852 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001853 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001854 return;
1855 }
1856
1857 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001858 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001859 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001860 return;
1861 }
1862
1863 // FIXME: Do we need to bounds check?
1864 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001865
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001866 if (HasImplicitThisParam) {
1867 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001868 S.Diag(Attr.getLoc(),
1869 diag::err_format_attribute_implicit_this_format_string)
1870 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001871 return;
1872 }
1873 ArgIdx--;
1874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Chris Lattner6b6b5372008-06-26 18:38:35 +00001876 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001877 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001878
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001879 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001880 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001881 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1882 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001883 return;
1884 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001885 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001886 // FIXME: do we need to check if the type is NSString*? What are the
1887 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001888 if (!isNSStringType(Ty, S.Context)) {
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 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001892 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001893 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001894 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001895 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001896 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001897 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1898 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001899 return;
1900 }
1901
1902 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001903 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001904 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001905 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1906 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001908 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001909 return;
1910 }
1911
1912 // check if the function is variadic if the 3rd argument non-zero
1913 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001914 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001915 ++NumArgs; // +1 for ...
1916 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001917 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001918 return;
1919 }
1920 }
1921
Chris Lattner3c73c412008-11-19 08:23:25 +00001922 // strftime requires FirstArg to be 0 because it doesn't read from any
1923 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001924 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001925 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001926 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1927 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001928 return;
1929 }
1930 // if 0 it disables parameter checking (to use with e.g. va_list)
1931 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001933 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001934 return;
1935 }
1936
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001937 // Check whether we already have an equivalent format attribute.
1938 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001939 i = D->specific_attr_begin<FormatAttr>(),
1940 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001941 i != e ; ++i) {
1942 FormatAttr *f = *i;
1943 if (f->getType() == Format &&
1944 f->getFormatIdx() == (int)Idx.getZExtValue() &&
1945 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
1946 // If we don't have a valid location for this attribute, adopt the
1947 // location.
1948 if (f->getLocation().isInvalid())
1949 f->setLocation(Attr.getLoc());
1950 return;
1951 }
1952 }
1953
Chandler Carruth87c44602011-07-01 23:49:12 +00001954 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00001955 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001956 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001957}
1958
Chandler Carruth87c44602011-07-01 23:49:12 +00001959static void HandleTransparentUnionAttr(Decl *D, const AttributeList &Attr,
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001960 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001961 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001962 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001963 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001964 return;
1965 }
1966
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001967 // Try to find the underlying union declaration.
1968 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00001969 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001970 if (TD && TD->getUnderlyingType()->isUnionType())
1971 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1972 else
Chandler Carruth87c44602011-07-01 23:49:12 +00001973 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001974
1975 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001977 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001978 return;
1979 }
1980
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001981 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001982 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001983 diag::warn_transparent_union_attribute_not_definition);
1984 return;
1985 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001986
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001987 RecordDecl::field_iterator Field = RD->field_begin(),
1988 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001989 if (Field == FieldEnd) {
1990 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1991 return;
1992 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001993
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001994 FieldDecl *FirstField = *Field;
1995 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001996 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001997 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001998 diag::warn_transparent_union_attribute_floating)
1999 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002000 return;
2001 }
2002
2003 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2004 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2005 for (; Field != FieldEnd; ++Field) {
2006 QualType FieldType = Field->getType();
2007 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2008 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2009 // Warn if we drop the attribute.
2010 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002011 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002012 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002013 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002014 diag::warn_transparent_union_attribute_field_size_align)
2015 << isSize << Field->getDeclName() << FieldBits;
2016 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002017 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002018 diag::note_transparent_union_first_field_size_align)
2019 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002020 return;
2021 }
2022 }
2023
Sean Huntcf807c42010-08-18 23:23:40 +00002024 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002025}
2026
Chandler Carruth87c44602011-07-01 23:49:12 +00002027static void HandleAnnotateAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002028 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002029 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002030 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002031 return;
2032 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002033 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002034 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002035
Chris Lattner6b6b5372008-06-26 18:38:35 +00002036 // Make sure that there is a string literal as the annotation's single
2037 // argument.
2038 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002039 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002040 return;
2041 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002042 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002043 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002044}
2045
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002046static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002047 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002048 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002049 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002050 return;
2051 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002052
2053 //FIXME: The C++0x version of this attribute has more limited applicabilty
2054 // than GNU's, and should error out when it is used to specify a
2055 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002056
Chris Lattner545dd342008-06-28 23:36:30 +00002057 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002058 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002059 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002060 }
Mike Stumpbf916502009-07-24 19:02:52 +00002061
Peter Collingbourne7a730022010-11-23 20:45:58 +00002062 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002063}
2064
2065void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2066 if (E->isTypeDependent() || E->isValueDependent()) {
2067 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002068 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002069 return;
2070 }
2071
Sean Huntcf807c42010-08-18 23:23:40 +00002072 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002073 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002074 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2075 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2076 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002077 return;
2078 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002079 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002080 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2081 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002082 return;
2083 }
2084
Sean Huntcf807c42010-08-18 23:23:40 +00002085 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2086}
2087
2088void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2089 // FIXME: Cache the number on the Attr object if non-dependent?
2090 // FIXME: Perform checking of type validity
2091 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2092 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002093}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002094
Mike Stumpbf916502009-07-24 19:02:52 +00002095/// HandleModeAttr - This attribute modifies the width of a decl with primitive
2096/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002097///
Mike Stumpbf916502009-07-24 19:02:52 +00002098/// Despite what would be logical, the mode attribute is a decl attribute, not a
2099/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2100/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002101static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002102 // This attribute isn't documented, but glibc uses it. It changes
2103 // the width of an int or unsigned int to the specified size.
2104
2105 // Check that there aren't any arguments
2106 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002107 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002108 return;
2109 }
2110
2111 IdentifierInfo *Name = Attr.getParameterName();
2112 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002113 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002114 return;
2115 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002116
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002117 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002118
2119 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002120 if (Str.startswith("__") && Str.endswith("__"))
2121 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002122
2123 unsigned DestWidth = 0;
2124 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002125 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002126 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002127 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002128 switch (Str[0]) {
2129 case 'Q': DestWidth = 8; break;
2130 case 'H': DestWidth = 16; break;
2131 case 'S': DestWidth = 32; break;
2132 case 'D': DestWidth = 64; break;
2133 case 'X': DestWidth = 96; break;
2134 case 'T': DestWidth = 128; break;
2135 }
2136 if (Str[1] == 'F') {
2137 IntegerMode = false;
2138 } else if (Str[1] == 'C') {
2139 IntegerMode = false;
2140 ComplexMode = true;
2141 } else if (Str[1] != 'I') {
2142 DestWidth = 0;
2143 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002144 break;
2145 case 4:
2146 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2147 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002148 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002149 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002150 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002151 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002152 break;
2153 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002154 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002155 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002156 break;
2157 }
2158
2159 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002160 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002161 OldTy = TD->getUnderlyingType();
2162 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2163 OldTy = VD->getType();
2164 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002165 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2166 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002167 return;
2168 }
Eli Friedman73397492009-03-03 06:41:03 +00002169
John McCall183700f2009-09-21 23:43:11 +00002170 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002171 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2172 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002173 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002174 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2175 } else if (ComplexMode) {
2176 if (!OldTy->isComplexType())
2177 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2178 } else {
2179 if (!OldTy->isFloatingType())
2180 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2181 }
2182
Mike Stump390b4cc2009-05-16 07:39:55 +00002183 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2184 // and friends, at least with glibc.
2185 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2186 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002187 // FIXME: Make sure floating-point mappings are accurate
2188 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002189 QualType NewTy;
2190 switch (DestWidth) {
2191 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002192 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002193 return;
2194 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002195 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002196 return;
2197 case 8:
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.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002204 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002205 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002206 break;
2207 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002208 if (!IntegerMode) {
2209 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2210 return;
2211 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002212 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002213 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002214 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002215 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002216 break;
2217 case 32:
2218 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002219 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002220 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002221 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002222 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002223 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002224 break;
2225 case 64:
2226 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002227 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002228 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002229 if (S.Context.Target.getLongWidth() == 64)
2230 NewTy = S.Context.LongTy;
2231 else
2232 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002233 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002234 if (S.Context.Target.getLongWidth() == 64)
2235 NewTy = S.Context.UnsignedLongTy;
2236 else
2237 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002238 break;
Eli Friedman73397492009-03-03 06:41:03 +00002239 case 96:
2240 NewTy = S.Context.LongDoubleTy;
2241 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002242 case 128:
2243 if (!IntegerMode) {
2244 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2245 return;
2246 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002247 if (OldTy->isSignedIntegerType())
2248 NewTy = S.Context.Int128Ty;
2249 else
2250 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002251 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002252 }
2253
Eli Friedman73397492009-03-03 06:41:03 +00002254 if (ComplexMode) {
2255 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002256 }
2257
2258 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002259 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002260 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002261 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002262 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002263 cast<ValueDecl>(D)->setType(NewTy);
2264}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002265
Chandler Carruth87c44602011-07-01 23:49:12 +00002266static void HandleNoDebugAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002267 // check the attribute arguments.
2268 if (Attr.getNumArgs() > 0) {
2269 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2270 return;
2271 }
Anders Carlssone896d982009-02-13 08:11:52 +00002272
Chandler Carruth87c44602011-07-01 23:49:12 +00002273 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002274 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002275 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002276 return;
2277 }
Mike Stumpbf916502009-07-24 19:02:52 +00002278
Chandler Carruth87c44602011-07-01 23:49:12 +00002279 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002280}
2281
Chandler Carruth87c44602011-07-01 23:49:12 +00002282static void HandleNoInlineAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002283 // check the attribute arguments.
2284 if (Attr.getNumArgs() != 0) {
2285 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2286 return;
2287 }
Mike Stumpbf916502009-07-24 19:02:52 +00002288
Chandler Carruth87c44602011-07-01 23:49:12 +00002289 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002290 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002291 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002292 return;
2293 }
Mike Stumpbf916502009-07-24 19:02:52 +00002294
Chandler Carruth87c44602011-07-01 23:49:12 +00002295 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002296}
2297
Chandler Carruth87c44602011-07-01 23:49:12 +00002298static void HandleNoInstrumentFunctionAttr(Decl *D, const AttributeList &Attr,
Chris Lattner7255a2d2010-06-22 00:03:40 +00002299 Sema &S) {
2300 // check the attribute arguments.
2301 if (Attr.getNumArgs() != 0) {
2302 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2303 return;
2304 }
2305
Chandler Carruth87c44602011-07-01 23:49:12 +00002306 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002307 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002308 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002309 return;
2310 }
2311
Chandler Carruth87c44602011-07-01 23:49:12 +00002312 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002313 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002314}
2315
Chandler Carruth87c44602011-07-01 23:49:12 +00002316static void HandleConstantAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002317 if (S.LangOpts.CUDA) {
2318 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002319 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002320 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2321 return;
2322 }
2323
Chandler Carruth87c44602011-07-01 23:49:12 +00002324 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002325 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002326 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002327 return;
2328 }
2329
Chandler Carruth87c44602011-07-01 23:49:12 +00002330 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002331 } else {
2332 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2333 }
2334}
2335
Chandler Carruth87c44602011-07-01 23:49:12 +00002336static void HandleDeviceAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002337 if (S.LangOpts.CUDA) {
2338 // check the attribute arguments.
2339 if (Attr.getNumArgs() != 0) {
2340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2341 return;
2342 }
2343
Chandler Carruth87c44602011-07-01 23:49:12 +00002344 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002345 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002346 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002347 return;
2348 }
2349
Chandler Carruth87c44602011-07-01 23:49:12 +00002350 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002351 } else {
2352 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2353 }
2354}
2355
Chandler Carruth87c44602011-07-01 23:49:12 +00002356static void HandleGlobalAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002357 if (S.LangOpts.CUDA) {
2358 // check the attribute arguments.
2359 if (Attr.getNumArgs() != 0) {
2360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2361 return;
2362 }
2363
Chandler Carruth87c44602011-07-01 23:49:12 +00002364 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002365 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002366 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002367 return;
2368 }
2369
Chandler Carruth87c44602011-07-01 23:49:12 +00002370 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002371 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002372 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002373 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2374 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2375 << FD->getType()
2376 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2377 "void");
2378 } else {
2379 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2380 << FD->getType();
2381 }
2382 return;
2383 }
2384
Chandler Carruth87c44602011-07-01 23:49:12 +00002385 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002386 } else {
2387 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2388 }
2389}
2390
Chandler Carruth87c44602011-07-01 23:49:12 +00002391static void HandleHostAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002392 if (S.LangOpts.CUDA) {
2393 // check the attribute arguments.
2394 if (Attr.getNumArgs() != 0) {
2395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2396 return;
2397 }
2398
Chandler Carruth87c44602011-07-01 23:49:12 +00002399 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002400 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002401 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002402 return;
2403 }
2404
Chandler Carruth87c44602011-07-01 23:49:12 +00002405 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002406 } else {
2407 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2408 }
2409}
2410
Chandler Carruth87c44602011-07-01 23:49:12 +00002411static void HandleSharedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002412 if (S.LangOpts.CUDA) {
2413 // check the attribute arguments.
2414 if (Attr.getNumArgs() != 0) {
2415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2416 return;
2417 }
2418
Chandler Carruth87c44602011-07-01 23:49:12 +00002419 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002421 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002422 return;
2423 }
2424
Chandler Carruth87c44602011-07-01 23:49:12 +00002425 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002426 } else {
2427 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2428 }
2429}
2430
Chandler Carruth87c44602011-07-01 23:49:12 +00002431static void HandleGNUInlineAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002432 // check the attribute arguments.
2433 if (Attr.getNumArgs() != 0) {
2434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2435 return;
2436 }
Mike Stumpbf916502009-07-24 19:02:52 +00002437
Chandler Carruth87c44602011-07-01 23:49:12 +00002438 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002439 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002440 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002441 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002442 return;
2443 }
Mike Stumpbf916502009-07-24 19:02:52 +00002444
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002445 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002446 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002447 return;
2448 }
Mike Stumpbf916502009-07-24 19:02:52 +00002449
Chandler Carruth87c44602011-07-01 23:49:12 +00002450 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002451}
2452
Chandler Carruth87c44602011-07-01 23:49:12 +00002453static void HandleCallConvAttr(Decl *D, const AttributeList &Attr, Sema &S) {
2454 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002455
Chandler Carruth87c44602011-07-01 23:49:12 +00002456 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002457 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2458 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002459 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002460 return;
2461
Chandler Carruth87c44602011-07-01 23:49:12 +00002462 if (!isa<ObjCMethodDecl>(D)) {
2463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2464 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002465 return;
2466 }
2467
Chandler Carruth87c44602011-07-01 23:49:12 +00002468 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002469 case AttributeList::AT_fastcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002470 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002471 return;
2472 case AttributeList::AT_stdcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002473 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002474 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002475 case AttributeList::AT_thiscall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002476 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002477 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002478 case AttributeList::AT_cdecl:
Chandler Carruth87c44602011-07-01 23:49:12 +00002479 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002480 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002481 case AttributeList::AT_pascal:
Chandler Carruth87c44602011-07-01 23:49:12 +00002482 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002483 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002484 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002485 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002486 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2487 if (Str == 0 || Str->isWide()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002489 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002490 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002491 return;
2492 }
2493
2494 llvm::StringRef StrRef = Str->getString();
2495 PcsAttr::PCSType PCS;
2496 if (StrRef == "aapcs")
2497 PCS = PcsAttr::AAPCS;
2498 else if (StrRef == "aapcs-vfp")
2499 PCS = PcsAttr::AAPCS_VFP;
2500 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002501 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2502 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002503 return;
2504 }
2505
Chandler Carruth87c44602011-07-01 23:49:12 +00002506 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002507 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002508 default:
2509 llvm_unreachable("unexpected attribute kind");
2510 return;
2511 }
2512}
2513
Chandler Carruth87c44602011-07-01 23:49:12 +00002514static void HandleOpenCLKernelAttr(Decl *D, const AttributeList &Attr, Sema &S){
Peter Collingbournef315fa82011-02-14 01:42:53 +00002515 assert(Attr.isInvalid() == false);
Chandler Carruth87c44602011-07-01 23:49:12 +00002516 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002517}
2518
John McCall711c52b2011-01-05 12:14:39 +00002519bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2520 if (attr.isInvalid())
2521 return true;
2522
Ted Kremenek831efae2011-04-15 05:49:29 +00002523 if ((attr.getNumArgs() != 0 &&
2524 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2525 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002526 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2527 attr.setInvalid();
2528 return true;
2529 }
2530
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002531 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2532 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002533 switch (attr.getKind()) {
2534 case AttributeList::AT_cdecl: CC = CC_C; break;
2535 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2536 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2537 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2538 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002539 case AttributeList::AT_pcs: {
2540 Expr *Arg = attr.getArg(0);
2541 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2542 if (Str == 0 || Str->isWide()) {
2543 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2544 << "pcs" << 1;
2545 attr.setInvalid();
2546 return true;
2547 }
2548
2549 llvm::StringRef StrRef = Str->getString();
2550 if (StrRef == "aapcs") {
2551 CC = CC_AAPCS;
2552 break;
2553 } else if (StrRef == "aapcs-vfp") {
2554 CC = CC_AAPCS_VFP;
2555 break;
2556 }
2557 // FALLS THROUGH
2558 }
John McCall711c52b2011-01-05 12:14:39 +00002559 default: llvm_unreachable("unexpected attribute kind"); return true;
2560 }
2561
2562 return false;
2563}
2564
Chandler Carruth87c44602011-07-01 23:49:12 +00002565static void HandleRegparmAttr(Decl *D, const AttributeList &Attr, Sema &S) {
2566 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00002567
2568 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00002569 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00002570 return;
2571
Chandler Carruth87c44602011-07-01 23:49:12 +00002572 if (!isa<ObjCMethodDecl>(D)) {
2573 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2574 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002575 return;
2576 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002577
Chandler Carruth87c44602011-07-01 23:49:12 +00002578 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00002579}
2580
2581/// Checks a regparm attribute, returning true if it is ill-formed and
2582/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00002583bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2584 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00002585 return true;
2586
Chandler Carruth87c44602011-07-01 23:49:12 +00002587 if (Attr.getNumArgs() != 1) {
2588 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2589 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002590 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002591 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002592
Chandler Carruth87c44602011-07-01 23:49:12 +00002593 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002594 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002595 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002596 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002597 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002598 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002599 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002600 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002601 }
2602
John McCall711c52b2011-01-05 12:14:39 +00002603 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002604 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002605 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002606 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002607 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002608 }
2609
John McCall711c52b2011-01-05 12:14:39 +00002610 numParams = NumParams.getZExtValue();
2611 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002612 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall711c52b2011-01-05 12:14:39 +00002613 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002614 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002615 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002616 }
2617
John McCall711c52b2011-01-05 12:14:39 +00002618 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002619}
2620
Chandler Carruth87c44602011-07-01 23:49:12 +00002621static void HandleLaunchBoundsAttr(Decl *D, const AttributeList &Attr, Sema &S){
Peter Collingbourne7b381982010-12-12 23:03:07 +00002622 if (S.LangOpts.CUDA) {
2623 // check the attribute arguments.
2624 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002625 // FIXME: 0 is not okay.
2626 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002627 return;
2628 }
2629
Chandler Carruth87c44602011-07-01 23:49:12 +00002630 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00002631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002632 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002633 return;
2634 }
2635
2636 Expr *MaxThreadsExpr = Attr.getArg(0);
2637 llvm::APSInt MaxThreads(32);
2638 if (MaxThreadsExpr->isTypeDependent() ||
2639 MaxThreadsExpr->isValueDependent() ||
2640 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2641 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2642 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2643 return;
2644 }
2645
2646 llvm::APSInt MinBlocks(32);
2647 if (Attr.getNumArgs() > 1) {
2648 Expr *MinBlocksExpr = Attr.getArg(1);
2649 if (MinBlocksExpr->isTypeDependent() ||
2650 MinBlocksExpr->isValueDependent() ||
2651 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2652 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2653 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2654 return;
2655 }
2656 }
2657
Chandler Carruth87c44602011-07-01 23:49:12 +00002658 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00002659 MaxThreads.getZExtValue(),
2660 MinBlocks.getZExtValue()));
2661 } else {
2662 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2663 }
2664}
2665
Chris Lattner0744e5f2008-06-29 00:23:49 +00002666//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002667// Checker-specific attribute handlers.
2668//===----------------------------------------------------------------------===//
2669
John McCallc7ad3812011-01-25 03:31:58 +00002670static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2671 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2672}
2673static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2674 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2675}
2676
Chandler Carruth87c44602011-07-01 23:49:12 +00002677static void HandleNSConsumedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
2678 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00002679 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002680 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2681 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002682 return;
2683 }
2684
2685 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002686 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00002687 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2688 cf = false;
2689 } else {
2690 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2691 cf = true;
2692 }
2693
2694 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002695 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2696 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00002697 return;
2698 }
2699
2700 if (cf)
Chandler Carruth87c44602011-07-01 23:49:12 +00002701 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002702 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002703 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002704}
2705
Chandler Carruth87c44602011-07-01 23:49:12 +00002706static void HandleNSConsumesSelfAttr(Decl *D, const AttributeList &Attr,
John McCallc7ad3812011-01-25 03:31:58 +00002707 Sema &S) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002708 if (!isa<ObjCMethodDecl>(D)) {
2709 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2710 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002711 return;
2712 }
2713
Chandler Carruth87c44602011-07-01 23:49:12 +00002714 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002715}
2716
Chandler Carruth87c44602011-07-01 23:49:12 +00002717static void HandleNSReturnsRetainedAttr(Decl *D, const AttributeList &Attr,
Ted Kremenekb71368d2009-05-09 02:44:38 +00002718 Sema &S) {
2719
John McCallc7ad3812011-01-25 03:31:58 +00002720 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002721
Chandler Carruth87c44602011-07-01 23:49:12 +00002722 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002723 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002724 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00002725 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002726 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2727 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00002728 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00002729 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002730 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002731 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002732 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2733 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002734 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002735 return;
2736 }
Mike Stumpbf916502009-07-24 19:02:52 +00002737
John McCallc7ad3812011-01-25 03:31:58 +00002738 bool typeOK;
2739 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002740 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00002741 default: llvm_unreachable("invalid ownership attribute"); return;
2742 case AttributeList::AT_ns_returns_autoreleased:
2743 case AttributeList::AT_ns_returns_retained:
2744 case AttributeList::AT_ns_returns_not_retained:
2745 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2746 cf = false;
2747 break;
2748
2749 case AttributeList::AT_cf_returns_retained:
2750 case AttributeList::AT_cf_returns_not_retained:
2751 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2752 cf = true;
2753 break;
2754 }
2755
2756 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002757 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2758 << SourceRange(Attr.getLoc())
2759 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002760 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002761 }
Mike Stumpbf916502009-07-24 19:02:52 +00002762
Chandler Carruth87c44602011-07-01 23:49:12 +00002763 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002764 default:
2765 assert(0 && "invalid ownership attribute");
2766 return;
John McCallc7ad3812011-01-25 03:31:58 +00002767 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruth87c44602011-07-01 23:49:12 +00002768 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCallc7ad3812011-01-25 03:31:58 +00002769 S.Context));
2770 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002771 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002772 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002773 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002774 return;
2775 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002776 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002777 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002778 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002779 case AttributeList::AT_cf_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002780 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002781 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002782 return;
2783 case AttributeList::AT_ns_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002784 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002785 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002786 return;
2787 };
2788}
2789
Chandler Carruth87c44602011-07-01 23:49:12 +00002790static void HandleObjCOwnershipAttr(Decl *D, const AttributeList &Attr,
John McCallf85e1932011-06-15 23:02:42 +00002791 Sema &S) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002792 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00002793
Chandler Carruth87c44602011-07-01 23:49:12 +00002794 SourceLocation L = Attr.getLoc();
2795 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2796 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00002797}
2798
Chandler Carruth87c44602011-07-01 23:49:12 +00002799static void HandleObjCPreciseLifetimeAttr(Decl *D, const AttributeList &Attr,
John McCallf85e1932011-06-15 23:02:42 +00002800 Sema &S) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002801 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
2802 SourceLocation L = Attr.getLoc();
2803 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2804 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00002805 return;
2806 }
2807
Chandler Carruth87c44602011-07-01 23:49:12 +00002808 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00002809 QualType type = vd->getType();
2810
2811 if (!type->isDependentType() &&
2812 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002813 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00002814 << type;
2815 return;
2816 }
2817
2818 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
2819
2820 // If we have no lifetime yet, check the lifetime we're presumably
2821 // going to infer.
2822 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
2823 lifetime = type->getObjCARCImplicitLifetime();
2824
2825 switch (lifetime) {
2826 case Qualifiers::OCL_None:
2827 assert(type->isDependentType() &&
2828 "didn't infer lifetime for non-dependent type?");
2829 break;
2830
2831 case Qualifiers::OCL_Weak: // meaningful
2832 case Qualifiers::OCL_Strong: // meaningful
2833 break;
2834
2835 case Qualifiers::OCL_ExplicitNone:
2836 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00002837 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00002838 << (lifetime == Qualifiers::OCL_Autoreleasing);
2839 break;
2840 }
2841
Chandler Carruth87c44602011-07-01 23:49:12 +00002842 D->addAttr(::new (S.Context)
2843 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00002844}
2845
Charles Davisf0122fe2010-02-16 18:27:26 +00002846static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2847 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002848 Attr.getKind() == AttributeList::AT_dllexport ||
2849 Attr.getKind() == AttributeList::AT_uuid;
2850}
2851
2852//===----------------------------------------------------------------------===//
2853// Microsoft specific attribute handlers.
2854//===----------------------------------------------------------------------===//
2855
Chandler Carruth87c44602011-07-01 23:49:12 +00002856static void HandleUuidAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Francois Pichet11542142010-12-19 06:50:37 +00002857 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2858 // check the attribute arguments.
2859 if (Attr.getNumArgs() != 1) {
2860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2861 return;
2862 }
2863 Expr *Arg = Attr.getArg(0);
2864 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002865 if (Str == 0 || Str->isWide()) {
2866 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2867 << "uuid" << 1;
2868 return;
2869 }
2870
2871 llvm::StringRef StrRef = Str->getString();
2872
2873 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2874 StrRef.back() == '}';
2875
2876 // Validate GUID length.
2877 if (IsCurly && StrRef.size() != 38) {
2878 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2879 return;
2880 }
2881 if (!IsCurly && StrRef.size() != 36) {
2882 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2883 return;
2884 }
2885
2886 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2887 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlssonf89e0422011-01-23 21:07:30 +00002888 llvm::StringRef::iterator I = StrRef.begin();
2889 if (IsCurly) // Skip the optional '{'
2890 ++I;
2891
2892 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002893 if (i == 8 || i == 13 || i == 18 || i == 23) {
2894 if (*I != '-') {
2895 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2896 return;
2897 }
2898 } else if (!isxdigit(*I)) {
2899 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2900 return;
2901 }
2902 I++;
2903 }
Francois Pichet11542142010-12-19 06:50:37 +00002904
Chandler Carruth87c44602011-07-01 23:49:12 +00002905 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00002906 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002907 } else
Francois Pichet11542142010-12-19 06:50:37 +00002908 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002909}
2910
Ted Kremenekb71368d2009-05-09 02:44:38 +00002911//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002912// Top Level Sema Entry Points
2913//===----------------------------------------------------------------------===//
2914
Peter Collingbourne60700392011-01-21 02:08:45 +00002915static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2916 const AttributeList &Attr, Sema &S) {
2917 switch (Attr.getKind()) {
2918 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2919 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2920 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2921 default:
2922 break;
2923 }
2924}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002925
Peter Collingbourne60700392011-01-21 02:08:45 +00002926static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2927 const AttributeList &Attr, Sema &S) {
Chris Lattner803d0802008-06-29 00:43:07 +00002928 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002929 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002930 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2931 case AttributeList::AT_IBOutletCollection:
2932 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002933 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002934 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002935 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002936 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002937 case AttributeList::AT_neon_vector_type:
2938 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002939 // Ignore these, these are type attributes, handled by
2940 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002941 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002942 case AttributeList::AT_device:
2943 case AttributeList::AT_host:
2944 case AttributeList::AT_overloadable:
2945 // Ignore, this is a non-inheritable attribute, handled
2946 // by ProcessNonInheritableDeclAttr.
2947 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002948 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2949 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002950 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002951 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002952 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002953 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002954 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002955 case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002956 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002957 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002958 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002959 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002960 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2961 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2962 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002963 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002964 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002965 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002966 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2967 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002968 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002969 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002970 case AttributeList::AT_launch_bounds:
2971 HandleLaunchBoundsAttr(D, Attr, S);
2972 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002973 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2974 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002975 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002976 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002977 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002978 case AttributeList::AT_ownership_returns:
2979 case AttributeList::AT_ownership_takes:
2980 case AttributeList::AT_ownership_holds:
2981 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002982 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002983 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2984 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002985 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002986 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002987
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00002988 case AttributeList::AT_objc_ownership:
2989 HandleObjCOwnershipAttr(D, Attr, S); break;
John McCallf85e1932011-06-15 23:02:42 +00002990 case AttributeList::AT_objc_precise_lifetime:
2991 HandleObjCPreciseLifetimeAttr(D, Attr, S); break;
2992
Ted Kremenekb71368d2009-05-09 02:44:38 +00002993 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00002994 case AttributeList::AT_cf_consumed:
2995 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2996 case AttributeList::AT_ns_consumes_self:
2997 HandleNSConsumesSelfAttr(D, Attr, S); break;
2998
2999 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003000 case AttributeList::AT_ns_returns_not_retained:
3001 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003002 case AttributeList::AT_ns_returns_retained:
3003 case AttributeList::AT_cf_returns_retained:
3004 HandleNSReturnsRetainedAttr(D, Attr, S); break;
3005
Nate Begeman6f3d8382009-06-26 06:32:41 +00003006 case AttributeList::AT_reqd_wg_size:
3007 HandleReqdWorkGroupSize(D, Attr, S); break;
3008
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003009 case AttributeList::AT_init_priority:
3010 HandleInitPriorityAttr(D, Attr, S); break;
3011
Sean Hunt7725e672009-11-25 04:20:27 +00003012 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00003013 case AttributeList::AT_MsStruct: HandleMsStructAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00003014 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00003015 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
3016 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
3017 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00003018 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00003019 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
3020 break;
Sean Hunt7725e672009-11-25 04:20:27 +00003021 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003022 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00003023 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003024 case AttributeList::AT_transparent_union:
3025 HandleTransparentUnionAttr(D, Attr, S);
3026 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003027 case AttributeList::AT_objc_exception:
3028 HandleObjCExceptionAttr(D, Attr, S);
3029 break;
John McCalld5313b02011-03-02 11:33:24 +00003030 case AttributeList::AT_objc_method_family:
3031 HandleObjCMethodFamilyAttr(D, Attr, S);
3032 break;
Sean Hunt7725e672009-11-25 04:20:27 +00003033 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
3034 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
3035 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
3036 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
3037 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
3038 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
3039 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
3040 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
3041 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003042 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003043 // Just ignore
3044 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003045 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
3046 HandleNoInstrumentFunctionAttr(D, Attr, S);
3047 break;
John McCall04a67a62010-02-05 21:31:56 +00003048 case AttributeList::AT_stdcall:
3049 case AttributeList::AT_cdecl:
3050 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003051 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003052 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003053 case AttributeList::AT_pcs:
Abramo Bagnarae215f722010-04-30 13:10:51 +00003054 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00003055 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003056 case AttributeList::AT_opencl_kernel_function:
3057 HandleOpenCLKernelAttr(D, Attr, S);
3058 break;
Francois Pichet11542142010-12-19 06:50:37 +00003059 case AttributeList::AT_uuid:
3060 HandleUuidAttr(D, Attr, S);
3061 break;
Chris Lattner803d0802008-06-29 00:43:07 +00003062 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003063 // Ask target about the attribute.
3064 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3065 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003066 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3067 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003068 break;
3069 }
3070}
3071
Peter Collingbourne60700392011-01-21 02:08:45 +00003072/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3073/// the attribute applies to decls. If the attribute is a type attribute, just
3074/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3075/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
3076static void ProcessDeclAttribute(Scope *scope, Decl *D,
3077 const AttributeList &Attr, Sema &S,
3078 bool NonInheritable, bool Inheritable) {
3079 if (Attr.isInvalid())
3080 return;
3081
3082 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3083 // FIXME: Try to deal with other __declspec attributes!
3084 return;
3085
3086 if (NonInheritable)
3087 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
3088
3089 if (Inheritable)
3090 ProcessInheritableDeclAttr(scope, D, Attr, S);
3091}
3092
Chris Lattner803d0802008-06-29 00:43:07 +00003093/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3094/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003095void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003096 const AttributeList *AttrList,
3097 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003098 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003099 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003100 }
3101
3102 // GCC accepts
3103 // static int a9 __attribute__((weakref));
3104 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003105 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003106 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003107 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003108 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003109 }
3110}
3111
Ryan Flynne25ff832009-07-30 03:15:39 +00003112/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3113/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003114NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003115 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003116 NamedDecl *NewD = 0;
3117 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3118 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003119 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003120 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003121 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003122 if (FD->getQualifier()) {
3123 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003124 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003125 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003126 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3127 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003128 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003129 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003130 VD->getStorageClass(),
3131 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003132 if (VD->getQualifier()) {
3133 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003134 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003135 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003136 }
3137 return NewD;
3138}
3139
3140/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3141/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003142void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003143 if (W.getUsed()) return; // only do this once
3144 W.setUsed(true);
3145 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3146 IdentifierInfo *NDId = ND->getIdentifier();
3147 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003148 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3149 NDId->getName()));
3150 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003151 WeakTopLevelDecl.push_back(NewD);
3152 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3153 // to insert Decl at TU scope, sorry.
3154 DeclContext *SavedContext = CurContext;
3155 CurContext = Context.getTranslationUnitDecl();
3156 PushOnScopeChains(NewD, S);
3157 CurContext = SavedContext;
3158 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003159 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003160 }
3161}
3162
Chris Lattner0744e5f2008-06-29 00:23:49 +00003163/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3164/// it, apply them to D. This is a bit tricky because PD can have attributes
3165/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003166void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3167 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003168 // It's valid to "forward-declare" #pragma weak, in which case we
3169 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00003170 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00003171 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3172 if (IdentifierInfo *Id = ND->getIdentifier()) {
3173 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3174 = WeakUndeclaredIdentifiers.find(Id);
3175 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3176 WeakInfo W = I->second;
3177 DeclApplyPragmaWeak(S, ND, W);
3178 WeakUndeclaredIdentifiers[Id] = W;
3179 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003180 }
3181 }
3182 }
3183
Chris Lattner0744e5f2008-06-29 00:23:49 +00003184 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003185 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003186 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003187
Chris Lattner0744e5f2008-06-29 00:23:49 +00003188 // Walk the declarator structure, applying decl attributes that were in a type
3189 // position to the decl itself. This handles cases like:
3190 // int *__attr__(x)** D;
3191 // when X is a decl attribute.
3192 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3193 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003194 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003195
Chris Lattner0744e5f2008-06-29 00:23:49 +00003196 // Finally, apply any attributes on the decl itself.
3197 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003198 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003199}
John McCall54abf7d2009-11-04 02:18:39 +00003200
John McCallf85e1932011-06-15 23:02:42 +00003201/// Is the given declaration allowed to use a forbidden type?
3202static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3203 // Private ivars are always okay. Unfortunately, people don't
3204 // always properly make their ivars private, even in system headers.
3205 // Plus we need to make fields okay, too.
3206 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3207 return false;
3208
3209 // Require it to be declared in a system header.
3210 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3211}
3212
3213/// Handle a delayed forbidden-type diagnostic.
3214static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3215 Decl *decl) {
3216 if (decl && isForbiddenTypeAllowed(S, decl)) {
3217 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3218 "this system declaration uses an unsupported type"));
3219 return;
3220 }
3221
3222 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3223 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3224 diag.Triggered = true;
3225}
3226
John McCalleee1d542011-02-14 07:13:47 +00003227// This duplicates a vector push_back but hides the need to know the
3228// size of the type.
3229void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3230 assert(StackSize <= StackCapacity);
3231
3232 // Grow the stack if necessary.
3233 if (StackSize == StackCapacity) {
3234 unsigned newCapacity = 2 * StackCapacity + 2;
3235 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3236 const char *oldBuffer = (const char*) Stack;
3237
3238 if (StackCapacity)
3239 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3240
3241 delete[] oldBuffer;
3242 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3243 StackCapacity = newCapacity;
3244 }
3245
3246 assert(StackSize < StackCapacity);
3247 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003248}
3249
John McCalleee1d542011-02-14 07:13:47 +00003250void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3251 Decl *decl) {
3252 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003253
John McCalleee1d542011-02-14 07:13:47 +00003254 // Check the invariants.
3255 assert(DD.StackSize >= state.SavedStackSize);
3256 assert(state.SavedStackSize >= DD.ActiveStackBase);
3257 assert(DD.ParsingDepth > 0);
3258
3259 // Drop the parsing depth.
3260 DD.ParsingDepth--;
3261
3262 // If there are no active diagnostics, we're done.
3263 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003264 return;
3265
John McCall2f514482010-01-27 03:50:35 +00003266 // We only want to actually emit delayed diagnostics when we
3267 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003268 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003269 // We emit all the active diagnostics, not just those starting
3270 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003271 // decl spec and another for each declarator; in a decl group like:
3272 // deprecated_typedef foo, *bar, baz();
3273 // only the declarator pops will be passed decls. This is correct;
3274 // we really do need to consider delayed diagnostics from the decl spec
3275 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003276 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3277 DelayedDiagnostic &diag = DD.Stack[i];
3278 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003279 continue;
3280
John McCalleee1d542011-02-14 07:13:47 +00003281 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003282 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003283 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003284 break;
3285
3286 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003287 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003288 break;
John McCallf85e1932011-06-15 23:02:42 +00003289
3290 case DelayedDiagnostic::ForbiddenType:
3291 handleDelayedForbiddenType(S, diag, decl);
3292 break;
John McCall2f514482010-01-27 03:50:35 +00003293 }
3294 }
3295 }
3296
John McCall58e6f342010-03-16 05:22:47 +00003297 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003298 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003299 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003300
John McCalleee1d542011-02-14 07:13:47 +00003301 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003302}
3303
3304static bool isDeclDeprecated(Decl *D) {
3305 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003306 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003307 return true;
3308 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3309 return false;
3310}
3311
John McCall9c3087b2010-08-26 02:13:20 +00003312void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003313 Decl *Ctx) {
3314 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003315 return;
3316
John McCall2f514482010-01-27 03:50:35 +00003317 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003318 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003319 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003320 << DD.getDeprecationDecl()->getDeclName()
3321 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003322 else
3323 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003324 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003325}
3326
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003327void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003328 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003329 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003330 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003331 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3332 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003333 return;
3334 }
3335
3336 // Otherwise, don't warn if our current context is deprecated.
3337 if (isDeclDeprecated(cast<Decl>(CurContext)))
3338 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003339 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003340 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3341 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003342 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003343 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003344 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003345 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003346 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003347 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3348 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003349 }
John McCall54abf7d2009-11-04 02:18:39 +00003350}