blob: 2294c0e7844daee116c02209f05802f2fc37a54a [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000021#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000023#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000025using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000026using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000027
John McCall883cc2c2011-03-02 12:29:23 +000028/// These constants match the enumerated choices of
29/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
30enum {
31 ExpectedFunction,
32 ExpectedUnion,
33 ExpectedVariableOrFunction,
34 ExpectedFunctionOrMethod,
35 ExpectedParameter,
36 ExpectedParameterOrMethod,
37 ExpectedFunctionMethodOrBlock,
38 ExpectedClassOrVirtualMethod,
39 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
41 ExpectedVirtualMethod,
42 ExpectedClassMember,
43 ExpectedVariable,
44 ExpectedMethod,
45 ExpectedVariableFunctionOrLabel
46};
47
Chris Lattnere5c5ee12008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruth87c44602011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000063
Chris Lattner6b6b5372008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000068
John McCall183700f2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000070}
71
Daniel Dunbar35682492008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopesd20254f2009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000086}
87
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruth87c44602011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100}
101
John McCall711c52b2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000108}
109
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000131}
132
Chandler Carruth87c44602011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000138
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000140}
141
Chandler Carruth87c44602011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146}
147
Chandler Carruth87c44602011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000154 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000169
John McCall506b57e2010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000173
John McCall506b57e2010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000175
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000189
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Chandler Carruth1731e202011-07-11 23:30:35 +0000197static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
198 unsigned int Num) {
199 if (Attr.getNumArgs() != Num) {
200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
201 return false;
202 }
203
204 return true;
205}
206
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000207///
208/// \brief Check if passed in Decl is a field or potentially shared global var
209/// \return true if the Decl is a field or potentially shared global variable
210///
211static bool mayBeSharedVariable(Decl *D) {
212 if (isa<FieldDecl>(D))
213 return true;
214 if(VarDecl *vd = dyn_cast<VarDecl>(D))
215 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
216
217 return false;
218}
219
220///
221/// \brief Check if passed in Decl is a pointer type.
222/// Note that this function may produce an error message.
223/// \return true if the Decl is a pointer type; false otherwise
224///
225bool checkIsPointer(Sema & S, Decl * D, const AttributeList & Attr) {
226 if(ValueDecl * vd = dyn_cast <ValueDecl>(D)) {
227 QualType QT = vd->getType();
228 if(QT->isAnyPointerType()){
229 return true;
230 }
231 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
232 << Attr.getName()->getName() << QT;
233 } else {
234 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
235 << Attr.getName();
236 }
237 return false;
238}
239
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000240//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000241// Attribute Implementations
242//===----------------------------------------------------------------------===//
243
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000244// FIXME: All this manual attribute parsing code is gross. At the
245// least add some helper functions to check most argument patterns (#
246// and types of args).
247
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000248static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
249 bool pointer = false) {
250 assert(!Attr.isInvalid());
251
252 if (!checkAttributeNumArgs(S, Attr, 0))
253 return;
254
255 // D must be either a member field or global (potentially shared) variable.
256 if (!mayBeSharedVariable(D)) {
257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
258 << Attr.getName() << 15; /*fields and global vars*/;
259 return;
260 }
261
262 if (pointer && !checkIsPointer(S, D, Attr))
263 return;
264
265 if (pointer)
266 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getLoc(), S.Context));
267 else
268 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getLoc(), S.Context));
269}
270
271static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
272 bool scoped = false) {
273 assert(!Attr.isInvalid());
274
275 if (!checkAttributeNumArgs(S, Attr, 0))
276 return;
277
278 if (!isa<CXXRecordDecl>(D)) {
279 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
280 << Attr.getName() << ExpectedClass;
281 return;
282 }
283
284 if (scoped)
285 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getLoc(), S.Context));
286 else
287 D->addAttr(::new (S.Context) LockableAttr(Attr.getLoc(), S.Context));
288}
289
290static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
291 const AttributeList &Attr) {
292 assert(!Attr.isInvalid());
293
294 if (!checkAttributeNumArgs(S, Attr, 0))
295 return;
296
297 if (!isFunction(D)) {
298 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
299 << Attr.getName() << ExpectedFunctionOrMethod;
300 return;
301 }
302
303 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getLoc(),
304 S.Context));
305}
306
Chandler Carruth1b03c872011-07-02 00:01:44 +0000307static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
308 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000309 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000310 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000311 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000312 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000313 }
Mike Stumpbf916502009-07-24 19:02:52 +0000314
Chris Lattner6b6b5372008-06-26 18:38:35 +0000315 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000316
317 Expr *sizeExpr;
318
319 // Special case where the argument is a template id.
320 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000321 CXXScopeSpec SS;
322 UnqualifiedId id;
323 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000324
325 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
326 if (Size.isInvalid())
327 return;
328
329 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000330 } else {
331 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000332 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000333 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000334
Peter Collingbourne7a730022010-11-23 20:45:58 +0000335 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000336 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000337
338 // Instantiate/Install the vector type, and let Sema build the type for us.
339 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000340 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000341 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000342 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000343 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000344
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000345 // Remember this typedef decl, we will need it later for diagnostics.
346 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000347 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000348}
349
Chandler Carruth1b03c872011-07-02 00:01:44 +0000350static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000351 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000352 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000353 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000354
Chandler Carruth87c44602011-07-01 23:49:12 +0000355 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Sean Huntcf807c42010-08-18 23:23:40 +0000356 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000357 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000358 // If the alignment is less than or equal to 8 bits, the packed attribute
359 // has no effect.
360 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000361 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000362 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000363 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000364 else
Sean Huntcf807c42010-08-18 23:23:40 +0000365 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000366 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000367 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000368}
369
Chandler Carruth1b03c872011-07-02 00:01:44 +0000370static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000371 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000372 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
373 else
374 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
375}
376
Chandler Carruth1b03c872011-07-02 00:01:44 +0000377static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000378 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000379 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000380 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000381
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000382 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000383 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000384 if (MD->isInstanceMethod()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000385 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000386 return;
387 }
388
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000389 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000390}
391
Chandler Carruth1b03c872011-07-02 00:01:44 +0000392static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000393 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000394 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000395 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000396
397 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000398 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000399 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
400 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000401 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000402 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000403
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000404 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000405}
406
Chandler Carruth1b03c872011-07-02 00:01:44 +0000407static void handleIBOutletCollection(Sema &S, Decl *D,
408 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000409
410 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000411 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000412 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
413 return;
414 }
415
416 // The IBOutletCollection attributes only apply to instance variables of
417 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000418 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000419 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000420 return;
421 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000422 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000423 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
424 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
425 << VD->getType() << 0;
426 return;
427 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000428 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000429 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
430 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
431 << PD->getType() << 1;
432 return;
433 }
434
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000435 IdentifierInfo *II = Attr.getParameterName();
436 if (!II)
437 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000438
John McCallb3d87482010-08-24 05:47:05 +0000439 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000440 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000441 if (!TypeRep) {
442 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
443 return;
444 }
John McCallb3d87482010-08-24 05:47:05 +0000445 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000446 // Diagnose use of non-object type in iboutletcollection attribute.
447 // FIXME. Gnu attribute extension ignores use of builtin types in
448 // attributes. So, __attribute__((iboutletcollection(char))) will be
449 // treated as __attribute__((iboutletcollection())).
450 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
451 !QT->isObjCObjectType()) {
452 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
453 return;
454 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000455 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000456 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000457}
458
Chandler Carruthd309c812011-07-01 23:49:16 +0000459static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000460 if (const RecordType *UT = T->getAsUnionType())
461 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
462 RecordDecl *UD = UT->getDecl();
463 for (RecordDecl::field_iterator it = UD->field_begin(),
464 itend = UD->field_end(); it != itend; ++it) {
465 QualType QT = it->getType();
466 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
467 T = QT;
468 return;
469 }
470 }
471 }
472}
473
Chandler Carruth1b03c872011-07-02 00:01:44 +0000474static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000475 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
476 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000477 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000478 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000479 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000480 return;
481 }
Mike Stumpbf916502009-07-24 19:02:52 +0000482
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000483 // In C++ the implicit 'this' function parameter also counts, and they are
484 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000485 bool HasImplicitThisParam = isInstanceMethod(D);
486 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000487
488 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000489 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000490
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000491 for (AttributeList::arg_iterator I=Attr.arg_begin(),
492 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000493
494
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000495 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000496 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000497 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000498 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
499 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
501 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000502 return;
503 }
Mike Stumpbf916502009-07-24 19:02:52 +0000504
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000505 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000506
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000507 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000509 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000510 return;
511 }
Mike Stumpbf916502009-07-24 19:02:52 +0000512
Ted Kremenek465172f2008-07-21 22:09:15 +0000513 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000514 if (HasImplicitThisParam) {
515 if (x == 0) {
516 S.Diag(Attr.getLoc(),
517 diag::err_attribute_invalid_implicit_this_argument)
518 << "nonnull" << Ex->getSourceRange();
519 return;
520 }
521 --x;
522 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000523
524 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000525 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000526 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000527
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000528 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000529 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000530 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000531 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000532 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000533 }
Mike Stumpbf916502009-07-24 19:02:52 +0000534
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000535 NonNullArgs.push_back(x);
536 }
Mike Stumpbf916502009-07-24 19:02:52 +0000537
538 // If no arguments were specified to __attribute__((nonnull)) then all pointer
539 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000540 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000541 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
542 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000543 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000544 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000545 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000546 }
Mike Stumpbf916502009-07-24 19:02:52 +0000547
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000548 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000549 if (NonNullArgs.empty()) {
550 // Warn the trivial case only if attribute is not coming from a
551 // macro instantiation.
552 if (Attr.getLoc().isFileID())
553 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000554 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000555 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000556 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000557
558 unsigned* start = &NonNullArgs[0];
559 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000560 llvm::array_pod_sort(start, start + size);
Chandler Carruth87c44602011-07-01 23:49:12 +0000561 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000562 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000563}
564
Chandler Carruth1b03c872011-07-02 00:01:44 +0000565static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000566 // This attribute must be applied to a function declaration.
567 // The first argument to the attribute must be a string,
568 // the name of the resource, for example "malloc".
569 // The following arguments must be argument indexes, the arguments must be
570 // of integer type for Returns, otherwise of pointer type.
571 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000572 // after being held. free() should be __attribute((ownership_takes)), whereas
573 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000574
575 if (!AL.getParameterName()) {
576 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
577 << AL.getName()->getName() << 1;
578 return;
579 }
580 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000581 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000582 switch (AL.getKind()) {
583 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000584 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000585 if (AL.getNumArgs() < 1) {
586 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
587 return;
588 }
Jordy Rose2a479922010-08-12 08:54:03 +0000589 break;
590 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000591 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000592 if (AL.getNumArgs() < 1) {
593 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
594 return;
595 }
Jordy Rose2a479922010-08-12 08:54:03 +0000596 break;
597 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000598 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000599 if (AL.getNumArgs() > 1) {
600 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
601 << AL.getNumArgs() + 1;
602 return;
603 }
Jordy Rose2a479922010-08-12 08:54:03 +0000604 break;
605 default:
606 // This should never happen given how we are called.
607 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000608 }
609
Chandler Carruth87c44602011-07-01 23:49:12 +0000610 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000611 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
612 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000613 return;
614 }
615
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000616 // In C++ the implicit 'this' function parameter also counts, and they are
617 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000618 bool HasImplicitThisParam = isInstanceMethod(D);
619 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000620
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000622
623 // Normalize the argument, __foo__ becomes foo.
624 if (Module.startswith("__") && Module.endswith("__"))
625 Module = Module.substr(2, Module.size() - 4);
626
Chris Lattner5f9e2722011-07-23 10:55:15 +0000627 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000628
Jordy Rose2a479922010-08-12 08:54:03 +0000629 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
630 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000631
Peter Collingbourne7a730022010-11-23 20:45:58 +0000632 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000633 llvm::APSInt ArgNum(32);
634 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
635 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
636 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
637 << AL.getName()->getName() << IdxExpr->getSourceRange();
638 continue;
639 }
640
641 unsigned x = (unsigned) ArgNum.getZExtValue();
642
643 if (x > NumArgs || x < 1) {
644 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
645 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
646 continue;
647 }
648 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000649 if (HasImplicitThisParam) {
650 if (x == 0) {
651 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
652 << "ownership" << IdxExpr->getSourceRange();
653 return;
654 }
655 --x;
656 }
657
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000658 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000659 case OwnershipAttr::Takes:
660 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000661 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000662 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000663 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
664 // FIXME: Should also highlight argument in decl.
665 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000666 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000667 << "pointer"
668 << IdxExpr->getSourceRange();
669 continue;
670 }
671 break;
672 }
Sean Huntcf807c42010-08-18 23:23:40 +0000673 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000674 if (AL.getNumArgs() > 1) {
675 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000676 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000677 llvm::APSInt ArgNum(32);
678 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
679 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
680 S.Diag(AL.getLoc(), diag::err_ownership_type)
681 << "ownership_returns" << "integer"
682 << IdxExpr->getSourceRange();
683 return;
684 }
685 }
686 break;
687 }
Jordy Rose2a479922010-08-12 08:54:03 +0000688 default:
689 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000690 } // switch
691
692 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000693 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +0000694 i = D->specific_attr_begin<OwnershipAttr>(),
695 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +0000696 i != e; ++i) {
697 if ((*i)->getOwnKind() != K) {
698 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
699 I!=E; ++I) {
700 if (x == *I) {
701 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
702 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000703 }
704 }
705 }
706 }
707 OwnershipArgs.push_back(x);
708 }
709
710 unsigned* start = OwnershipArgs.data();
711 unsigned size = OwnershipArgs.size();
712 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000713
714 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
715 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
716 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000717 }
Sean Huntcf807c42010-08-18 23:23:40 +0000718
Chandler Carruth87c44602011-07-01 23:49:12 +0000719 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +0000720 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000721}
722
John McCall332bb2a2011-02-08 22:35:49 +0000723/// Whether this declaration has internal linkage for the purposes of
724/// things that want to complain about things not have internal linkage.
725static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
726 switch (D->getLinkage()) {
727 case NoLinkage:
728 case InternalLinkage:
729 return true;
730
731 // Template instantiations that go from external to unique-external
732 // shouldn't get diagnosed.
733 case UniqueExternalLinkage:
734 return true;
735
736 case ExternalLinkage:
737 return false;
738 }
739 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000740 return false;
741}
742
Chandler Carruth1b03c872011-07-02 00:01:44 +0000743static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000744 // Check the attribute arguments.
745 if (Attr.getNumArgs() > 1) {
746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
747 return;
748 }
749
Chandler Carruth87c44602011-07-01 23:49:12 +0000750 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000752 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +0000753 return;
754 }
755
Chandler Carruth87c44602011-07-01 23:49:12 +0000756 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +0000757
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000758 // gcc rejects
759 // class c {
760 // static int a __attribute__((weakref ("v2")));
761 // static int b() __attribute__((weakref ("f3")));
762 // };
763 // and ignores the attributes of
764 // void f(void) {
765 // static int a __attribute__((weakref ("v2")));
766 // }
767 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +0000768 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000769 if (!Ctx->isFileContext()) {
770 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +0000771 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000772 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000773 }
774
775 // The GCC manual says
776 //
777 // At present, a declaration to which `weakref' is attached can only
778 // be `static'.
779 //
780 // It also says
781 //
782 // Without a TARGET,
783 // given as an argument to `weakref' or to `alias', `weakref' is
784 // equivalent to `weak'.
785 //
786 // gcc 4.4.1 will accept
787 // int a7 __attribute__((weakref));
788 // as
789 // int a7 __attribute__((weak));
790 // This looks like a bug in gcc. We reject that for now. We should revisit
791 // it if this behaviour is actually used.
792
John McCall332bb2a2011-02-08 22:35:49 +0000793 if (!hasEffectivelyInternalLinkage(nd)) {
794 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000795 return;
796 }
797
798 // GCC rejects
799 // static ((alias ("y"), weakref)).
800 // Should we? How to check that weakref is before or after alias?
801
802 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000803 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000804 Arg = Arg->IgnoreParenCasts();
805 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
806
Douglas Gregor5cee1192011-07-27 05:40:30 +0000807 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000808 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
809 << "weakref" << 1;
810 return;
811 }
812 // GCC will accept anything as the argument of weakref. Should we
813 // check for an existing decl?
Chandler Carruth87c44602011-07-01 23:49:12 +0000814 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000815 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000816 }
817
Chandler Carruth87c44602011-07-01 23:49:12 +0000818 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000819}
820
Chandler Carruth1b03c872011-07-02 00:01:44 +0000821static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000822 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000823 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000824 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000825 return;
826 }
Mike Stumpbf916502009-07-24 19:02:52 +0000827
Peter Collingbourne7a730022010-11-23 20:45:58 +0000828 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000829 Arg = Arg->IgnoreParenCasts();
830 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000831
Douglas Gregor5cee1192011-07-27 05:40:30 +0000832 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000833 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000834 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000835 return;
836 }
Mike Stumpbf916502009-07-24 19:02:52 +0000837
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000838 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +0000839 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
840 return;
841 }
842
Chris Lattner6b6b5372008-06-26 18:38:35 +0000843 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000844
Chandler Carruth87c44602011-07-01 23:49:12 +0000845 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000846 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000847}
848
Chandler Carruth1b03c872011-07-02 00:01:44 +0000849static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000850 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000851 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000852 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000853
Chandler Carruth87c44602011-07-01 23:49:12 +0000854 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000856 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000857 return;
858 }
859
Chandler Carruth87c44602011-07-01 23:49:12 +0000860 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000861}
862
Chandler Carruth1b03c872011-07-02 00:01:44 +0000863static void handleAlwaysInlineAttr(Sema &S, Decl *D,
864 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000865 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000866 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000867 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
868 return;
869 }
870
Chandler Carruth87c44602011-07-01 23:49:12 +0000871 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000873 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000874 return;
875 }
Mike Stumpbf916502009-07-24 19:02:52 +0000876
Chandler Carruth87c44602011-07-01 23:49:12 +0000877 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000878}
879
Chandler Carruth1b03c872011-07-02 00:01:44 +0000880static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000881 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000882 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +0000883 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
884 return;
885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chandler Carruth87c44602011-07-01 23:49:12 +0000887 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000888 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000889 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000890 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000891 return;
892 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000893 }
894
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000895 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000896}
897
Chandler Carruth1b03c872011-07-02 00:01:44 +0000898static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +0000899 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000900 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +0000901 return;
Dan Gohman34c26302010-11-17 00:03:07 +0000902
Chandler Carruth87c44602011-07-01 23:49:12 +0000903 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +0000904}
905
Chandler Carruth1b03c872011-07-02 00:01:44 +0000906static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +0000907 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +0000908 if (isa<VarDecl>(D))
909 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +0000910 else
911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000912 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000913}
914
Chandler Carruth1b03c872011-07-02 00:01:44 +0000915static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +0000916 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +0000917 if (isa<VarDecl>(D))
918 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +0000919 else
920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000921 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000922}
923
Chandler Carruth1b03c872011-07-02 00:01:44 +0000924static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000925 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +0000926
927 if (S.CheckNoReturnAttr(attr)) return;
928
Chandler Carruth87c44602011-07-01 23:49:12 +0000929 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +0000930 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000931 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +0000932 return;
933 }
934
Chandler Carruth87c44602011-07-01 23:49:12 +0000935 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +0000936}
937
938bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +0000939 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +0000940 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
941 attr.setInvalid();
942 return true;
943 }
944
945 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +0000946}
947
Chandler Carruth1b03c872011-07-02 00:01:44 +0000948static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
949 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000950
951 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
952 // because 'analyzer_noreturn' does not impact the type.
953
Chandler Carruth1731e202011-07-11 23:30:35 +0000954 if(!checkAttributeNumArgs(S, Attr, 0))
955 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000956
Chandler Carruth87c44602011-07-01 23:49:12 +0000957 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
958 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000959 if (VD == 0 || (!VD->getType()->isBlockPointerType()
960 && !VD->getType()->isFunctionPointerType())) {
961 S.Diag(Attr.getLoc(),
962 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
963 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000964 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000965 return;
966 }
967 }
968
Chandler Carruth87c44602011-07-01 23:49:12 +0000969 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000970}
971
John Thompson35cc9622010-08-09 21:53:52 +0000972// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +0000973static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +0000974/*
975 Returning a Vector Class in Registers
976
Eric Christopherf48f3672010-12-01 22:13:54 +0000977 According to the PPU ABI specifications, a class with a single member of
978 vector type is returned in memory when used as the return value of a function.
979 This results in inefficient code when implementing vector classes. To return
980 the value in a single vector register, add the vecreturn attribute to the
981 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000982
983 Example:
984
985 struct Vector
986 {
987 __vector float xyzw;
988 } __attribute__((vecreturn));
989
990 Vector Add(Vector lhs, Vector rhs)
991 {
992 Vector result;
993 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
994 return result; // This will be returned in a register
995 }
996*/
Chandler Carruth87c44602011-07-01 23:49:12 +0000997 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000999 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001000 return;
1001 }
1002
Chandler Carruth87c44602011-07-01 23:49:12 +00001003 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001004 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1005 return;
1006 }
1007
Chandler Carruth87c44602011-07-01 23:49:12 +00001008 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001009 int count = 0;
1010
1011 if (!isa<CXXRecordDecl>(record)) {
1012 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1013 return;
1014 }
1015
1016 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1017 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1018 return;
1019 }
1020
Eric Christopherf48f3672010-12-01 22:13:54 +00001021 for (RecordDecl::field_iterator iter = record->field_begin();
1022 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001023 if ((count == 1) || !iter->getType()->isVectorType()) {
1024 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1025 return;
1026 }
1027 count++;
1028 }
1029
Chandler Carruth87c44602011-07-01 23:49:12 +00001030 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001031}
1032
Chandler Carruth1b03c872011-07-02 00:01:44 +00001033static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001034 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001036 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001037 return;
1038 }
1039 // FIXME: Actually store the attribute on the declaration
1040}
1041
Chandler Carruth1b03c872011-07-02 00:01:44 +00001042static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001043 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001044 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001045 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001046 return;
1047 }
Mike Stumpbf916502009-07-24 19:02:52 +00001048
Chandler Carruth87c44602011-07-01 23:49:12 +00001049 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1050 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001052 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001053 return;
1054 }
Mike Stumpbf916502009-07-24 19:02:52 +00001055
Chandler Carruth87c44602011-07-01 23:49:12 +00001056 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001057}
1058
Chandler Carruth1b03c872011-07-02 00:01:44 +00001059static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001060 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001061 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1063 return;
1064 }
Mike Stumpbf916502009-07-24 19:02:52 +00001065
Chandler Carruth87c44602011-07-01 23:49:12 +00001066 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001067 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001068 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1069 return;
1070 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001071 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001072 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001073 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001074 return;
1075 }
Mike Stumpbf916502009-07-24 19:02:52 +00001076
Chandler Carruth87c44602011-07-01 23:49:12 +00001077 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001078}
1079
Chandler Carruth1b03c872011-07-02 00:01:44 +00001080static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001081 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001082 if (Attr.getNumArgs() > 1) {
1083 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001084 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001085 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001086
1087 int priority = 65535; // FIXME: Do not hardcode such constants.
1088 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001089 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001090 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001091 if (E->isTypeDependent() || E->isValueDependent() ||
1092 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001094 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001095 return;
1096 }
1097 priority = Idx.getZExtValue();
1098 }
Mike Stumpbf916502009-07-24 19:02:52 +00001099
Chandler Carruth87c44602011-07-01 23:49:12 +00001100 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001101 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001102 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001103 return;
1104 }
1105
Chandler Carruth87c44602011-07-01 23:49:12 +00001106 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001107 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001108}
1109
Chandler Carruth1b03c872011-07-02 00:01:44 +00001110static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001111 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001112 if (Attr.getNumArgs() > 1) {
1113 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001114 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001115 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001116
1117 int priority = 65535; // FIXME: Do not hardcode such constants.
1118 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001119 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001120 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001121 if (E->isTypeDependent() || E->isValueDependent() ||
1122 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001124 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001125 return;
1126 }
1127 priority = Idx.getZExtValue();
1128 }
Mike Stumpbf916502009-07-24 19:02:52 +00001129
Chandler Carruth87c44602011-07-01 23:49:12 +00001130 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001131 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001132 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001133 return;
1134 }
1135
Chandler Carruth87c44602011-07-01 23:49:12 +00001136 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001137 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001138}
1139
Chandler Carruth1b03c872011-07-02 00:01:44 +00001140static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001141 unsigned NumArgs = Attr.getNumArgs();
1142 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001143 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144 return;
1145 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001146
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001147 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001148 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001149 if (NumArgs == 1) {
1150 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001151 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001152 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1153 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001154 return;
1155 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001156 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001157 }
Mike Stumpbf916502009-07-24 19:02:52 +00001158
Chandler Carruth87c44602011-07-01 23:49:12 +00001159 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001160}
1161
Chandler Carruth1b03c872011-07-02 00:01:44 +00001162static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001163 unsigned NumArgs = Attr.getNumArgs();
1164 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001165 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001166 return;
1167 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001168
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001169 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001170 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001171 if (NumArgs == 1) {
1172 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001173 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001174 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001175 diag::err_attribute_not_string) << "unavailable";
1176 return;
1177 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001178 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001179 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001180 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001181}
1182
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001183static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1184 const AttributeList &Attr) {
1185 unsigned NumArgs = Attr.getNumArgs();
1186 if (NumArgs > 0) {
1187 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1188 return;
1189 }
1190
1191 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1192 Attr.getLoc(), S.Context));
1193}
1194
Chandler Carruth1b03c872011-07-02 00:01:44 +00001195static void handleAvailabilityAttr(Sema &S, Decl *D,
1196 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001197 IdentifierInfo *Platform = Attr.getParameterName();
1198 SourceLocation PlatformLoc = Attr.getParameterLoc();
1199
Chris Lattner5f9e2722011-07-23 10:55:15 +00001200 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001201 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1202 if (PlatformName.empty()) {
1203 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1204 << Platform;
1205
1206 PlatformName = Platform->getName();
1207 }
1208
1209 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1210 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1211 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001212 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001213
1214 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1215 // of these steps are needed).
1216 if (Introduced.isValid() && Deprecated.isValid() &&
1217 !(Introduced.Version < Deprecated.Version)) {
1218 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1219 << 1 << PlatformName << Deprecated.Version.getAsString()
1220 << 0 << Introduced.Version.getAsString();
1221 return;
1222 }
1223
1224 if (Introduced.isValid() && Obsoleted.isValid() &&
1225 !(Introduced.Version < Obsoleted.Version)) {
1226 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1227 << 2 << PlatformName << Obsoleted.Version.getAsString()
1228 << 0 << Introduced.Version.getAsString();
1229 return;
1230 }
1231
1232 if (Deprecated.isValid() && Obsoleted.isValid() &&
1233 !(Deprecated.Version < Obsoleted.Version)) {
1234 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1235 << 2 << PlatformName << Obsoleted.Version.getAsString()
1236 << 1 << Deprecated.Version.getAsString();
1237 return;
1238 }
1239
Chandler Carruth87c44602011-07-01 23:49:12 +00001240 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001241 Platform,
1242 Introduced.Version,
1243 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001244 Obsoleted.Version,
1245 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001246}
1247
Chandler Carruth1b03c872011-07-02 00:01:44 +00001248static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001249 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001250 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001251 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001252
Peter Collingbourne7a730022010-11-23 20:45:58 +00001253 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001254 Arg = Arg->IgnoreParenCasts();
1255 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001256
Douglas Gregor5cee1192011-07-27 05:40:30 +00001257 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001259 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 return;
1261 }
Mike Stumpbf916502009-07-24 19:02:52 +00001262
Chris Lattner5f9e2722011-07-23 10:55:15 +00001263 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001264 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001265
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001266 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001267 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001268 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001269 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001270 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001271 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001272 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001273 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001275 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001276 return;
1277 }
Mike Stumpbf916502009-07-24 19:02:52 +00001278
Chandler Carruth87c44602011-07-01 23:49:12 +00001279 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001280}
1281
Chandler Carruth1b03c872011-07-02 00:01:44 +00001282static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1283 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001284 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1285 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001287 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001288 return;
1289 }
1290
Chandler Carruth87c44602011-07-01 23:49:12 +00001291 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1292 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1293 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001294 << "objc_method_family" << 1;
1295 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001296 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001297 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001298 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001299 return;
1300 }
1301
Chris Lattner5f9e2722011-07-23 10:55:15 +00001302 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001303 ObjCMethodFamilyAttr::FamilyKind family;
1304 if (param == "none")
1305 family = ObjCMethodFamilyAttr::OMF_None;
1306 else if (param == "alloc")
1307 family = ObjCMethodFamilyAttr::OMF_alloc;
1308 else if (param == "copy")
1309 family = ObjCMethodFamilyAttr::OMF_copy;
1310 else if (param == "init")
1311 family = ObjCMethodFamilyAttr::OMF_init;
1312 else if (param == "mutableCopy")
1313 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1314 else if (param == "new")
1315 family = ObjCMethodFamilyAttr::OMF_new;
1316 else {
1317 // Just warn and ignore it. This is future-proof against new
1318 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001319 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001320 return;
1321 }
1322
John McCallf85e1932011-06-15 23:02:42 +00001323 if (family == ObjCMethodFamilyAttr::OMF_init &&
1324 !method->getResultType()->isObjCObjectPointerType()) {
1325 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1326 << method->getResultType();
1327 // Ignore the attribute.
1328 return;
1329 }
1330
Chandler Carruth87c44602011-07-01 23:49:12 +00001331 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCallf85e1932011-06-15 23:02:42 +00001332 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001333}
1334
Chandler Carruth1b03c872011-07-02 00:01:44 +00001335static void handleObjCExceptionAttr(Sema &S, Decl *D,
1336 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001337 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001338 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001339
Chris Lattner0db29ec2009-02-14 08:09:34 +00001340 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1341 if (OCI == 0) {
1342 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1343 return;
1344 }
Mike Stumpbf916502009-07-24 19:02:52 +00001345
Sean Huntcf807c42010-08-18 23:23:40 +00001346 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001347}
1348
Chandler Carruth1b03c872011-07-02 00:01:44 +00001349static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001350 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001352 return;
1353 }
Richard Smith162e1c12011-04-15 14:24:37 +00001354 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001355 QualType T = TD->getUnderlyingType();
1356 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001357 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001358 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1359 return;
1360 }
1361 }
Sean Huntcf807c42010-08-18 23:23:40 +00001362 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001363}
1364
Mike Stumpbf916502009-07-24 19:02:52 +00001365static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001366handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001367 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001368 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001369 return;
1370 }
1371
1372 if (!isa<FunctionDecl>(D)) {
1373 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1374 return;
1375 }
1376
Sean Huntcf807c42010-08-18 23:23:40 +00001377 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001378}
1379
Chandler Carruth1b03c872011-07-02 00:01:44 +00001380static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001381 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001382 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001383 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001384 return;
1385 }
Mike Stumpbf916502009-07-24 19:02:52 +00001386
Steve Naroff9eae5762008-09-18 16:44:58 +00001387 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001389 return;
1390 }
Mike Stumpbf916502009-07-24 19:02:52 +00001391
Sean Huntcf807c42010-08-18 23:23:40 +00001392 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001393 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001394 type = BlocksAttr::ByRef;
1395 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001396 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001397 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001398 return;
1399 }
Mike Stumpbf916502009-07-24 19:02:52 +00001400
Chandler Carruth87c44602011-07-01 23:49:12 +00001401 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001402}
1403
Chandler Carruth1b03c872011-07-02 00:01:44 +00001404static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001405 // check the attribute arguments.
1406 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001407 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001408 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001409 }
1410
Anders Carlsson77091822008-10-05 18:05:59 +00001411 int sentinel = 0;
1412 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001413 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001414 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001415 if (E->isTypeDependent() || E->isValueDependent() ||
1416 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001417 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001418 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001419 return;
1420 }
1421 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001422
Anders Carlsson77091822008-10-05 18:05:59 +00001423 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001424 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1425 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001426 return;
1427 }
1428 }
1429
1430 int nullPos = 0;
1431 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001432 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001433 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001434 if (E->isTypeDependent() || E->isValueDependent() ||
1435 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001436 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001437 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001438 return;
1439 }
1440 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001441
Anders Carlsson77091822008-10-05 18:05:59 +00001442 if (nullPos > 1 || nullPos < 0) {
1443 // FIXME: This error message could be improved, it would be nice
1444 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001445 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1446 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001447 return;
1448 }
1449 }
1450
Chandler Carruth87c44602011-07-01 23:49:12 +00001451 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall183700f2009-09-21 23:43:11 +00001452 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001453 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001454
Chris Lattner897cd902009-03-17 23:03:47 +00001455 if (isa<FunctionNoProtoType>(FT)) {
1456 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1457 return;
1458 }
Mike Stumpbf916502009-07-24 19:02:52 +00001459
Chris Lattner897cd902009-03-17 23:03:47 +00001460 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001461 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001462 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001463 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001464 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001465 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001466 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001467 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001468 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001469 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001470 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1471 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001472 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001473 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001474 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001475 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001476 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001477 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001478 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001479 int m = Ty->isFunctionPointerType() ? 0 : 1;
1480 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001481 return;
1482 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001483 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001484 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001485 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001486 return;
1487 }
Anders Carlsson77091822008-10-05 18:05:59 +00001488 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001489 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001490 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001491 return;
1492 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001493 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001494 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001495}
1496
Chandler Carruth1b03c872011-07-02 00:01:44 +00001497static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001498 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001499 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001500 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001501
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001502 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001503 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001504 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001505 return;
1506 }
Mike Stumpbf916502009-07-24 19:02:52 +00001507
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001508 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1509 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1510 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001511 return;
1512 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001513 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1514 if (MD->getResultType()->isVoidType()) {
1515 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1516 << Attr.getName() << 1;
1517 return;
1518 }
1519
Sean Huntcf807c42010-08-18 23:23:40 +00001520 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001521}
1522
Chandler Carruth1b03c872011-07-02 00:01:44 +00001523static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001524 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001525 if (Attr.hasParameterOrArguments()) {
1526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001527 return;
1528 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001529
Chandler Carruth87c44602011-07-01 23:49:12 +00001530 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1532 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001533 return;
1534 }
1535
Chandler Carruth87c44602011-07-01 23:49:12 +00001536 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001537
1538 // 'weak' only applies to declarations with external linkage.
1539 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001540 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001541 return;
1542 }
Mike Stumpbf916502009-07-24 19:02:52 +00001543
Chandler Carruth87c44602011-07-01 23:49:12 +00001544 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001545}
1546
Chandler Carruth1b03c872011-07-02 00:01:44 +00001547static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001548 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001549 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001550 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001551
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001552
1553 // weak_import only applies to variable & function declarations.
1554 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001555 if (!D->canBeWeakImported(isDef)) {
1556 if (isDef)
1557 S.Diag(Attr.getLoc(),
1558 diag::warn_attribute_weak_import_invalid_on_definition)
1559 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001560 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001561 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001562 isa<ObjCInterfaceDecl>(D))) {
1563 // Nothing to warn about here.
1564 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001565 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001566 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001567
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001568 return;
1569 }
1570
Sean Huntcf807c42010-08-18 23:23:40 +00001571 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001572}
1573
Chandler Carruth1b03c872011-07-02 00:01:44 +00001574static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1575 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001576 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001577 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001578 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001579
1580 unsigned WGSize[3];
1581 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001582 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001583 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001584 if (E->isTypeDependent() || E->isValueDependent() ||
1585 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1587 << "reqd_work_group_size" << E->getSourceRange();
1588 return;
1589 }
1590 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1591 }
Sean Huntcf807c42010-08-18 23:23:40 +00001592 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1593 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001594 WGSize[2]));
1595}
1596
Chandler Carruth1b03c872011-07-02 00:01:44 +00001597static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001598 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001599 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001600 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001601
1602 // Make sure that there is a string literal as the sections's single
1603 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001604 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001605 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001606 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001607 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001608 return;
1609 }
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Chris Lattner797c3c42009-08-10 19:03:04 +00001611 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001612 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001613 if (!Error.empty()) {
1614 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1615 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001616 return;
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001619 // This attribute cannot be applied to local variables.
1620 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1621 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1622 return;
1623 }
1624
Eric Christopherf48f3672010-12-01 22:13:54 +00001625 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1626 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001627}
1628
Chris Lattner6b6b5372008-06-26 18:38:35 +00001629
Chandler Carruth1b03c872011-07-02 00:01:44 +00001630static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001631 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001632 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001634 return;
1635 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001636
Chandler Carruth87c44602011-07-01 23:49:12 +00001637 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001638 if (Existing->getLocation().isInvalid())
1639 Existing->setLocation(Attr.getLoc());
1640 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001641 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001642 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001643}
1644
Chandler Carruth1b03c872011-07-02 00:01:44 +00001645static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001646 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001647 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001649 return;
1650 }
Mike Stumpbf916502009-07-24 19:02:52 +00001651
Chandler Carruth87c44602011-07-01 23:49:12 +00001652 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001653 if (Existing->getLocation().isInvalid())
1654 Existing->setLocation(Attr.getLoc());
1655 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001656 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001657 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001658}
1659
Chandler Carruth1b03c872011-07-02 00:01:44 +00001660static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001661 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001662 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001663 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001664
Chandler Carruth87c44602011-07-01 23:49:12 +00001665 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001666}
1667
Chandler Carruth1b03c872011-07-02 00:01:44 +00001668static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001669 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1671 return;
1672 }
Mike Stumpbf916502009-07-24 19:02:52 +00001673
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001674 if (Attr.getNumArgs() != 0) {
1675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1676 return;
1677 }
Mike Stumpbf916502009-07-24 19:02:52 +00001678
Chandler Carruth87c44602011-07-01 23:49:12 +00001679 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00001680
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001681 if (!VD || !VD->hasLocalStorage()) {
1682 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1683 return;
1684 }
Mike Stumpbf916502009-07-24 19:02:52 +00001685
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001686 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001687 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001688 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001689 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1690 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001691 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001692 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001693 Attr.getParameterName();
1694 return;
1695 }
Mike Stumpbf916502009-07-24 19:02:52 +00001696
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001697 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1698 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001699 S.Diag(Attr.getParameterLoc(),
1700 diag::err_attribute_cleanup_arg_not_function)
1701 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001702 return;
1703 }
1704
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001705 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001706 S.Diag(Attr.getParameterLoc(),
1707 diag::err_attribute_cleanup_func_must_take_one_arg)
1708 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001709 return;
1710 }
Mike Stumpbf916502009-07-24 19:02:52 +00001711
Anders Carlsson89941c12009-02-07 23:16:50 +00001712 // We're currently more strict than GCC about what function types we accept.
1713 // If this ever proves to be a problem it should be easy to fix.
1714 QualType Ty = S.Context.getPointerType(VD->getType());
1715 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001716 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1717 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001718 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001719 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1720 Attr.getParameterName() << ParamTy << Ty;
1721 return;
1722 }
Mike Stumpbf916502009-07-24 19:02:52 +00001723
Chandler Carruth87c44602011-07-01 23:49:12 +00001724 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001725 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001726}
1727
Mike Stumpbf916502009-07-24 19:02:52 +00001728/// Handle __attribute__((format_arg((idx)))) attribute based on
1729/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001730static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001731 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001732 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001733
Chandler Carruth87c44602011-07-01 23:49:12 +00001734 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001735 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001736 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001737 return;
1738 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001739
1740 // In C++ the implicit 'this' function parameter also counts, and they are
1741 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001742 bool HasImplicitThisParam = isInstanceMethod(D);
1743 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001744 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001745
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001746 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001747 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001748 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001749 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1750 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001751 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1752 << "format" << 2 << IdxExpr->getSourceRange();
1753 return;
1754 }
Mike Stumpbf916502009-07-24 19:02:52 +00001755
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001756 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1757 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1758 << "format" << 2 << IdxExpr->getSourceRange();
1759 return;
1760 }
Mike Stumpbf916502009-07-24 19:02:52 +00001761
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001762 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001763
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001764 if (HasImplicitThisParam) {
1765 if (ArgIdx == 0) {
1766 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1767 << "format_arg" << IdxExpr->getSourceRange();
1768 return;
1769 }
1770 ArgIdx--;
1771 }
1772
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001773 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001774 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001775
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001776 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1777 if (not_nsstring_type &&
1778 !isCFStringType(Ty, S.Context) &&
1779 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001780 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001781 // FIXME: Should highlight the actual expression that has the wrong type.
1782 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001783 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001784 << IdxExpr->getSourceRange();
1785 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001786 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001787 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001788 if (!isNSStringType(Ty, S.Context) &&
1789 !isCFStringType(Ty, S.Context) &&
1790 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001791 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001792 // FIXME: Should highlight the actual expression that has the wrong type.
1793 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001794 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001795 << IdxExpr->getSourceRange();
1796 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001797 }
1798
Chandler Carruth87c44602011-07-01 23:49:12 +00001799 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001800 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001801}
1802
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001803enum FormatAttrKind {
1804 CFStringFormat,
1805 NSStringFormat,
1806 StrftimeFormat,
1807 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001808 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001809 InvalidFormat
1810};
1811
1812/// getFormatAttrKind - Map from format attribute names to supported format
1813/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001814static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001815 // Check for formats that get handled specially.
1816 if (Format == "NSString")
1817 return NSStringFormat;
1818 if (Format == "CFString")
1819 return CFStringFormat;
1820 if (Format == "strftime")
1821 return StrftimeFormat;
1822
1823 // Otherwise, check for supported formats.
1824 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1825 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1826 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001827 Format == "zcmn_err" ||
1828 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001829 return SupportedFormat;
1830
Duncan Sandsbc525952010-03-23 14:44:19 +00001831 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1832 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001833 return IgnoredFormat;
1834
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001835 return InvalidFormat;
1836}
1837
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001838/// Handle __attribute__((init_priority(priority))) attributes based on
1839/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001840static void handleInitPriorityAttr(Sema &S, Decl *D,
1841 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001842 if (!S.getLangOptions().CPlusPlus) {
1843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1844 return;
1845 }
1846
Chandler Carruth87c44602011-07-01 23:49:12 +00001847 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001848 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1849 Attr.setInvalid();
1850 return;
1851 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001853 if (S.Context.getAsArrayType(T))
1854 T = S.Context.getBaseElementType(T);
1855 if (!T->getAs<RecordType>()) {
1856 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1857 Attr.setInvalid();
1858 return;
1859 }
1860
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001861 if (Attr.getNumArgs() != 1) {
1862 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1863 Attr.setInvalid();
1864 return;
1865 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001866 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001867
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001868 llvm::APSInt priority(32);
1869 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1870 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1871 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1872 << "init_priority" << priorityExpr->getSourceRange();
1873 Attr.setInvalid();
1874 return;
1875 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001876 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001877 if (prioritynum < 101 || prioritynum > 65535) {
1878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1879 << priorityExpr->getSourceRange();
1880 Attr.setInvalid();
1881 return;
1882 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001883 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001884 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001885}
1886
Mike Stumpbf916502009-07-24 19:02:52 +00001887/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1888/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001889static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001890
Chris Lattner545dd342008-06-28 23:36:30 +00001891 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001892 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001893 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001894 return;
1895 }
1896
Chris Lattner545dd342008-06-28 23:36:30 +00001897 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001899 return;
1900 }
1901
Chandler Carruth87c44602011-07-01 23:49:12 +00001902 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001903 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001904 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001905 return;
1906 }
1907
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001908 // In C++ the implicit 'this' function parameter also counts, and they are
1909 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001910 bool HasImplicitThisParam = isInstanceMethod(D);
1911 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001912 unsigned FirstIdx = 1;
1913
Chris Lattner5f9e2722011-07-23 10:55:15 +00001914 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001915
1916 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001917 if (Format.startswith("__") && Format.endswith("__"))
1918 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001919
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001920 // Check for supported formats.
1921 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001922
1923 if (Kind == IgnoredFormat)
1924 return;
1925
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001926 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001927 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001928 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001929 return;
1930 }
1931
1932 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001933 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001934 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001935 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1936 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001937 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001938 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001939 return;
1940 }
1941
1942 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001943 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001944 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001945 return;
1946 }
1947
1948 // FIXME: Do we need to bounds check?
1949 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001950
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001951 if (HasImplicitThisParam) {
1952 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001953 S.Diag(Attr.getLoc(),
1954 diag::err_format_attribute_implicit_this_format_string)
1955 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001956 return;
1957 }
1958 ArgIdx--;
1959 }
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Chris Lattner6b6b5372008-06-26 18:38:35 +00001961 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001962 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001963
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001964 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001965 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001966 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1967 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001968 return;
1969 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001970 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001971 // FIXME: do we need to check if the type is NSString*? What are the
1972 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001973 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001974 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001975 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1976 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001977 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001978 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001979 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001980 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001981 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001982 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1983 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001984 return;
1985 }
1986
1987 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001988 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001989 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001990 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1991 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001993 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001994 return;
1995 }
1996
1997 // check if the function is variadic if the 3rd argument non-zero
1998 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001999 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002000 ++NumArgs; // +1 for ...
2001 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002002 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002003 return;
2004 }
2005 }
2006
Chris Lattner3c73c412008-11-19 08:23:25 +00002007 // strftime requires FirstArg to be 0 because it doesn't read from any
2008 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002009 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002010 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002011 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2012 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002013 return;
2014 }
2015 // if 0 it disables parameter checking (to use with e.g. va_list)
2016 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002018 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002019 return;
2020 }
2021
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002022 // Check whether we already have an equivalent format attribute.
2023 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002024 i = D->specific_attr_begin<FormatAttr>(),
2025 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002026 i != e ; ++i) {
2027 FormatAttr *f = *i;
2028 if (f->getType() == Format &&
2029 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2030 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2031 // If we don't have a valid location for this attribute, adopt the
2032 // location.
2033 if (f->getLocation().isInvalid())
2034 f->setLocation(Attr.getLoc());
2035 return;
2036 }
2037 }
2038
Chandler Carruth87c44602011-07-01 23:49:12 +00002039 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002040 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002041 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002042}
2043
Chandler Carruth1b03c872011-07-02 00:01:44 +00002044static void handleTransparentUnionAttr(Sema &S, Decl *D,
2045 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002046 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002047 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002048 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002049
Chris Lattner6b6b5372008-06-26 18:38:35 +00002050
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002051 // Try to find the underlying union declaration.
2052 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002053 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002054 if (TD && TD->getUnderlyingType()->isUnionType())
2055 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2056 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002057 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002058
2059 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002061 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002062 return;
2063 }
2064
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002065 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002066 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002067 diag::warn_transparent_union_attribute_not_definition);
2068 return;
2069 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002070
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002071 RecordDecl::field_iterator Field = RD->field_begin(),
2072 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002073 if (Field == FieldEnd) {
2074 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2075 return;
2076 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002077
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002078 FieldDecl *FirstField = *Field;
2079 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002080 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002081 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002082 diag::warn_transparent_union_attribute_floating)
2083 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002084 return;
2085 }
2086
2087 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2088 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2089 for (; Field != FieldEnd; ++Field) {
2090 QualType FieldType = Field->getType();
2091 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2092 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2093 // Warn if we drop the attribute.
2094 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002095 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002096 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002097 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002098 diag::warn_transparent_union_attribute_field_size_align)
2099 << isSize << Field->getDeclName() << FieldBits;
2100 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002101 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002102 diag::note_transparent_union_first_field_size_align)
2103 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002104 return;
2105 }
2106 }
2107
Sean Huntcf807c42010-08-18 23:23:40 +00002108 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002109}
2110
Chandler Carruth1b03c872011-07-02 00:01:44 +00002111static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002112 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002113 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002114 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002115
Peter Collingbourne7a730022010-11-23 20:45:58 +00002116 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002117 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002118
Chris Lattner6b6b5372008-06-26 18:38:35 +00002119 // Make sure that there is a string literal as the annotation's single
2120 // argument.
2121 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002122 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002123 return;
2124 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002125 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002126 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002127}
2128
Chandler Carruth1b03c872011-07-02 00:01:44 +00002129static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002130 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002131 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002133 return;
2134 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002135
2136 //FIXME: The C++0x version of this attribute has more limited applicabilty
2137 // than GNU's, and should error out when it is used to specify a
2138 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002139
Chris Lattner545dd342008-06-28 23:36:30 +00002140 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002141 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002142 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002143 }
Mike Stumpbf916502009-07-24 19:02:52 +00002144
Peter Collingbourne7a730022010-11-23 20:45:58 +00002145 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002146}
2147
2148void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2149 if (E->isTypeDependent() || E->isValueDependent()) {
2150 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002151 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002152 return;
2153 }
2154
Sean Huntcf807c42010-08-18 23:23:40 +00002155 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002156 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002157 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2158 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2159 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002160 return;
2161 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002162 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002163 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2164 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002165 return;
2166 }
2167
Sean Huntcf807c42010-08-18 23:23:40 +00002168 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2169}
2170
2171void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2172 // FIXME: Cache the number on the Attr object if non-dependent?
2173 // FIXME: Perform checking of type validity
2174 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2175 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002176}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002177
Chandler Carruthd309c812011-07-01 23:49:16 +00002178/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002179/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002180///
Mike Stumpbf916502009-07-24 19:02:52 +00002181/// Despite what would be logical, the mode attribute is a decl attribute, not a
2182/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2183/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002184static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002185 // This attribute isn't documented, but glibc uses it. It changes
2186 // the width of an int or unsigned int to the specified size.
2187
2188 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002189 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002190 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002191
Chris Lattnerfbf13472008-06-27 22:18:37 +00002192
2193 IdentifierInfo *Name = Attr.getParameterName();
2194 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002195 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002196 return;
2197 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002198
Chris Lattner5f9e2722011-07-23 10:55:15 +00002199 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002200
2201 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002202 if (Str.startswith("__") && Str.endswith("__"))
2203 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002204
2205 unsigned DestWidth = 0;
2206 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002207 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002208 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002209 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002210 switch (Str[0]) {
2211 case 'Q': DestWidth = 8; break;
2212 case 'H': DestWidth = 16; break;
2213 case 'S': DestWidth = 32; break;
2214 case 'D': DestWidth = 64; break;
2215 case 'X': DestWidth = 96; break;
2216 case 'T': DestWidth = 128; break;
2217 }
2218 if (Str[1] == 'F') {
2219 IntegerMode = false;
2220 } else if (Str[1] == 'C') {
2221 IntegerMode = false;
2222 ComplexMode = true;
2223 } else if (Str[1] != 'I') {
2224 DestWidth = 0;
2225 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002226 break;
2227 case 4:
2228 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2229 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002230 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002231 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002232 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002233 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002234 break;
2235 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002236 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002237 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002238 break;
2239 }
2240
2241 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002242 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002243 OldTy = TD->getUnderlyingType();
2244 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2245 OldTy = VD->getType();
2246 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002247 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2248 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002249 return;
2250 }
Eli Friedman73397492009-03-03 06:41:03 +00002251
John McCall183700f2009-09-21 23:43:11 +00002252 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002253 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2254 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002255 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002256 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2257 } else if (ComplexMode) {
2258 if (!OldTy->isComplexType())
2259 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2260 } else {
2261 if (!OldTy->isFloatingType())
2262 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2263 }
2264
Mike Stump390b4cc2009-05-16 07:39:55 +00002265 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2266 // and friends, at least with glibc.
2267 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2268 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002269 // FIXME: Make sure floating-point mappings are accurate
2270 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002271 QualType NewTy;
2272 switch (DestWidth) {
2273 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002274 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002275 return;
2276 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002277 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002278 return;
2279 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002280 if (!IntegerMode) {
2281 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2282 return;
2283 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002284 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002285 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002286 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002287 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002288 break;
2289 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002290 if (!IntegerMode) {
2291 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2292 return;
2293 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002294 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002295 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002296 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002297 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002298 break;
2299 case 32:
2300 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002301 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002302 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002303 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002304 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002305 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002306 break;
2307 case 64:
2308 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002309 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002310 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002311 if (S.Context.Target.getLongWidth() == 64)
2312 NewTy = S.Context.LongTy;
2313 else
2314 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002315 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002316 if (S.Context.Target.getLongWidth() == 64)
2317 NewTy = S.Context.UnsignedLongTy;
2318 else
2319 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002320 break;
Eli Friedman73397492009-03-03 06:41:03 +00002321 case 96:
2322 NewTy = S.Context.LongDoubleTy;
2323 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002324 case 128:
2325 if (!IntegerMode) {
2326 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2327 return;
2328 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002329 if (OldTy->isSignedIntegerType())
2330 NewTy = S.Context.Int128Ty;
2331 else
2332 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002333 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002334 }
2335
Eli Friedman73397492009-03-03 06:41:03 +00002336 if (ComplexMode) {
2337 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002338 }
2339
2340 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002341 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002342 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002343 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002344 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002345 cast<ValueDecl>(D)->setType(NewTy);
2346}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002347
Chandler Carruth1b03c872011-07-02 00:01:44 +00002348static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002349 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002350 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002351 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002352
Chandler Carruth87c44602011-07-01 23:49:12 +00002353 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002355 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002356 return;
2357 }
Mike Stumpbf916502009-07-24 19:02:52 +00002358
Chandler Carruth87c44602011-07-01 23:49:12 +00002359 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002360}
2361
Chandler Carruth1b03c872011-07-02 00:01:44 +00002362static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002363 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002364 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002365 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002366
Mike Stumpbf916502009-07-24 19:02:52 +00002367
Chandler Carruth87c44602011-07-01 23:49:12 +00002368 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002370 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002371 return;
2372 }
Mike Stumpbf916502009-07-24 19:02:52 +00002373
Chandler Carruth87c44602011-07-01 23:49:12 +00002374 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002375}
2376
Chandler Carruth1b03c872011-07-02 00:01:44 +00002377static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2378 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002379 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002380 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002381 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002382
Chris Lattner7255a2d2010-06-22 00:03:40 +00002383
Chandler Carruth87c44602011-07-01 23:49:12 +00002384 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002385 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002386 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002387 return;
2388 }
2389
Chandler Carruth87c44602011-07-01 23:49:12 +00002390 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002391 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002392}
2393
Chandler Carruth1b03c872011-07-02 00:01:44 +00002394static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002395 if (S.LangOpts.CUDA) {
2396 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002397 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2399 return;
2400 }
2401
Chandler Carruth87c44602011-07-01 23:49:12 +00002402 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002403 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002404 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002405 return;
2406 }
2407
Chandler Carruth87c44602011-07-01 23:49:12 +00002408 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002409 } else {
2410 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2411 }
2412}
2413
Chandler Carruth1b03c872011-07-02 00:01:44 +00002414static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002415 if (S.LangOpts.CUDA) {
2416 // check the attribute arguments.
2417 if (Attr.getNumArgs() != 0) {
2418 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2419 return;
2420 }
2421
Chandler Carruth87c44602011-07-01 23:49:12 +00002422 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002423 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002424 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002425 return;
2426 }
2427
Chandler Carruth87c44602011-07-01 23:49:12 +00002428 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002429 } else {
2430 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2431 }
2432}
2433
Chandler Carruth1b03c872011-07-02 00:01:44 +00002434static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002435 if (S.LangOpts.CUDA) {
2436 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002437 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002438 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002439
Chandler Carruth87c44602011-07-01 23:49:12 +00002440 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002441 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002442 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002443 return;
2444 }
2445
Chandler Carruth87c44602011-07-01 23:49:12 +00002446 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002447 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002448 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002449 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2450 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2451 << FD->getType()
2452 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2453 "void");
2454 } else {
2455 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2456 << FD->getType();
2457 }
2458 return;
2459 }
2460
Chandler Carruth87c44602011-07-01 23:49:12 +00002461 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002462 } else {
2463 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2464 }
2465}
2466
Chandler Carruth1b03c872011-07-02 00:01:44 +00002467static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002468 if (S.LangOpts.CUDA) {
2469 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002470 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002471 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002472
Peter Collingbourneced76712010-12-01 03:15:31 +00002473
Chandler Carruth87c44602011-07-01 23:49:12 +00002474 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002475 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002476 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002477 return;
2478 }
2479
Chandler Carruth87c44602011-07-01 23:49:12 +00002480 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002481 } else {
2482 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2483 }
2484}
2485
Chandler Carruth1b03c872011-07-02 00:01:44 +00002486static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002487 if (S.LangOpts.CUDA) {
2488 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002489 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002490 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002491
Peter Collingbourneced76712010-12-01 03:15:31 +00002492
Chandler Carruth87c44602011-07-01 23:49:12 +00002493 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002494 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002495 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002496 return;
2497 }
2498
Chandler Carruth87c44602011-07-01 23:49:12 +00002499 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002500 } else {
2501 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2502 }
2503}
2504
Chandler Carruth1b03c872011-07-02 00:01:44 +00002505static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002506 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002507 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002508 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002509
Chandler Carruth87c44602011-07-01 23:49:12 +00002510 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002511 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002513 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002514 return;
2515 }
Mike Stumpbf916502009-07-24 19:02:52 +00002516
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002517 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002518 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002519 return;
2520 }
Mike Stumpbf916502009-07-24 19:02:52 +00002521
Chandler Carruth87c44602011-07-01 23:49:12 +00002522 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002523}
2524
Chandler Carruth1b03c872011-07-02 00:01:44 +00002525static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002526 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002527
Chandler Carruth87c44602011-07-01 23:49:12 +00002528 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002529 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2530 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002531 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002532 return;
2533
Chandler Carruth87c44602011-07-01 23:49:12 +00002534 if (!isa<ObjCMethodDecl>(D)) {
2535 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2536 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002537 return;
2538 }
2539
Chandler Carruth87c44602011-07-01 23:49:12 +00002540 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002541 case AttributeList::AT_fastcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002542 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002543 return;
2544 case AttributeList::AT_stdcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002545 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002546 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002547 case AttributeList::AT_thiscall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002548 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002549 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002550 case AttributeList::AT_cdecl:
Chandler Carruth87c44602011-07-01 23:49:12 +00002551 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002552 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002553 case AttributeList::AT_pascal:
Chandler Carruth87c44602011-07-01 23:49:12 +00002554 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002555 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002556 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002557 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002558 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002559 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002561 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002562 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002563 return;
2564 }
2565
Chris Lattner5f9e2722011-07-23 10:55:15 +00002566 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002567 PcsAttr::PCSType PCS;
2568 if (StrRef == "aapcs")
2569 PCS = PcsAttr::AAPCS;
2570 else if (StrRef == "aapcs-vfp")
2571 PCS = PcsAttr::AAPCS_VFP;
2572 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002573 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2574 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002575 return;
2576 }
2577
Chandler Carruth87c44602011-07-01 23:49:12 +00002578 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002579 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002580 default:
2581 llvm_unreachable("unexpected attribute kind");
2582 return;
2583 }
2584}
2585
Chandler Carruth1b03c872011-07-02 00:01:44 +00002586static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00002587 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00002588 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002589}
2590
John McCall711c52b2011-01-05 12:14:39 +00002591bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2592 if (attr.isInvalid())
2593 return true;
2594
Ted Kremenek831efae2011-04-15 05:49:29 +00002595 if ((attr.getNumArgs() != 0 &&
2596 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2597 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002598 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2599 attr.setInvalid();
2600 return true;
2601 }
2602
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002603 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2604 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002605 switch (attr.getKind()) {
2606 case AttributeList::AT_cdecl: CC = CC_C; break;
2607 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2608 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2609 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2610 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002611 case AttributeList::AT_pcs: {
2612 Expr *Arg = attr.getArg(0);
2613 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002614 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002615 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2616 << "pcs" << 1;
2617 attr.setInvalid();
2618 return true;
2619 }
2620
Chris Lattner5f9e2722011-07-23 10:55:15 +00002621 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002622 if (StrRef == "aapcs") {
2623 CC = CC_AAPCS;
2624 break;
2625 } else if (StrRef == "aapcs-vfp") {
2626 CC = CC_AAPCS_VFP;
2627 break;
2628 }
2629 // FALLS THROUGH
2630 }
John McCall711c52b2011-01-05 12:14:39 +00002631 default: llvm_unreachable("unexpected attribute kind"); return true;
2632 }
2633
2634 return false;
2635}
2636
Chandler Carruth1b03c872011-07-02 00:01:44 +00002637static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002638 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00002639
2640 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00002641 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00002642 return;
2643
Chandler Carruth87c44602011-07-01 23:49:12 +00002644 if (!isa<ObjCMethodDecl>(D)) {
2645 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2646 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002647 return;
2648 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002649
Chandler Carruth87c44602011-07-01 23:49:12 +00002650 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00002651}
2652
2653/// Checks a regparm attribute, returning true if it is ill-formed and
2654/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00002655bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2656 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00002657 return true;
2658
Chandler Carruth87c44602011-07-01 23:49:12 +00002659 if (Attr.getNumArgs() != 1) {
2660 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2661 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002662 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002663 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002664
Chandler Carruth87c44602011-07-01 23:49:12 +00002665 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002666 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002667 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002668 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002669 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002670 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002671 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002672 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002673 }
2674
John McCall711c52b2011-01-05 12:14:39 +00002675 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002676 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002677 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002678 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002679 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002680 }
2681
John McCall711c52b2011-01-05 12:14:39 +00002682 numParams = NumParams.getZExtValue();
2683 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002684 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall711c52b2011-01-05 12:14:39 +00002685 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002686 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002687 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002688 }
2689
John McCall711c52b2011-01-05 12:14:39 +00002690 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002691}
2692
Chandler Carruth1b03c872011-07-02 00:01:44 +00002693static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00002694 if (S.LangOpts.CUDA) {
2695 // check the attribute arguments.
2696 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002697 // FIXME: 0 is not okay.
2698 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002699 return;
2700 }
2701
Chandler Carruth87c44602011-07-01 23:49:12 +00002702 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00002703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002704 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002705 return;
2706 }
2707
2708 Expr *MaxThreadsExpr = Attr.getArg(0);
2709 llvm::APSInt MaxThreads(32);
2710 if (MaxThreadsExpr->isTypeDependent() ||
2711 MaxThreadsExpr->isValueDependent() ||
2712 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2713 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2714 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2715 return;
2716 }
2717
2718 llvm::APSInt MinBlocks(32);
2719 if (Attr.getNumArgs() > 1) {
2720 Expr *MinBlocksExpr = Attr.getArg(1);
2721 if (MinBlocksExpr->isTypeDependent() ||
2722 MinBlocksExpr->isValueDependent() ||
2723 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2724 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2725 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2726 return;
2727 }
2728 }
2729
Chandler Carruth87c44602011-07-01 23:49:12 +00002730 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00002731 MaxThreads.getZExtValue(),
2732 MinBlocks.getZExtValue()));
2733 } else {
2734 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2735 }
2736}
2737
Chris Lattner0744e5f2008-06-29 00:23:49 +00002738//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002739// Checker-specific attribute handlers.
2740//===----------------------------------------------------------------------===//
2741
John McCallc7ad3812011-01-25 03:31:58 +00002742static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2743 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2744}
2745static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2746 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2747}
2748
Chandler Carruth1b03c872011-07-02 00:01:44 +00002749static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002750 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00002751 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002752 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2753 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002754 return;
2755 }
2756
2757 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002758 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00002759 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2760 cf = false;
2761 } else {
2762 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2763 cf = true;
2764 }
2765
2766 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002767 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2768 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00002769 return;
2770 }
2771
2772 if (cf)
Chandler Carruth87c44602011-07-01 23:49:12 +00002773 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002774 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002775 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002776}
2777
Chandler Carruth1b03c872011-07-02 00:01:44 +00002778static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
2779 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002780 if (!isa<ObjCMethodDecl>(D)) {
2781 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2782 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002783 return;
2784 }
2785
Chandler Carruth87c44602011-07-01 23:49:12 +00002786 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002787}
2788
Chandler Carruth1b03c872011-07-02 00:01:44 +00002789static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
2790 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002791
John McCallc7ad3812011-01-25 03:31:58 +00002792 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002793
Chandler Carruth87c44602011-07-01 23:49:12 +00002794 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002795 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002796 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00002797 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002798 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2799 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00002800 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00002801 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002802 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002803 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002804 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2805 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002806 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002807 return;
2808 }
Mike Stumpbf916502009-07-24 19:02:52 +00002809
John McCallc7ad3812011-01-25 03:31:58 +00002810 bool typeOK;
2811 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002812 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00002813 default: llvm_unreachable("invalid ownership attribute"); return;
2814 case AttributeList::AT_ns_returns_autoreleased:
2815 case AttributeList::AT_ns_returns_retained:
2816 case AttributeList::AT_ns_returns_not_retained:
2817 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2818 cf = false;
2819 break;
2820
2821 case AttributeList::AT_cf_returns_retained:
2822 case AttributeList::AT_cf_returns_not_retained:
2823 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2824 cf = true;
2825 break;
2826 }
2827
2828 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002829 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2830 << SourceRange(Attr.getLoc())
2831 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002832 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002833 }
Mike Stumpbf916502009-07-24 19:02:52 +00002834
Chandler Carruth87c44602011-07-01 23:49:12 +00002835 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002836 default:
2837 assert(0 && "invalid ownership attribute");
2838 return;
John McCallc7ad3812011-01-25 03:31:58 +00002839 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruth87c44602011-07-01 23:49:12 +00002840 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCallc7ad3812011-01-25 03:31:58 +00002841 S.Context));
2842 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002843 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002844 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002845 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002846 return;
2847 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002848 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002849 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002850 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002851 case AttributeList::AT_cf_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002852 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002853 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002854 return;
2855 case AttributeList::AT_ns_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00002856 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002857 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002858 return;
2859 };
2860}
2861
John McCalldc7c5ad2011-07-22 08:53:00 +00002862static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
2863 const AttributeList &attr) {
2864 SourceLocation loc = attr.getLoc();
2865
2866 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
2867
2868 if (!isa<ObjCMethodDecl>(method)) {
2869 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
2870 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
2871 return;
2872 }
2873
2874 // Check that the method returns a normal pointer.
2875 QualType resultType = method->getResultType();
2876 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
2877 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2878 << SourceRange(loc)
2879 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
2880
2881 // Drop the attribute.
2882 return;
2883 }
2884
2885 method->addAttr(
2886 ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context));
2887}
2888
Chandler Carruth1b03c872011-07-02 00:01:44 +00002889static void handleObjCOwnershipAttr(Sema &S, Decl *D,
2890 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002891 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00002892
Chandler Carruth87c44602011-07-01 23:49:12 +00002893 SourceLocation L = Attr.getLoc();
2894 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2895 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00002896}
2897
Chandler Carruth1b03c872011-07-02 00:01:44 +00002898static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
2899 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002900 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
2901 SourceLocation L = Attr.getLoc();
2902 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2903 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00002904 return;
2905 }
2906
Chandler Carruth87c44602011-07-01 23:49:12 +00002907 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00002908 QualType type = vd->getType();
2909
2910 if (!type->isDependentType() &&
2911 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002912 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00002913 << type;
2914 return;
2915 }
2916
2917 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
2918
2919 // If we have no lifetime yet, check the lifetime we're presumably
2920 // going to infer.
2921 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
2922 lifetime = type->getObjCARCImplicitLifetime();
2923
2924 switch (lifetime) {
2925 case Qualifiers::OCL_None:
2926 assert(type->isDependentType() &&
2927 "didn't infer lifetime for non-dependent type?");
2928 break;
2929
2930 case Qualifiers::OCL_Weak: // meaningful
2931 case Qualifiers::OCL_Strong: // meaningful
2932 break;
2933
2934 case Qualifiers::OCL_ExplicitNone:
2935 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00002936 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00002937 << (lifetime == Qualifiers::OCL_Autoreleasing);
2938 break;
2939 }
2940
Chandler Carruth87c44602011-07-01 23:49:12 +00002941 D->addAttr(::new (S.Context)
2942 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00002943}
2944
Charles Davisf0122fe2010-02-16 18:27:26 +00002945static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2946 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002947 Attr.getKind() == AttributeList::AT_dllexport ||
2948 Attr.getKind() == AttributeList::AT_uuid;
2949}
2950
2951//===----------------------------------------------------------------------===//
2952// Microsoft specific attribute handlers.
2953//===----------------------------------------------------------------------===//
2954
Chandler Carruth1b03c872011-07-02 00:01:44 +00002955static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet11542142010-12-19 06:50:37 +00002956 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2957 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002958 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00002959 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002960
Francois Pichet11542142010-12-19 06:50:37 +00002961 Expr *Arg = Attr.getArg(0);
2962 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002963 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2965 << "uuid" << 1;
2966 return;
2967 }
2968
Chris Lattner5f9e2722011-07-23 10:55:15 +00002969 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00002970
2971 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2972 StrRef.back() == '}';
2973
2974 // Validate GUID length.
2975 if (IsCurly && StrRef.size() != 38) {
2976 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2977 return;
2978 }
2979 if (!IsCurly && StrRef.size() != 36) {
2980 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2981 return;
2982 }
2983
2984 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2985 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00002986 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00002987 if (IsCurly) // Skip the optional '{'
2988 ++I;
2989
2990 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002991 if (i == 8 || i == 13 || i == 18 || i == 23) {
2992 if (*I != '-') {
2993 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2994 return;
2995 }
2996 } else if (!isxdigit(*I)) {
2997 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2998 return;
2999 }
3000 I++;
3001 }
Francois Pichet11542142010-12-19 06:50:37 +00003002
Chandler Carruth87c44602011-07-01 23:49:12 +00003003 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003004 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003005 } else
Francois Pichet11542142010-12-19 06:50:37 +00003006 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003007}
3008
Ted Kremenekb71368d2009-05-09 02:44:38 +00003009//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003010// Top Level Sema Entry Points
3011//===----------------------------------------------------------------------===//
3012
Chandler Carruth1b03c872011-07-02 00:01:44 +00003013static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3014 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003015 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003016 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3017 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3018 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003019 default:
3020 break;
3021 }
3022}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003023
Chandler Carruth1b03c872011-07-02 00:01:44 +00003024static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3025 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003026 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003027 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3028 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00003029 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003030 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003031 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003032 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003033 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003034 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003035 case AttributeList::AT_neon_vector_type:
3036 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003037 // Ignore these, these are type attributes, handled by
3038 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003039 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003040 case AttributeList::AT_device:
3041 case AttributeList::AT_host:
3042 case AttributeList::AT_overloadable:
3043 // Ignore, this is a non-inheritable attribute, handled
3044 // by ProcessNonInheritableDeclAttr.
3045 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003046 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3047 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003048 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003049 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003050 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003051 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3052 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3053 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003054 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003055 handleDependencyAttr (S, D, Attr); break;
3056 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3057 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3058 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3059 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3060 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003061 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003062 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003063 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003064 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3065 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3066 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3067 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003068 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003069 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003070 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003071 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3072 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3073 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3074 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3075 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003076 case AttributeList::AT_ownership_returns:
3077 case AttributeList::AT_ownership_takes:
3078 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003079 handleOwnershipAttr (S, D, Attr); break;
3080 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3081 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3082 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3083 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3084 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003085
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003086 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003087 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003088 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003089 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003090
John McCalldc7c5ad2011-07-22 08:53:00 +00003091 case AttributeList::AT_objc_returns_inner_pointer:
3092 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3093
Ted Kremenekb71368d2009-05-09 02:44:38 +00003094 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003095 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003096 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003097 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003098 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003099
3100 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003101 case AttributeList::AT_ns_returns_not_retained:
3102 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003103 case AttributeList::AT_ns_returns_retained:
3104 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003105 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003106
Nate Begeman6f3d8382009-06-26 06:32:41 +00003107 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003108 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003109
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003110 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003111 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003112
Chandler Carruth1b03c872011-07-02 00:01:44 +00003113 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3114 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3115 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3116 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003117 case AttributeList::AT_arc_weakref_unavailable:
3118 handleArcWeakrefUnavailableAttr (S, D, Attr);
3119 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003120 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3121 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3122 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3123 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003124 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003125 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3126 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3127 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003128 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003129 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003130 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003131 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003132 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003133 break;
John McCalld5313b02011-03-02 11:33:24 +00003134 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003135 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003136 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003137 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3138 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3139 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3140 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3141 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3142 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3143 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3144 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3145 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003146 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003147 // Just ignore
3148 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003149 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003150 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003151 break;
John McCall04a67a62010-02-05 21:31:56 +00003152 case AttributeList::AT_stdcall:
3153 case AttributeList::AT_cdecl:
3154 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003155 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003156 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003157 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003158 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003159 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003160 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003161 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003162 break;
Francois Pichet11542142010-12-19 06:50:37 +00003163 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003164 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003165 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003166
3167 // Thread safety attributes:
3168 case AttributeList::AT_guarded_var:
3169 handleGuardedVarAttr(S, D, Attr);
3170 break;
3171 case AttributeList::AT_pt_guarded_var:
3172 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3173 break;
3174 case AttributeList::AT_scoped_lockable:
3175 handleLockableAttr(S, D, Attr, /*scoped = */true);
3176 break;
3177 case AttributeList::AT_no_thread_safety_analysis:
3178 handleNoThreadSafetyAttr(S, D, Attr);
3179 break;
3180 case AttributeList::AT_lockable:
3181 handleLockableAttr(S, D, Attr);
3182 break;
3183
Chris Lattner803d0802008-06-29 00:43:07 +00003184 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003185 // Ask target about the attribute.
3186 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3187 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003188 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3189 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003190 break;
3191 }
3192}
3193
Peter Collingbourne60700392011-01-21 02:08:45 +00003194/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3195/// the attribute applies to decls. If the attribute is a type attribute, just
3196/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3197/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003198static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3199 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003200 bool NonInheritable, bool Inheritable) {
3201 if (Attr.isInvalid())
3202 return;
3203
3204 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3205 // FIXME: Try to deal with other __declspec attributes!
3206 return;
3207
3208 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003209 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003210
3211 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003212 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003213}
3214
Chris Lattner803d0802008-06-29 00:43:07 +00003215/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3216/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003217void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003218 const AttributeList *AttrList,
3219 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003220 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003221 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003222 }
3223
3224 // GCC accepts
3225 // static int a9 __attribute__((weakref));
3226 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003227 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003228 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003229 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003230 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003231 }
3232}
3233
Ryan Flynne25ff832009-07-30 03:15:39 +00003234/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3235/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003236NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003237 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003238 NamedDecl *NewD = 0;
3239 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3240 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003241 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003242 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003243 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003244 if (FD->getQualifier()) {
3245 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003246 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003247 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003248 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3249 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003250 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003251 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003252 VD->getStorageClass(),
3253 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003254 if (VD->getQualifier()) {
3255 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003256 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003257 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003258 }
3259 return NewD;
3260}
3261
3262/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3263/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003264void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003265 if (W.getUsed()) return; // only do this once
3266 W.setUsed(true);
3267 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3268 IdentifierInfo *NDId = ND->getIdentifier();
3269 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003270 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3271 NDId->getName()));
3272 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003273 WeakTopLevelDecl.push_back(NewD);
3274 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3275 // to insert Decl at TU scope, sorry.
3276 DeclContext *SavedContext = CurContext;
3277 CurContext = Context.getTranslationUnitDecl();
3278 PushOnScopeChains(NewD, S);
3279 CurContext = SavedContext;
3280 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003281 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003282 }
3283}
3284
Chris Lattner0744e5f2008-06-29 00:23:49 +00003285/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3286/// it, apply them to D. This is a bit tricky because PD can have attributes
3287/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003288void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3289 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003290 // It's valid to "forward-declare" #pragma weak, in which case we
3291 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00003292 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00003293 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3294 if (IdentifierInfo *Id = ND->getIdentifier()) {
3295 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3296 = WeakUndeclaredIdentifiers.find(Id);
3297 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3298 WeakInfo W = I->second;
3299 DeclApplyPragmaWeak(S, ND, W);
3300 WeakUndeclaredIdentifiers[Id] = W;
3301 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003302 }
3303 }
3304 }
3305
Chris Lattner0744e5f2008-06-29 00:23:49 +00003306 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003307 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003308 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003309
Chris Lattner0744e5f2008-06-29 00:23:49 +00003310 // Walk the declarator structure, applying decl attributes that were in a type
3311 // position to the decl itself. This handles cases like:
3312 // int *__attr__(x)** D;
3313 // when X is a decl attribute.
3314 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3315 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003316 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003317
Chris Lattner0744e5f2008-06-29 00:23:49 +00003318 // Finally, apply any attributes on the decl itself.
3319 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003320 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003321}
John McCall54abf7d2009-11-04 02:18:39 +00003322
John McCallf85e1932011-06-15 23:02:42 +00003323/// Is the given declaration allowed to use a forbidden type?
3324static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3325 // Private ivars are always okay. Unfortunately, people don't
3326 // always properly make their ivars private, even in system headers.
3327 // Plus we need to make fields okay, too.
3328 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3329 return false;
3330
3331 // Require it to be declared in a system header.
3332 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3333}
3334
3335/// Handle a delayed forbidden-type diagnostic.
3336static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3337 Decl *decl) {
3338 if (decl && isForbiddenTypeAllowed(S, decl)) {
3339 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3340 "this system declaration uses an unsupported type"));
3341 return;
3342 }
3343
3344 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3345 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3346 diag.Triggered = true;
3347}
3348
John McCalleee1d542011-02-14 07:13:47 +00003349// This duplicates a vector push_back but hides the need to know the
3350// size of the type.
3351void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3352 assert(StackSize <= StackCapacity);
3353
3354 // Grow the stack if necessary.
3355 if (StackSize == StackCapacity) {
3356 unsigned newCapacity = 2 * StackCapacity + 2;
3357 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3358 const char *oldBuffer = (const char*) Stack;
3359
3360 if (StackCapacity)
3361 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3362
3363 delete[] oldBuffer;
3364 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3365 StackCapacity = newCapacity;
3366 }
3367
3368 assert(StackSize < StackCapacity);
3369 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003370}
3371
John McCalleee1d542011-02-14 07:13:47 +00003372void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3373 Decl *decl) {
3374 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003375
John McCalleee1d542011-02-14 07:13:47 +00003376 // Check the invariants.
3377 assert(DD.StackSize >= state.SavedStackSize);
3378 assert(state.SavedStackSize >= DD.ActiveStackBase);
3379 assert(DD.ParsingDepth > 0);
3380
3381 // Drop the parsing depth.
3382 DD.ParsingDepth--;
3383
3384 // If there are no active diagnostics, we're done.
3385 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003386 return;
3387
John McCall2f514482010-01-27 03:50:35 +00003388 // We only want to actually emit delayed diagnostics when we
3389 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003390 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003391 // We emit all the active diagnostics, not just those starting
3392 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003393 // decl spec and another for each declarator; in a decl group like:
3394 // deprecated_typedef foo, *bar, baz();
3395 // only the declarator pops will be passed decls. This is correct;
3396 // we really do need to consider delayed diagnostics from the decl spec
3397 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003398 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3399 DelayedDiagnostic &diag = DD.Stack[i];
3400 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003401 continue;
3402
John McCalleee1d542011-02-14 07:13:47 +00003403 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003404 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003405 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003406 break;
3407
3408 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003409 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003410 break;
John McCallf85e1932011-06-15 23:02:42 +00003411
3412 case DelayedDiagnostic::ForbiddenType:
3413 handleDelayedForbiddenType(S, diag, decl);
3414 break;
John McCall2f514482010-01-27 03:50:35 +00003415 }
3416 }
3417 }
3418
John McCall58e6f342010-03-16 05:22:47 +00003419 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003420 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003421 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003422
John McCalleee1d542011-02-14 07:13:47 +00003423 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003424}
3425
3426static bool isDeclDeprecated(Decl *D) {
3427 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003428 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003429 return true;
3430 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3431 return false;
3432}
3433
John McCall9c3087b2010-08-26 02:13:20 +00003434void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003435 Decl *Ctx) {
3436 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003437 return;
3438
John McCall2f514482010-01-27 03:50:35 +00003439 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003440 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003441 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003442 << DD.getDeprecationDecl()->getDeclName()
3443 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003444 else
3445 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003446 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003447}
3448
Chris Lattner5f9e2722011-07-23 10:55:15 +00003449void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003450 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003451 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003452 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003453 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3454 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003455 return;
3456 }
3457
3458 // Otherwise, don't warn if our current context is deprecated.
3459 if (isDeclDeprecated(cast<Decl>(CurContext)))
3460 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003461 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003462 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3463 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003464 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003465 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003466 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003467 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003468 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003469 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3470 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003471 }
John McCall54abf7d2009-11-04 02:18:39 +00003472}