blob: 0f02ed31df2149fdbcb4ddaf9c6c001794d2d392 [file] [log] [blame]
Chris Lattner2c6fcf52008-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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000020#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000021#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000023#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000025using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000026using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000027
John McCall5fca7ea2011-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.
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000030enum AttributeDeclType {
John McCall5fca7ea2011-03-02 12:29:23 +000031 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,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000045 ExpectedVariableFunctionOrLabel,
46 ExpectedFieldOrGlobalVar
John McCall5fca7ea2011-03-02 12:29:23 +000047};
48
Chris Lattner58418ff2008-06-29 00:16:31 +000049//===----------------------------------------------------------------------===//
50// Helper functions
51//===----------------------------------------------------------------------===//
52
Chandler Carruthff4c4f02011-07-01 23:49:12 +000053static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000054 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000055 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000056 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000057 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000059 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000060 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000061 Ty = decl->getUnderlyingType();
62 else
63 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000064
Chris Lattner2c6fcf52008-06-26 18:38:35 +000065 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000066 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000067 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000068 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069
John McCall9dd450b2009-09-21 23:43:11 +000070 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000071}
72
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000073// FIXME: We should provide an abstraction around a method or function
74// to provide the following bits of information.
75
Nuno Lopes518e3702009-12-20 23:11:08 +000076/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000077/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078static bool isFunction(const Decl *D) {
79 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000080}
81
82/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083/// type (function or function-typed variable) or an Objective-C
84/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085static bool isFunctionOrMethod(const Decl *D) {
86 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000087}
88
Fariborz Jahanian4447e172009-05-15 23:15:03 +000089/// isFunctionOrMethodOrBlock - Return true if the given decl has function
90/// type (function or function-typed variable) or an Objective-C
91/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092static bool isFunctionOrMethodOrBlock(const Decl *D) {
93 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000094 return true;
95 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000097 QualType Ty = V->getType();
98 return Ty->isBlockPointerType();
99 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000101}
102
John McCall3882ace2011-01-05 12:14:39 +0000103/// Return true if the given decl has a declarator that should have
104/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000105static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000106 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
108 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000109}
110
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000111/// hasFunctionProto - Return true if the given decl has a argument
112/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000113/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000114static bool hasFunctionProto(const Decl *D) {
115 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000116 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000117 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000118 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000119 return true;
120 }
121}
122
123/// getFunctionOrMethodNumArgs - Return number of function or method
124/// arguments. It is an error to call this on a K&R function (use
125/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000128 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000130 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000131 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000136 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000138 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000139
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000141}
142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143static QualType getFunctionOrMethodResultType(const Decl *D) {
144 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000145 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000146 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000147}
148
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149static bool isFunctionOrMethodVariadic(const Decl *D) {
150 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000151 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000152 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000153 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000154 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000155 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000157 }
158}
159
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000160static bool isInstanceMethod(const Decl *D) {
161 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000162 return MethodDecl->isInstance();
163 return false;
164}
165
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000166static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000167 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000168 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000169 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000170
John McCall96fa4842010-05-17 21:00:27 +0000171 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
172 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
John McCall96fa4842010-05-17 21:00:27 +0000175 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000176
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000177 // FIXME: Should we walk the chain of classes?
178 return ClsName == &Ctx.Idents.get("NSString") ||
179 ClsName == &Ctx.Idents.get("NSMutableString");
180}
181
Daniel Dunbar980c6692008-09-26 03:32:58 +0000182static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000183 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000184 if (!PT)
185 return false;
186
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000187 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000188 if (!RT)
189 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000190
Daniel Dunbar980c6692008-09-26 03:32:58 +0000191 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000192 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000193 return false;
194
195 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
196}
197
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000198static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
199 unsigned int Num) {
200 if (Attr.getNumArgs() != Num) {
201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
202 return false;
203 }
204
205 return true;
206}
207
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000208///
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000209/// \brief Check the total number of argumenation, whether parsed by clang
210/// as arguments or parameters. Outputs a warning.
211/// \return false if the number of argumenation units does not match expectation
212///
213static bool checkAttributeNumArgsPlusParams(Sema &S, const AttributeList &Attr,
214 unsigned int Num,
215 bool moreok = false) {
216 unsigned int numArgsPlusParams = 0;
217
218 if (Attr.getParameterName())
219 numArgsPlusParams++;
220
221 numArgsPlusParams += Attr.getNumArgs();
222
223 if (moreok && numArgsPlusParams < Num) {
224 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
225 return false;
226 }
227
228 if (!moreok && numArgsPlusParams != Num) {
229 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
230 return false;
231 }
232
233 return true;
234}
235
236///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000237/// \brief Check if passed in Decl is a field or potentially shared global var
238/// \return true if the Decl is a field or potentially shared global variable
239///
240static bool mayBeSharedVariable(Decl *D) {
241 if (isa<FieldDecl>(D))
242 return true;
243 if(VarDecl *vd = dyn_cast<VarDecl>(D))
244 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
245
246 return false;
247}
248
249///
250/// \brief Check if passed in Decl is a pointer type.
251/// Note that this function may produce an error message.
252/// \return true if the Decl is a pointer type; false otherwise
253///
254bool checkIsPointer(Sema & S, Decl * D, const AttributeList & Attr) {
255 if(ValueDecl * vd = dyn_cast <ValueDecl>(D)) {
256 QualType QT = vd->getType();
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000257 if(QT->isAnyPointerType()) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000258 return true;
259 }
260 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
261 << Attr.getName()->getName() << QT;
262 } else {
263 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
264 << Attr.getName();
265 }
266 return false;
267}
268
Chris Lattner58418ff2008-06-29 00:16:31 +0000269//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000270// Attribute Implementations
271//===----------------------------------------------------------------------===//
272
Daniel Dunbar032db472008-07-31 22:40:48 +0000273// FIXME: All this manual attribute parsing code is gross. At the
274// least add some helper functions to check most argument patterns (#
275// and types of args).
276
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000277static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
278 bool pointer = false) {
279 assert(!Attr.isInvalid());
280
281 if (!checkAttributeNumArgs(S, Attr, 0))
282 return;
283
284 // D must be either a member field or global (potentially shared) variable.
285 if (!mayBeSharedVariable(D)) {
286 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
287 << Attr.getName() << 15; /*fields and global vars*/;
288 return;
289 }
290
291 if (pointer && !checkIsPointer(S, D, Attr))
292 return;
293
294 if (pointer)
295 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getLoc(), S.Context));
296 else
297 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getLoc(), S.Context));
298}
299
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000300static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
301 bool pointer = false) {
302 assert(!Attr.isInvalid());
303
304 if (!checkAttributeNumArgsPlusParams(S, Attr, 1))
305 return;
306
307 // D must be either a member field or global (potentially shared) variable.
308 if (!mayBeSharedVariable(D)) {
309 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
310 << Attr.getName() << 15; /*fields and global vars*/;
311 return;
312 }
313
314 if (pointer && !checkIsPointer(S, D, Attr))
315 return;
316
317 if (pointer)
318 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getLoc(), S.Context));
319 else
320 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getLoc(), S.Context));
321}
322
323
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000324static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
325 bool scoped = false) {
326 assert(!Attr.isInvalid());
327
328 if (!checkAttributeNumArgs(S, Attr, 0))
329 return;
330
331 if (!isa<CXXRecordDecl>(D)) {
332 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
333 << Attr.getName() << ExpectedClass;
334 return;
335 }
336
337 if (scoped)
338 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getLoc(), S.Context));
339 else
340 D->addAttr(::new (S.Context) LockableAttr(Attr.getLoc(), S.Context));
341}
342
343static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
344 const AttributeList &Attr) {
345 assert(!Attr.isInvalid());
346
347 if (!checkAttributeNumArgs(S, Attr, 0))
348 return;
349
350 if (!isFunction(D)) {
351 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
352 << Attr.getName() << ExpectedFunctionOrMethod;
353 return;
354 }
355
356 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getLoc(),
357 S.Context));
358}
359
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000360static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
361 bool before) {
362 assert(!Attr.isInvalid());
363
364 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
365 return;
366
367 // D must be either a member field or global (potentially shared) variable.
368 if (!mayBeSharedVariable(D)) {
369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
370 << Attr.getName() << 15; /*fields and global vars*/;
371 return;
372 }
373
374 if (before)
375 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getLoc(), S.Context));
376 else
377 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getLoc(), S.Context));
378}
379
380static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
381 bool exclusive = false) {
382 assert(!Attr.isInvalid());
383
384 // zero or more arguments ok
385
386 if (!isFunction(D)) {
387 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
388 << Attr.getName() << ExpectedFunctionOrMethod;
389 return;
390 }
391
392 if (exclusive)
393 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getLoc(),
394 S.Context));
395 else
396 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getLoc(),
397 S.Context));
398}
399
400static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
401 bool exclusive = false) {
402 assert(!Attr.isInvalid());
403
404 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
405 return;
406
407 if (!isFunction(D)) {
408 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
409 << Attr.getName() << ExpectedFunctionOrMethod;
410 return;
411 }
412
413 if (exclusive)
414 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getLoc(),
415 S.Context));
416 else
417 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getLoc(),
418 S.Context));
419
420}
421
422static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
423 bool exclusive = false) {
424 assert(!Attr.isInvalid());
425
426 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
427 return;
428
429 if (!isFunction(D)) {
430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
431 << Attr.getName() << ExpectedFunctionOrMethod;
432 return;
433 }
434
435 if (exclusive)
436 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getLoc(),
437 S.Context));
438 else
439 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getLoc(),
440 S.Context));
441}
442
443
444static void handleUnlockFunAttr(Sema &S, Decl *D,
445 const AttributeList &Attr) {
446 assert(!Attr.isInvalid());
447
448 // zero or more arguments ok
449
450 if (!isFunction(D)) {
451 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
452 << Attr.getName() << ExpectedFunctionOrMethod;
453 return;
454 }
455
456 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getLoc(), S.Context));
457}
458
459static void handleLockReturnedAttr(Sema &S, Decl *D,
460 const AttributeList &Attr) {
461 assert(!Attr.isInvalid());
462
463 if (!checkAttributeNumArgsPlusParams(S, Attr, 1))
464 return;
465
466 if (!isFunction(D)) {
467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
468 << Attr.getName() << ExpectedFunctionOrMethod;
469 return;
470 }
471
472 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getLoc(), S.Context));
473}
474
475static void handleLocksExcludedAttr(Sema &S, Decl *D,
476 const AttributeList &Attr) {
477 assert(!Attr.isInvalid());
478
479 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
480 return;
481
482 if (!isFunction(D)) {
483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
484 << Attr.getName() << ExpectedFunctionOrMethod;
485 return;
486 }
487
488 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getLoc(), S.Context));
489}
490
491
Chandler Carruthedc2c642011-07-02 00:01:44 +0000492static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
493 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000494 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000495 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000496 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000497 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000498 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000499
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000500 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000501
502 Expr *sizeExpr;
503
504 // Special case where the argument is a template id.
505 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000506 CXXScopeSpec SS;
507 UnqualifiedId id;
508 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor39c02722011-06-15 16:02:29 +0000509
510 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
511 if (Size.isInvalid())
512 return;
513
514 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000515 } else {
516 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000517 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor758a8692009-06-17 21:51:59 +0000518 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000519
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000520 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000521 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000522
523 // Instantiate/Install the vector type, and let Sema build the type for us.
524 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000525 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000526 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000527 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000528 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000529
Douglas Gregor758a8692009-06-17 21:51:59 +0000530 // Remember this typedef decl, we will need it later for diagnostics.
531 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000532 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000533}
534
Chandler Carruthedc2c642011-07-02 00:01:44 +0000535static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000536 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000537 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000538 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000539
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000540 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000541 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000542 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000543 // If the alignment is less than or equal to 8 bits, the packed attribute
544 // has no effect.
545 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000546 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000547 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000548 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000549 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000550 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000551 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000552 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000553}
554
Chandler Carruthedc2c642011-07-02 00:01:44 +0000555static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000556 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000557 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
558 else
559 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
560}
561
Chandler Carruthedc2c642011-07-02 00:01:44 +0000562static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000563 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000564 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000565 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000566
Ted Kremenek1f672822010-02-18 03:08:58 +0000567 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000568 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000569 if (MD->isInstanceMethod()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000570 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000571 return;
572 }
573
Ted Kremenekd68ec812011-02-04 06:54:16 +0000574 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000575}
576
Chandler Carruthedc2c642011-07-02 00:01:44 +0000577static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000578 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000579 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +0000580 return;
Ted Kremenek1f672822010-02-18 03:08:58 +0000581
582 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000583 // Objective-C classes.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000584 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
585 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000586 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000587 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000588
Ted Kremenekd68ec812011-02-04 06:54:16 +0000589 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000590}
591
Chandler Carruthedc2c642011-07-02 00:01:44 +0000592static void handleIBOutletCollection(Sema &S, Decl *D,
593 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000594
595 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000596 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
598 return;
599 }
600
601 // The IBOutletCollection attributes only apply to instance variables of
602 // Objective-C classes.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000603 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenekd68ec812011-02-04 06:54:16 +0000604 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek26bde772010-05-19 17:38:06 +0000605 return;
606 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000607 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000608 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
609 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
610 << VD->getType() << 0;
611 return;
612 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000613 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000614 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
615 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
616 << PD->getType() << 1;
617 return;
618 }
619
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000620 IdentifierInfo *II = Attr.getParameterName();
621 if (!II)
622 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000623
John McCallba7bf592010-08-24 05:47:05 +0000624 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000625 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000626 if (!TypeRep) {
627 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
628 return;
629 }
John McCallba7bf592010-08-24 05:47:05 +0000630 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000631 // Diagnose use of non-object type in iboutletcollection attribute.
632 // FIXME. Gnu attribute extension ignores use of builtin types in
633 // attributes. So, __attribute__((iboutletcollection(char))) will be
634 // treated as __attribute__((iboutletcollection())).
635 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
636 !QT->isObjCObjectType()) {
637 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
638 return;
639 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000640 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000641 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000642}
643
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000644static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000645 if (const RecordType *UT = T->getAsUnionType())
646 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
647 RecordDecl *UD = UT->getDecl();
648 for (RecordDecl::field_iterator it = UD->field_begin(),
649 itend = UD->field_end(); it != itend; ++it) {
650 QualType QT = it->getType();
651 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
652 T = QT;
653 return;
654 }
655 }
656 }
657}
658
Chandler Carruthedc2c642011-07-02 00:01:44 +0000659static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000660 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
661 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000662 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000664 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000665 return;
666 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000667
Chandler Carruth743682b2010-11-16 08:35:43 +0000668 // In C++ the implicit 'this' function parameter also counts, and they are
669 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000670 bool HasImplicitThisParam = isInstanceMethod(D);
671 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000672
673 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000674 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000675
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000676 for (AttributeList::arg_iterator I=Attr.arg_begin(),
677 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000678
679
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000680 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000681 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000682 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000683 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
684 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000685 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
686 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000687 return;
688 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000689
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000690 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000691
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000692 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000693 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000694 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000695 return;
696 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000697
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000698 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000699 if (HasImplicitThisParam) {
700 if (x == 0) {
701 S.Diag(Attr.getLoc(),
702 diag::err_attribute_invalid_implicit_this_argument)
703 << "nonnull" << Ex->getSourceRange();
704 return;
705 }
706 --x;
707 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000708
709 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000710 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000711 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000712
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000713 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000714 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000715 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000716 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000717 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000718 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000719
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000720 NonNullArgs.push_back(x);
721 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000722
723 // If no arguments were specified to __attribute__((nonnull)) then all pointer
724 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000725 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000726 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
727 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000728 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000729 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000730 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000731 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000732
Ted Kremenek22813f42010-10-21 18:49:36 +0000733 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000734 if (NonNullArgs.empty()) {
735 // Warn the trivial case only if attribute is not coming from a
736 // macro instantiation.
737 if (Attr.getLoc().isFileID())
738 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000739 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000740 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000741 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000742
743 unsigned* start = &NonNullArgs[0];
744 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000745 llvm::array_pod_sort(start, start + size);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000746 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000747 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000748}
749
Chandler Carruthedc2c642011-07-02 00:01:44 +0000750static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000751 // This attribute must be applied to a function declaration.
752 // The first argument to the attribute must be a string,
753 // the name of the resource, for example "malloc".
754 // The following arguments must be argument indexes, the arguments must be
755 // of integer type for Returns, otherwise of pointer type.
756 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000757 // after being held. free() should be __attribute((ownership_takes)), whereas
758 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000759
760 if (!AL.getParameterName()) {
761 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
762 << AL.getName()->getName() << 1;
763 return;
764 }
765 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000766 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000767 switch (AL.getKind()) {
768 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000769 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000770 if (AL.getNumArgs() < 1) {
771 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
772 return;
773 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000774 break;
775 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000776 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000777 if (AL.getNumArgs() < 1) {
778 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
779 return;
780 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000781 break;
782 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000783 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000784 if (AL.getNumArgs() > 1) {
785 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
786 << AL.getNumArgs() + 1;
787 return;
788 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000789 break;
790 default:
791 // This should never happen given how we are called.
792 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000793 }
794
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000795 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +0000796 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
797 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000798 return;
799 }
800
Chandler Carruth743682b2010-11-16 08:35:43 +0000801 // In C++ the implicit 'this' function parameter also counts, and they are
802 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000803 bool HasImplicitThisParam = isInstanceMethod(D);
804 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000805
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000806 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000807
808 // Normalize the argument, __foo__ becomes foo.
809 if (Module.startswith("__") && Module.endswith("__"))
810 Module = Module.substr(2, Module.size() - 4);
811
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000812 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000813
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000814 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
815 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000816
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000817 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000818 llvm::APSInt ArgNum(32);
819 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
820 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
821 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
822 << AL.getName()->getName() << IdxExpr->getSourceRange();
823 continue;
824 }
825
826 unsigned x = (unsigned) ArgNum.getZExtValue();
827
828 if (x > NumArgs || x < 1) {
829 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
830 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
831 continue;
832 }
833 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000834 if (HasImplicitThisParam) {
835 if (x == 0) {
836 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
837 << "ownership" << IdxExpr->getSourceRange();
838 return;
839 }
840 --x;
841 }
842
Ted Kremenekd21139a2010-07-31 01:52:11 +0000843 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000844 case OwnershipAttr::Takes:
845 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000846 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000847 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000848 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
849 // FIXME: Should also highlight argument in decl.
850 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000851 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000852 << "pointer"
853 << IdxExpr->getSourceRange();
854 continue;
855 }
856 break;
857 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000858 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000859 if (AL.getNumArgs() > 1) {
860 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000861 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000862 llvm::APSInt ArgNum(32);
863 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
864 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
865 S.Diag(AL.getLoc(), diag::err_ownership_type)
866 << "ownership_returns" << "integer"
867 << IdxExpr->getSourceRange();
868 return;
869 }
870 }
871 break;
872 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000873 default:
874 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000875 } // switch
876
877 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000878 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000879 i = D->specific_attr_begin<OwnershipAttr>(),
880 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000881 i != e; ++i) {
882 if ((*i)->getOwnKind() != K) {
883 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
884 I!=E; ++I) {
885 if (x == *I) {
886 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
887 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000888 }
889 }
890 }
891 }
892 OwnershipArgs.push_back(x);
893 }
894
895 unsigned* start = OwnershipArgs.data();
896 unsigned size = OwnershipArgs.size();
897 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000898
899 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
900 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
901 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000902 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000903
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000904 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000905 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000906}
907
John McCall7a198ce2011-02-08 22:35:49 +0000908/// Whether this declaration has internal linkage for the purposes of
909/// things that want to complain about things not have internal linkage.
910static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
911 switch (D->getLinkage()) {
912 case NoLinkage:
913 case InternalLinkage:
914 return true;
915
916 // Template instantiations that go from external to unique-external
917 // shouldn't get diagnosed.
918 case UniqueExternalLinkage:
919 return true;
920
921 case ExternalLinkage:
922 return false;
923 }
924 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +0000925 return false;
926}
927
Chandler Carruthedc2c642011-07-02 00:01:44 +0000928static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +0000929 // Check the attribute arguments.
930 if (Attr.getNumArgs() > 1) {
931 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
932 return;
933 }
934
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000935 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +0000936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000937 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +0000938 return;
939 }
940
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000941 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +0000942
Rafael Espindolac18086a2010-02-23 22:00:30 +0000943 // gcc rejects
944 // class c {
945 // static int a __attribute__((weakref ("v2")));
946 // static int b() __attribute__((weakref ("f3")));
947 // };
948 // and ignores the attributes of
949 // void f(void) {
950 // static int a __attribute__((weakref ("v2")));
951 // }
952 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000953 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +0000954 if (!Ctx->isFileContext()) {
955 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +0000956 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +0000957 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000958 }
959
960 // The GCC manual says
961 //
962 // At present, a declaration to which `weakref' is attached can only
963 // be `static'.
964 //
965 // It also says
966 //
967 // Without a TARGET,
968 // given as an argument to `weakref' or to `alias', `weakref' is
969 // equivalent to `weak'.
970 //
971 // gcc 4.4.1 will accept
972 // int a7 __attribute__((weakref));
973 // as
974 // int a7 __attribute__((weak));
975 // This looks like a bug in gcc. We reject that for now. We should revisit
976 // it if this behaviour is actually used.
977
John McCall7a198ce2011-02-08 22:35:49 +0000978 if (!hasEffectivelyInternalLinkage(nd)) {
979 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000980 return;
981 }
982
983 // GCC rejects
984 // static ((alias ("y"), weakref)).
985 // Should we? How to check that weakref is before or after alias?
986
987 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000988 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000989 Arg = Arg->IgnoreParenCasts();
990 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
991
Douglas Gregorfb65e592011-07-27 05:40:30 +0000992 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +0000993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
994 << "weakref" << 1;
995 return;
996 }
997 // GCC will accept anything as the argument of weakref. Should we
998 // check for an existing decl?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000999 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001000 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001001 }
1002
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001003 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001004}
1005
Chandler Carruthedc2c642011-07-02 00:01:44 +00001006static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001007 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001008 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001009 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001010 return;
1011 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001012
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001013 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001014 Arg = Arg->IgnoreParenCasts();
1015 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001016
Douglas Gregorfb65e592011-07-27 05:40:30 +00001017 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001018 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001019 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001020 return;
1021 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001022
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00001023 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001024 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1025 return;
1026 }
1027
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001028 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001029
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001030 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001031 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001032}
1033
Chandler Carruthedc2c642011-07-02 00:01:44 +00001034static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001035 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001036 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001037 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001038
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001039 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001040 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001041 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001042 return;
1043 }
1044
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001045 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001046}
1047
Chandler Carruthedc2c642011-07-02 00:01:44 +00001048static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1049 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001050 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001051 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001052 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1053 return;
1054 }
1055
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001056 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001057 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001058 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001059 return;
1060 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001061
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001062 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001063}
1064
Chandler Carruthedc2c642011-07-02 00:01:44 +00001065static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001066 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001067 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001068 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1069 return;
1070 }
Mike Stump11289f42009-09-09 15:08:12 +00001071
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001072 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001073 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001074 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001075 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001076 return;
1077 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001078 }
1079
Ted Kremenek08479ae2009-08-15 00:51:46 +00001080 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001081}
1082
Chandler Carruthedc2c642011-07-02 00:01:44 +00001083static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001084 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001085 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001086 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001087
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001088 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001089}
1090
Chandler Carruthedc2c642011-07-02 00:01:44 +00001091static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001092 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001093 if (isa<VarDecl>(D))
1094 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001095 else
1096 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001097 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001098}
1099
Chandler Carruthedc2c642011-07-02 00:01:44 +00001100static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001101 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001102 if (isa<VarDecl>(D))
1103 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001104 else
1105 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001106 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001107}
1108
Chandler Carruthedc2c642011-07-02 00:01:44 +00001109static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001110 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001111
1112 if (S.CheckNoReturnAttr(attr)) return;
1113
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001114 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001115 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001116 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001117 return;
1118 }
1119
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001120 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001121}
1122
1123bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001124 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001125 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1126 attr.setInvalid();
1127 return true;
1128 }
1129
1130 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001131}
1132
Chandler Carruthedc2c642011-07-02 00:01:44 +00001133static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1134 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001135
1136 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1137 // because 'analyzer_noreturn' does not impact the type.
1138
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001139 if(!checkAttributeNumArgs(S, Attr, 0))
1140 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001141
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001142 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1143 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001144 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1145 && !VD->getType()->isFunctionPointerType())) {
1146 S.Diag(Attr.getLoc(),
1147 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1148 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001149 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001150 return;
1151 }
1152 }
1153
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001154 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001155}
1156
John Thompsoncdb847ba2010-08-09 21:53:52 +00001157// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001158static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001159/*
1160 Returning a Vector Class in Registers
1161
Eric Christopherbc638a82010-12-01 22:13:54 +00001162 According to the PPU ABI specifications, a class with a single member of
1163 vector type is returned in memory when used as the return value of a function.
1164 This results in inefficient code when implementing vector classes. To return
1165 the value in a single vector register, add the vecreturn attribute to the
1166 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001167
1168 Example:
1169
1170 struct Vector
1171 {
1172 __vector float xyzw;
1173 } __attribute__((vecreturn));
1174
1175 Vector Add(Vector lhs, Vector rhs)
1176 {
1177 Vector result;
1178 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1179 return result; // This will be returned in a register
1180 }
1181*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001182 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001184 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001185 return;
1186 }
1187
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001188 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001189 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1190 return;
1191 }
1192
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001193 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001194 int count = 0;
1195
1196 if (!isa<CXXRecordDecl>(record)) {
1197 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1198 return;
1199 }
1200
1201 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1202 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1203 return;
1204 }
1205
Eric Christopherbc638a82010-12-01 22:13:54 +00001206 for (RecordDecl::field_iterator iter = record->field_begin();
1207 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001208 if ((count == 1) || !iter->getType()->isVectorType()) {
1209 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1210 return;
1211 }
1212 count++;
1213 }
1214
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001215 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001216}
1217
Chandler Carruthedc2c642011-07-02 00:01:44 +00001218static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001219 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001220 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001221 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001222 return;
1223 }
1224 // FIXME: Actually store the attribute on the declaration
1225}
1226
Chandler Carruthedc2c642011-07-02 00:01:44 +00001227static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001228 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001229 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001230 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001231 return;
1232 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001233
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001234 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1235 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001236 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001237 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001238 return;
1239 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001240
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001241 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001242}
1243
Chandler Carruthedc2c642011-07-02 00:01:44 +00001244static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001245 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001246 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1248 return;
1249 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001250
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001251 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001252 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001253 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1254 return;
1255 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001256 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001258 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001259 return;
1260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001262 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001263}
1264
Chandler Carruthedc2c642011-07-02 00:01:44 +00001265static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001266 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001267 if (Attr.getNumArgs() > 1) {
1268 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001269 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001270 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001271
1272 int priority = 65535; // FIXME: Do not hardcode such constants.
1273 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001274 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001275 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001276 if (E->isTypeDependent() || E->isValueDependent() ||
1277 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001279 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001280 return;
1281 }
1282 priority = Idx.getZExtValue();
1283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001284
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001285 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001286 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001287 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001288 return;
1289 }
1290
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001291 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001292 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001293}
1294
Chandler Carruthedc2c642011-07-02 00:01:44 +00001295static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001296 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001297 if (Attr.getNumArgs() > 1) {
1298 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001299 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001300 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001301
1302 int priority = 65535; // FIXME: Do not hardcode such constants.
1303 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001304 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001305 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001306 if (E->isTypeDependent() || E->isValueDependent() ||
1307 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001308 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001309 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001310 return;
1311 }
1312 priority = Idx.getZExtValue();
1313 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001314
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001315 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001316 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001317 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001318 return;
1319 }
1320
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001321 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001322 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001323}
1324
Chandler Carruthedc2c642011-07-02 00:01:44 +00001325static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001326 unsigned NumArgs = Attr.getNumArgs();
1327 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001328 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001329 return;
1330 }
Chris Lattner190aa102011-02-24 05:42:24 +00001331
Fariborz Jahanian551063102010-10-06 21:18:44 +00001332 // Handle the case where deprecated attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001333 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001334 if (NumArgs == 1) {
1335 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001336 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001337 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1338 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001339 return;
1340 }
Chris Lattner190aa102011-02-24 05:42:24 +00001341 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001342 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001343
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001344 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001345}
1346
Chandler Carruthedc2c642011-07-02 00:01:44 +00001347static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001348 unsigned NumArgs = Attr.getNumArgs();
1349 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001350 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001351 return;
1352 }
Chris Lattner190aa102011-02-24 05:42:24 +00001353
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001354 // Handle the case where unavailable attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001355 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001356 if (NumArgs == 1) {
1357 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001358 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001359 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001360 diag::err_attribute_not_string) << "unavailable";
1361 return;
1362 }
Chris Lattner190aa102011-02-24 05:42:24 +00001363 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001364 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001365 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001366}
1367
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001368static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1369 const AttributeList &Attr) {
1370 unsigned NumArgs = Attr.getNumArgs();
1371 if (NumArgs > 0) {
1372 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1373 return;
1374 }
1375
1376 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1377 Attr.getLoc(), S.Context));
1378}
1379
Chandler Carruthedc2c642011-07-02 00:01:44 +00001380static void handleAvailabilityAttr(Sema &S, Decl *D,
1381 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001382 IdentifierInfo *Platform = Attr.getParameterName();
1383 SourceLocation PlatformLoc = Attr.getParameterLoc();
1384
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001385 StringRef PlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001386 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1387 if (PlatformName.empty()) {
1388 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1389 << Platform;
1390
1391 PlatformName = Platform->getName();
1392 }
1393
1394 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1395 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1396 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001397 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001398
1399 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1400 // of these steps are needed).
1401 if (Introduced.isValid() && Deprecated.isValid() &&
1402 !(Introduced.Version < Deprecated.Version)) {
1403 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1404 << 1 << PlatformName << Deprecated.Version.getAsString()
1405 << 0 << Introduced.Version.getAsString();
1406 return;
1407 }
1408
1409 if (Introduced.isValid() && Obsoleted.isValid() &&
1410 !(Introduced.Version < Obsoleted.Version)) {
1411 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1412 << 2 << PlatformName << Obsoleted.Version.getAsString()
1413 << 0 << Introduced.Version.getAsString();
1414 return;
1415 }
1416
1417 if (Deprecated.isValid() && Obsoleted.isValid() &&
1418 !(Deprecated.Version < Obsoleted.Version)) {
1419 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1420 << 2 << PlatformName << Obsoleted.Version.getAsString()
1421 << 1 << Deprecated.Version.getAsString();
1422 return;
1423 }
1424
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001425 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001426 Platform,
1427 Introduced.Version,
1428 Deprecated.Version,
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001429 Obsoleted.Version,
1430 IsUnavailable));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001431}
1432
Chandler Carruthedc2c642011-07-02 00:01:44 +00001433static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001434 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001435 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001436 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001437
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001438 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001439 Arg = Arg->IgnoreParenCasts();
1440 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001441
Douglas Gregorfb65e592011-07-27 05:40:30 +00001442 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001444 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001445 return;
1446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001447
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001448 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001449 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001450
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001451 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001452 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001453 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001454 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001455 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001456 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001457 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001458 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001459 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001460 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001461 return;
1462 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001463
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001464 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001465}
1466
Chandler Carruthedc2c642011-07-02 00:01:44 +00001467static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1468 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001469 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1470 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001471 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001472 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001473 return;
1474 }
1475
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001476 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1477 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1478 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001479 << "objc_method_family" << 1;
1480 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001482 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001483 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001484 return;
1485 }
1486
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001487 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001488 ObjCMethodFamilyAttr::FamilyKind family;
1489 if (param == "none")
1490 family = ObjCMethodFamilyAttr::OMF_None;
1491 else if (param == "alloc")
1492 family = ObjCMethodFamilyAttr::OMF_alloc;
1493 else if (param == "copy")
1494 family = ObjCMethodFamilyAttr::OMF_copy;
1495 else if (param == "init")
1496 family = ObjCMethodFamilyAttr::OMF_init;
1497 else if (param == "mutableCopy")
1498 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1499 else if (param == "new")
1500 family = ObjCMethodFamilyAttr::OMF_new;
1501 else {
1502 // Just warn and ignore it. This is future-proof against new
1503 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001504 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001505 return;
1506 }
1507
John McCall31168b02011-06-15 23:02:42 +00001508 if (family == ObjCMethodFamilyAttr::OMF_init &&
1509 !method->getResultType()->isObjCObjectPointerType()) {
1510 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1511 << method->getResultType();
1512 // Ignore the attribute.
1513 return;
1514 }
1515
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001516 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCall31168b02011-06-15 23:02:42 +00001517 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001518}
1519
Chandler Carruthedc2c642011-07-02 00:01:44 +00001520static void handleObjCExceptionAttr(Sema &S, Decl *D,
1521 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001522 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00001523 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001524
Chris Lattner677a3582009-02-14 08:09:34 +00001525 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1526 if (OCI == 0) {
1527 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1528 return;
1529 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001530
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001531 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001532}
1533
Chandler Carruthedc2c642011-07-02 00:01:44 +00001534static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001535 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001537 return;
1538 }
Richard Smithdda56e42011-04-15 14:24:37 +00001539 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001540 QualType T = TD->getUnderlyingType();
1541 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001542 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001543 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1544 return;
1545 }
1546 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001547 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001548}
1549
Mike Stumpd3bb5572009-07-24 19:02:52 +00001550static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00001551handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001552 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001553 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001554 return;
1555 }
1556
1557 if (!isa<FunctionDecl>(D)) {
1558 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1559 return;
1560 }
1561
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001562 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001563}
1564
Chandler Carruthedc2c642011-07-02 00:01:44 +00001565static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001566 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001567 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001568 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001569 return;
1570 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001571
Steve Naroff3405a732008-09-18 16:44:58 +00001572 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001573 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001574 return;
1575 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001576
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001577 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001578 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001579 type = BlocksAttr::ByRef;
1580 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001581 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001582 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001583 return;
1584 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001585
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001586 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001587}
1588
Chandler Carruthedc2c642011-07-02 00:01:44 +00001589static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001590 // check the attribute arguments.
1591 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001592 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001593 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001594 }
1595
Anders Carlssonc181b012008-10-05 18:05:59 +00001596 int sentinel = 0;
1597 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001598 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001599 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001600 if (E->isTypeDependent() || E->isValueDependent() ||
1601 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001602 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001603 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001604 return;
1605 }
1606 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001607
Anders Carlssonc181b012008-10-05 18:05:59 +00001608 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001609 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1610 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001611 return;
1612 }
1613 }
1614
1615 int nullPos = 0;
1616 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001617 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001618 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001619 if (E->isTypeDependent() || E->isValueDependent() ||
1620 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001621 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001622 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001623 return;
1624 }
1625 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001626
Anders Carlssonc181b012008-10-05 18:05:59 +00001627 if (nullPos > 1 || nullPos < 0) {
1628 // FIXME: This error message could be improved, it would be nice
1629 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001630 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1631 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001632 return;
1633 }
1634 }
1635
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001636 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall9dd450b2009-09-21 23:43:11 +00001637 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001638 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001639
Chris Lattner9363e312009-03-17 23:03:47 +00001640 if (isa<FunctionNoProtoType>(FT)) {
1641 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1642 return;
1643 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001644
Chris Lattner9363e312009-03-17 23:03:47 +00001645 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001646 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001647 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001648 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001649 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001650 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001652 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001653 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001654 } else if (isa<BlockDecl>(D)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001655 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1656 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001657 ;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001658 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001659 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001660 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001661 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00001662 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001663 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001664 int m = Ty->isFunctionPointerType() ? 0 : 1;
1665 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001666 return;
1667 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001668 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001670 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001671 return;
1672 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001673 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001674 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001675 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001676 return;
1677 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001678 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00001679 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001680}
1681
Chandler Carruthedc2c642011-07-02 00:01:44 +00001682static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00001683 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001684 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00001685 return;
Chris Lattner237f2752009-02-14 07:37:35 +00001686
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001687 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001689 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001690 return;
1691 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001692
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001693 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1694 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1695 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001696 return;
1697 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001698 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1699 if (MD->getResultType()->isVoidType()) {
1700 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1701 << Attr.getName() << 1;
1702 return;
1703 }
1704
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001705 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001706}
1707
Chandler Carruthedc2c642011-07-02 00:01:44 +00001708static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001709 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001710 if (Attr.hasParameterOrArguments()) {
1711 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001712 return;
1713 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001714
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001715 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1717 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001718 return;
1719 }
1720
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001721 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001722
1723 // 'weak' only applies to declarations with external linkage.
1724 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001725 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001726 return;
1727 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001728
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001729 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001730}
1731
Chandler Carruthedc2c642011-07-02 00:01:44 +00001732static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001733 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001734 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001735 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001736
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001737
1738 // weak_import only applies to variable & function declarations.
1739 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001740 if (!D->canBeWeakImported(isDef)) {
1741 if (isDef)
1742 S.Diag(Attr.getLoc(),
1743 diag::warn_attribute_weak_import_invalid_on_definition)
1744 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00001745 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00001746 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregord71149a2011-03-23 13:27:51 +00001747 isa<ObjCInterfaceDecl>(D))) {
1748 // Nothing to warn about here.
1749 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001750 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001751 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001752
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001753 return;
1754 }
1755
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001756 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001757}
1758
Chandler Carruthedc2c642011-07-02 00:01:44 +00001759static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1760 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00001761 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001762 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00001763 return;
Nate Begemanf2758702009-06-26 06:32:41 +00001764
1765 unsigned WGSize[3];
1766 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001767 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001768 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001769 if (E->isTypeDependent() || E->isValueDependent() ||
1770 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001771 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1772 << "reqd_work_group_size" << E->getSourceRange();
1773 return;
1774 }
1775 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1776 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001777 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1778 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001779 WGSize[2]));
1780}
1781
Chandler Carruthedc2c642011-07-02 00:01:44 +00001782static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001783 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001784 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00001785 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00001786
1787 // Make sure that there is a string literal as the sections's single
1788 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001789 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001790 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001791 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001792 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001793 return;
1794 }
Mike Stump11289f42009-09-09 15:08:12 +00001795
Chris Lattner30ba6742009-08-10 19:03:04 +00001796 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001797 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001798 if (!Error.empty()) {
1799 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1800 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001801 return;
1802 }
Mike Stump11289f42009-09-09 15:08:12 +00001803
Chris Lattner20aee9b2010-01-12 20:58:53 +00001804 // This attribute cannot be applied to local variables.
1805 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1806 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1807 return;
1808 }
1809
Eric Christopherbc638a82010-12-01 22:13:54 +00001810 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1811 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001812}
1813
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001814
Chandler Carruthedc2c642011-07-02 00:01:44 +00001815static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001816 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001817 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001818 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001819 return;
1820 }
Douglas Gregor88336832011-06-15 05:45:11 +00001821
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001822 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00001823 if (Existing->getLocation().isInvalid())
1824 Existing->setLocation(Attr.getLoc());
1825 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001826 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00001827 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001828}
1829
Chandler Carruthedc2c642011-07-02 00:01:44 +00001830static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00001831 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001832 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001834 return;
1835 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001836
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001837 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00001838 if (Existing->getLocation().isInvalid())
1839 Existing->setLocation(Attr.getLoc());
1840 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001841 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00001842 }
Anders Carlssonb8316282008-10-05 23:32:53 +00001843}
1844
Chandler Carruthedc2c642011-07-02 00:01:44 +00001845static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00001846 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001847 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00001848 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001849
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001850 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001851}
1852
Chandler Carruthedc2c642011-07-02 00:01:44 +00001853static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001854 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001855 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1856 return;
1857 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001858
Anders Carlssond277d792009-01-31 01:16:18 +00001859 if (Attr.getNumArgs() != 0) {
1860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1861 return;
1862 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001863
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001864 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001865
Anders Carlssond277d792009-01-31 01:16:18 +00001866 if (!VD || !VD->hasLocalStorage()) {
1867 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1868 return;
1869 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001870
Anders Carlssond277d792009-01-31 01:16:18 +00001871 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001872 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001873 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001874 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1875 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001876 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001877 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001878 Attr.getParameterName();
1879 return;
1880 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001881
Anders Carlssond277d792009-01-31 01:16:18 +00001882 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1883 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001884 S.Diag(Attr.getParameterLoc(),
1885 diag::err_attribute_cleanup_arg_not_function)
1886 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001887 return;
1888 }
1889
Anders Carlssond277d792009-01-31 01:16:18 +00001890 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001891 S.Diag(Attr.getParameterLoc(),
1892 diag::err_attribute_cleanup_func_must_take_one_arg)
1893 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001894 return;
1895 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001896
Anders Carlsson723f55d2009-02-07 23:16:50 +00001897 // We're currently more strict than GCC about what function types we accept.
1898 // If this ever proves to be a problem it should be easy to fix.
1899 QualType Ty = S.Context.getPointerType(VD->getType());
1900 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001901 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1902 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001903 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001904 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1905 Attr.getParameterName() << ParamTy << Ty;
1906 return;
1907 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001908
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001909 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001910 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001911}
1912
Mike Stumpd3bb5572009-07-24 19:02:52 +00001913/// Handle __attribute__((format_arg((idx)))) attribute based on
1914/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001915static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001916 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001917 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001918
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001919 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001921 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001922 return;
1923 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001924
1925 // In C++ the implicit 'this' function parameter also counts, and they are
1926 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001927 bool HasImplicitThisParam = isInstanceMethod(D);
1928 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001929 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001930
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001931 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001932 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001933 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001934 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1935 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001936 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1937 << "format" << 2 << IdxExpr->getSourceRange();
1938 return;
1939 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001940
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001941 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1943 << "format" << 2 << IdxExpr->getSourceRange();
1944 return;
1945 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001946
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001947 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001948
Chandler Carruth743682b2010-11-16 08:35:43 +00001949 if (HasImplicitThisParam) {
1950 if (ArgIdx == 0) {
1951 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1952 << "format_arg" << IdxExpr->getSourceRange();
1953 return;
1954 }
1955 ArgIdx--;
1956 }
1957
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001958 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001959 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001960
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001961 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1962 if (not_nsstring_type &&
1963 !isCFStringType(Ty, S.Context) &&
1964 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001965 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001966 // FIXME: Should highlight the actual expression that has the wrong type.
1967 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001968 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001969 << IdxExpr->getSourceRange();
1970 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001971 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001972 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001973 if (!isNSStringType(Ty, S.Context) &&
1974 !isCFStringType(Ty, S.Context) &&
1975 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001976 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001977 // FIXME: Should highlight the actual expression that has the wrong type.
1978 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001979 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001980 << IdxExpr->getSourceRange();
1981 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001982 }
1983
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001984 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00001985 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001986}
1987
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001988enum FormatAttrKind {
1989 CFStringFormat,
1990 NSStringFormat,
1991 StrftimeFormat,
1992 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001993 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001994 InvalidFormat
1995};
1996
1997/// getFormatAttrKind - Map from format attribute names to supported format
1998/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001999static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002000 // Check for formats that get handled specially.
2001 if (Format == "NSString")
2002 return NSStringFormat;
2003 if (Format == "CFString")
2004 return CFStringFormat;
2005 if (Format == "strftime")
2006 return StrftimeFormat;
2007
2008 // Otherwise, check for supported formats.
2009 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2010 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
2011 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00002012 Format == "zcmn_err" ||
2013 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002014 return SupportedFormat;
2015
Duncan Sandsde4fe352010-03-23 14:44:19 +00002016 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2017 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00002018 return IgnoredFormat;
2019
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002020 return InvalidFormat;
2021}
2022
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002023/// Handle __attribute__((init_priority(priority))) attributes based on
2024/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002025static void handleInitPriorityAttr(Sema &S, Decl *D,
2026 const AttributeList &Attr) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002027 if (!S.getLangOptions().CPlusPlus) {
2028 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2029 return;
2030 }
2031
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002032 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002033 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2034 Attr.setInvalid();
2035 return;
2036 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002037 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002038 if (S.Context.getAsArrayType(T))
2039 T = S.Context.getBaseElementType(T);
2040 if (!T->getAs<RecordType>()) {
2041 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2042 Attr.setInvalid();
2043 return;
2044 }
2045
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002046 if (Attr.getNumArgs() != 1) {
2047 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2048 Attr.setInvalid();
2049 return;
2050 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002051 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002052
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002053 llvm::APSInt priority(32);
2054 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2055 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2056 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2057 << "init_priority" << priorityExpr->getSourceRange();
2058 Attr.setInvalid();
2059 return;
2060 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002061 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002062 if (prioritynum < 101 || prioritynum > 65535) {
2063 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2064 << priorityExpr->getSourceRange();
2065 Attr.setInvalid();
2066 return;
2067 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002068 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002069 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002070}
2071
Mike Stumpd3bb5572009-07-24 19:02:52 +00002072/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2073/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002074static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002075
Chris Lattner4a927cb2008-06-28 23:36:30 +00002076 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002078 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002079 return;
2080 }
2081
Chris Lattner4a927cb2008-06-28 23:36:30 +00002082 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002083 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002084 return;
2085 }
2086
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002087 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002088 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002089 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002090 return;
2091 }
2092
Chandler Carruth743682b2010-11-16 08:35:43 +00002093 // In C++ the implicit 'this' function parameter also counts, and they are
2094 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002095 bool HasImplicitThisParam = isInstanceMethod(D);
2096 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002097 unsigned FirstIdx = 1;
2098
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002099 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002100
2101 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002102 if (Format.startswith("__") && Format.endswith("__"))
2103 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002104
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002105 // Check for supported formats.
2106 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002107
2108 if (Kind == IgnoredFormat)
2109 return;
2110
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002111 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002112 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002113 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002114 return;
2115 }
2116
2117 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002118 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002119 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002120 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2121 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002122 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002123 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002124 return;
2125 }
2126
2127 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002128 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002129 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002130 return;
2131 }
2132
2133 // FIXME: Do we need to bounds check?
2134 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002135
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002136 if (HasImplicitThisParam) {
2137 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002138 S.Diag(Attr.getLoc(),
2139 diag::err_format_attribute_implicit_this_format_string)
2140 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002141 return;
2142 }
2143 ArgIdx--;
2144 }
Mike Stump11289f42009-09-09 15:08:12 +00002145
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002146 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002147 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002148
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002149 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002150 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002151 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2152 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002153 return;
2154 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002155 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002156 // FIXME: do we need to check if the type is NSString*? What are the
2157 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002158 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002159 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002160 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2161 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002162 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002163 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002164 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002165 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002166 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002167 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2168 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002169 return;
2170 }
2171
2172 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002173 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002174 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002175 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2176 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002178 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002179 return;
2180 }
2181
2182 // check if the function is variadic if the 3rd argument non-zero
2183 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002184 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002185 ++NumArgs; // +1 for ...
2186 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002187 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002188 return;
2189 }
2190 }
2191
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002192 // strftime requires FirstArg to be 0 because it doesn't read from any
2193 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002194 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002195 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002196 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2197 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002198 return;
2199 }
2200 // if 0 it disables parameter checking (to use with e.g. va_list)
2201 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002202 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002203 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002204 return;
2205 }
2206
Douglas Gregor88336832011-06-15 05:45:11 +00002207 // Check whether we already have an equivalent format attribute.
2208 for (specific_attr_iterator<FormatAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002209 i = D->specific_attr_begin<FormatAttr>(),
2210 e = D->specific_attr_end<FormatAttr>();
Douglas Gregor88336832011-06-15 05:45:11 +00002211 i != e ; ++i) {
2212 FormatAttr *f = *i;
2213 if (f->getType() == Format &&
2214 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2215 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2216 // If we don't have a valid location for this attribute, adopt the
2217 // location.
2218 if (f->getLocation().isInvalid())
2219 f->setLocation(Attr.getLoc());
2220 return;
2221 }
2222 }
2223
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002224 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002225 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002226 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002227}
2228
Chandler Carruthedc2c642011-07-02 00:01:44 +00002229static void handleTransparentUnionAttr(Sema &S, Decl *D,
2230 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002231 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002232 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002233 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002234
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002235
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002236 // Try to find the underlying union declaration.
2237 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002238 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002239 if (TD && TD->getUnderlyingType()->isUnionType())
2240 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2241 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002242 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002243
2244 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002245 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002246 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002247 return;
2248 }
2249
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002250 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002251 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002252 diag::warn_transparent_union_attribute_not_definition);
2253 return;
2254 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002255
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002256 RecordDecl::field_iterator Field = RD->field_begin(),
2257 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002258 if (Field == FieldEnd) {
2259 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2260 return;
2261 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002262
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002263 FieldDecl *FirstField = *Field;
2264 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002265 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002266 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002267 diag::warn_transparent_union_attribute_floating)
2268 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002269 return;
2270 }
2271
2272 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2273 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2274 for (; Field != FieldEnd; ++Field) {
2275 QualType FieldType = Field->getType();
2276 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2277 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2278 // Warn if we drop the attribute.
2279 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002280 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002281 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002282 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002283 diag::warn_transparent_union_attribute_field_size_align)
2284 << isSize << Field->getDeclName() << FieldBits;
2285 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002286 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002287 diag::note_transparent_union_first_field_size_align)
2288 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002289 return;
2290 }
2291 }
2292
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002293 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002294}
2295
Chandler Carruthedc2c642011-07-02 00:01:44 +00002296static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002297 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002298 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002299 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002300
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002301 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002302 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002303
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002304 // Make sure that there is a string literal as the annotation's single
2305 // argument.
2306 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002307 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002308 return;
2309 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002310 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002311 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002312}
2313
Chandler Carruthedc2c642011-07-02 00:01:44 +00002314static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002315 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002316 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002317 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002318 return;
2319 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002320
2321 //FIXME: The C++0x version of this attribute has more limited applicabilty
2322 // than GNU's, and should error out when it is used to specify a
2323 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002324
Chris Lattner4a927cb2008-06-28 23:36:30 +00002325 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002326 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002327 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002328 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002329
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002330 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002331}
2332
2333void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2334 if (E->isTypeDependent() || E->isValueDependent()) {
2335 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002336 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002337 return;
2338 }
2339
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002340 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002341 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002342 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2343 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2344 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00002345 return;
2346 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002347 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002348 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2349 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002350 return;
2351 }
2352
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002353 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2354}
2355
2356void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2357 // FIXME: Cache the number on the Attr object if non-dependent?
2358 // FIXME: Perform checking of type validity
2359 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2360 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002361}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002362
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002363/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002364/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002365///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002366/// Despite what would be logical, the mode attribute is a decl attribute, not a
2367/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2368/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002369static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002370 // This attribute isn't documented, but glibc uses it. It changes
2371 // the width of an int or unsigned int to the specified size.
2372
2373 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002374 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002375 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002376
Chris Lattneracbc2d22008-06-27 22:18:37 +00002377
2378 IdentifierInfo *Name = Attr.getParameterName();
2379 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002380 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002381 return;
2382 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002383
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002384 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002385
2386 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002387 if (Str.startswith("__") && Str.endswith("__"))
2388 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002389
2390 unsigned DestWidth = 0;
2391 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002392 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002393 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002394 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002395 switch (Str[0]) {
2396 case 'Q': DestWidth = 8; break;
2397 case 'H': DestWidth = 16; break;
2398 case 'S': DestWidth = 32; break;
2399 case 'D': DestWidth = 64; break;
2400 case 'X': DestWidth = 96; break;
2401 case 'T': DestWidth = 128; break;
2402 }
2403 if (Str[1] == 'F') {
2404 IntegerMode = false;
2405 } else if (Str[1] == 'C') {
2406 IntegerMode = false;
2407 ComplexMode = true;
2408 } else if (Str[1] != 'I') {
2409 DestWidth = 0;
2410 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002411 break;
2412 case 4:
2413 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2414 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002415 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002416 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002417 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002418 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002419 break;
2420 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002421 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002422 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002423 break;
2424 }
2425
2426 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002427 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002428 OldTy = TD->getUnderlyingType();
2429 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2430 OldTy = VD->getType();
2431 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002432 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2433 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002434 return;
2435 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002436
John McCall9dd450b2009-09-21 23:43:11 +00002437 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002438 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2439 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002440 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002441 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2442 } else if (ComplexMode) {
2443 if (!OldTy->isComplexType())
2444 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2445 } else {
2446 if (!OldTy->isFloatingType())
2447 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2448 }
2449
Mike Stump87c57ac2009-05-16 07:39:55 +00002450 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2451 // and friends, at least with glibc.
2452 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2453 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002454 // FIXME: Make sure floating-point mappings are accurate
2455 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002456 QualType NewTy;
2457 switch (DestWidth) {
2458 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002459 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002460 return;
2461 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002462 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002463 return;
2464 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002465 if (!IntegerMode) {
2466 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2467 return;
2468 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002469 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002470 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002471 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002472 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002473 break;
2474 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002475 if (!IntegerMode) {
2476 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2477 return;
2478 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002479 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002480 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002481 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002482 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002483 break;
2484 case 32:
2485 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002486 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002487 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002488 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002489 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002490 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002491 break;
2492 case 64:
2493 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002494 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002495 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002496 if (S.Context.Target.getLongWidth() == 64)
2497 NewTy = S.Context.LongTy;
2498 else
2499 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002500 else
Chandler Carruth72343702010-01-26 06:39:24 +00002501 if (S.Context.Target.getLongWidth() == 64)
2502 NewTy = S.Context.UnsignedLongTy;
2503 else
2504 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002505 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002506 case 96:
2507 NewTy = S.Context.LongDoubleTy;
2508 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002509 case 128:
2510 if (!IntegerMode) {
2511 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2512 return;
2513 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002514 if (OldTy->isSignedIntegerType())
2515 NewTy = S.Context.Int128Ty;
2516 else
2517 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002518 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002519 }
2520
Eli Friedman4735374e2009-03-03 06:41:03 +00002521 if (ComplexMode) {
2522 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002523 }
2524
2525 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00002526 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00002527 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002528 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002529 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002530 cast<ValueDecl>(D)->setType(NewTy);
2531}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002532
Chandler Carruthedc2c642011-07-02 00:01:44 +00002533static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002534 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002535 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00002536 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00002537
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002538 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002539 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002540 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002541 return;
2542 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002543
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002544 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002545}
2546
Chandler Carruthedc2c642011-07-02 00:01:44 +00002547static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00002548 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002549 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00002550 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002551
Mike Stumpd3bb5572009-07-24 19:02:52 +00002552
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002553 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002555 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002556 return;
2557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002559 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002560}
2561
Chandler Carruthedc2c642011-07-02 00:01:44 +00002562static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2563 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002564 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002565 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00002566 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002567
Chris Lattner3c77a352010-06-22 00:03:40 +00002568
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002569 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002570 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002571 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002572 return;
2573 }
2574
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002575 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002576 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002577}
2578
Chandler Carruthedc2c642011-07-02 00:01:44 +00002579static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002580 if (S.LangOpts.CUDA) {
2581 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002582 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2584 return;
2585 }
2586
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002587 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002588 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002589 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002590 return;
2591 }
2592
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002593 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002594 } else {
2595 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2596 }
2597}
2598
Chandler Carruthedc2c642011-07-02 00:01:44 +00002599static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002600 if (S.LangOpts.CUDA) {
2601 // check the attribute arguments.
2602 if (Attr.getNumArgs() != 0) {
2603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2604 return;
2605 }
2606
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002607 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002609 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002610 return;
2611 }
2612
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002613 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002614 } else {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2616 }
2617}
2618
Chandler Carruthedc2c642011-07-02 00:01:44 +00002619static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002620 if (S.LangOpts.CUDA) {
2621 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002622 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002623 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002624
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002625 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002626 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002627 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002628 return;
2629 }
2630
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002631 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002632 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002633 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002634 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2635 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2636 << FD->getType()
2637 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2638 "void");
2639 } else {
2640 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2641 << FD->getType();
2642 }
2643 return;
2644 }
2645
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002646 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002647 } else {
2648 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2649 }
2650}
2651
Chandler Carruthedc2c642011-07-02 00:01:44 +00002652static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002653 if (S.LangOpts.CUDA) {
2654 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002655 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002656 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002657
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002658
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002659 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002661 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002662 return;
2663 }
2664
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002665 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002666 } else {
2667 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2668 }
2669}
2670
Chandler Carruthedc2c642011-07-02 00:01:44 +00002671static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002672 if (S.LangOpts.CUDA) {
2673 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002674 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002675 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002676
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002677
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002678 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002680 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002681 return;
2682 }
2683
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002684 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002685 } else {
2686 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2687 }
2688}
2689
Chandler Carruthedc2c642011-07-02 00:01:44 +00002690static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002691 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002692 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00002693 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002694
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002695 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00002696 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002698 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002699 return;
2700 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002701
Douglas Gregor35b57532009-10-27 21:01:01 +00002702 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002703 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002704 return;
2705 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002706
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002707 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002708}
2709
Chandler Carruthedc2c642011-07-02 00:01:44 +00002710static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002711 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002712
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002713 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00002714 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2715 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002716 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00002717 return;
2718
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002719 if (!isa<ObjCMethodDecl>(D)) {
2720 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2721 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002722 return;
2723 }
2724
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002725 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002726 case AttributeList::AT_fastcall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002727 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002728 return;
2729 case AttributeList::AT_stdcall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002730 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002731 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002732 case AttributeList::AT_thiscall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002733 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002734 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002735 case AttributeList::AT_cdecl:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002736 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002737 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002738 case AttributeList::AT_pascal:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002739 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002740 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002741 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002742 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002743 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00002744 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002745 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002746 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002747 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002748 return;
2749 }
2750
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002751 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002752 PcsAttr::PCSType PCS;
2753 if (StrRef == "aapcs")
2754 PCS = PcsAttr::AAPCS;
2755 else if (StrRef == "aapcs-vfp")
2756 PCS = PcsAttr::AAPCS_VFP;
2757 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002758 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2759 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002760 return;
2761 }
2762
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002763 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002764 }
Abramo Bagnara50099372010-04-30 13:10:51 +00002765 default:
2766 llvm_unreachable("unexpected attribute kind");
2767 return;
2768 }
2769}
2770
Chandler Carruthedc2c642011-07-02 00:01:44 +00002771static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00002772 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002773 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002774}
2775
John McCall3882ace2011-01-05 12:14:39 +00002776bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2777 if (attr.isInvalid())
2778 return true;
2779
Ted Kremenek1551d552011-04-15 05:49:29 +00002780 if ((attr.getNumArgs() != 0 &&
2781 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2782 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00002783 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2784 attr.setInvalid();
2785 return true;
2786 }
2787
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002788 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2789 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00002790 switch (attr.getKind()) {
2791 case AttributeList::AT_cdecl: CC = CC_C; break;
2792 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2793 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2794 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2795 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002796 case AttributeList::AT_pcs: {
2797 Expr *Arg = attr.getArg(0);
2798 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00002799 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002800 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2801 << "pcs" << 1;
2802 attr.setInvalid();
2803 return true;
2804 }
2805
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002806 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002807 if (StrRef == "aapcs") {
2808 CC = CC_AAPCS;
2809 break;
2810 } else if (StrRef == "aapcs-vfp") {
2811 CC = CC_AAPCS_VFP;
2812 break;
2813 }
2814 // FALLS THROUGH
2815 }
John McCall3882ace2011-01-05 12:14:39 +00002816 default: llvm_unreachable("unexpected attribute kind"); return true;
2817 }
2818
2819 return false;
2820}
2821
Chandler Carruthedc2c642011-07-02 00:01:44 +00002822static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002823 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002824
2825 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002826 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00002827 return;
2828
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002829 if (!isa<ObjCMethodDecl>(D)) {
2830 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2831 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002832 return;
2833 }
Eli Friedman7044b762009-03-27 21:06:47 +00002834
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002835 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00002836}
2837
2838/// Checks a regparm attribute, returning true if it is ill-formed and
2839/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002840bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2841 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00002842 return true;
2843
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002844 if (Attr.getNumArgs() != 1) {
2845 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2846 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002847 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002848 }
Eli Friedman7044b762009-03-27 21:06:47 +00002849
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002850 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002851 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002852 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002853 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002854 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002855 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002856 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002857 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002858 }
2859
John McCall3882ace2011-01-05 12:14:39 +00002860 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002861 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002862 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002863 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002864 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002865 }
2866
John McCall3882ace2011-01-05 12:14:39 +00002867 numParams = NumParams.getZExtValue();
2868 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002869 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall3882ace2011-01-05 12:14:39 +00002870 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002871 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002872 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002873 }
2874
John McCall3882ace2011-01-05 12:14:39 +00002875 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002876}
2877
Chandler Carruthedc2c642011-07-02 00:01:44 +00002878static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00002879 if (S.LangOpts.CUDA) {
2880 // check the attribute arguments.
2881 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00002882 // FIXME: 0 is not okay.
2883 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002884 return;
2885 }
2886
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002887 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00002888 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002889 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002890 return;
2891 }
2892
2893 Expr *MaxThreadsExpr = Attr.getArg(0);
2894 llvm::APSInt MaxThreads(32);
2895 if (MaxThreadsExpr->isTypeDependent() ||
2896 MaxThreadsExpr->isValueDependent() ||
2897 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2898 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2899 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2900 return;
2901 }
2902
2903 llvm::APSInt MinBlocks(32);
2904 if (Attr.getNumArgs() > 1) {
2905 Expr *MinBlocksExpr = Attr.getArg(1);
2906 if (MinBlocksExpr->isTypeDependent() ||
2907 MinBlocksExpr->isValueDependent() ||
2908 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2909 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2910 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2911 return;
2912 }
2913 }
2914
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002915 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00002916 MaxThreads.getZExtValue(),
2917 MinBlocks.getZExtValue()));
2918 } else {
2919 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2920 }
2921}
2922
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002923//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002924// Checker-specific attribute handlers.
2925//===----------------------------------------------------------------------===//
2926
John McCalled433932011-01-25 03:31:58 +00002927static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2928 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2929}
2930static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2931 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2932}
2933
Chandler Carruthedc2c642011-07-02 00:01:44 +00002934static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002935 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00002936 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002937 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2938 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00002939 return;
2940 }
2941
2942 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002943 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00002944 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2945 cf = false;
2946 } else {
2947 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2948 cf = true;
2949 }
2950
2951 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002952 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2953 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00002954 return;
2955 }
2956
2957 if (cf)
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002958 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002959 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002960 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002961}
2962
Chandler Carruthedc2c642011-07-02 00:01:44 +00002963static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
2964 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002965 if (!isa<ObjCMethodDecl>(D)) {
2966 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2967 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00002968 return;
2969 }
2970
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002971 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002972}
2973
Chandler Carruthedc2c642011-07-02 00:01:44 +00002974static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
2975 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002976
John McCalled433932011-01-25 03:31:58 +00002977 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002978
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002979 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00002980 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002981 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002982 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002983 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2984 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00002985 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002986 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00002987 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002988 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002989 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2990 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00002991 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002992 return;
2993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002994
John McCalled433932011-01-25 03:31:58 +00002995 bool typeOK;
2996 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002997 switch (Attr.getKind()) {
John McCalled433932011-01-25 03:31:58 +00002998 default: llvm_unreachable("invalid ownership attribute"); return;
2999 case AttributeList::AT_ns_returns_autoreleased:
3000 case AttributeList::AT_ns_returns_retained:
3001 case AttributeList::AT_ns_returns_not_retained:
3002 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3003 cf = false;
3004 break;
3005
3006 case AttributeList::AT_cf_returns_retained:
3007 case AttributeList::AT_cf_returns_not_retained:
3008 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3009 cf = true;
3010 break;
3011 }
3012
3013 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003014 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3015 << SourceRange(Attr.getLoc())
3016 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003017 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003018 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003019
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003020 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003021 default:
3022 assert(0 && "invalid ownership attribute");
3023 return;
John McCalled433932011-01-25 03:31:58 +00003024 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003025 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCalled433932011-01-25 03:31:58 +00003026 S.Context));
3027 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003028 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003029 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003030 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003031 return;
3032 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003033 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003034 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003035 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003036 case AttributeList::AT_cf_returns_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003037 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003038 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003039 return;
3040 case AttributeList::AT_ns_returns_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003041 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003042 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003043 return;
3044 };
3045}
3046
John McCallcf166702011-07-22 08:53:00 +00003047static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3048 const AttributeList &attr) {
3049 SourceLocation loc = attr.getLoc();
3050
3051 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3052
3053 if (!isa<ObjCMethodDecl>(method)) {
3054 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3055 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
3056 return;
3057 }
3058
3059 // Check that the method returns a normal pointer.
3060 QualType resultType = method->getResultType();
3061 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
3062 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3063 << SourceRange(loc)
3064 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3065
3066 // Drop the attribute.
3067 return;
3068 }
3069
3070 method->addAttr(
3071 ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context));
3072}
3073
Chandler Carruthedc2c642011-07-02 00:01:44 +00003074static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3075 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003076 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003077
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003078 SourceLocation L = Attr.getLoc();
3079 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3080 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCall31168b02011-06-15 23:02:42 +00003081}
3082
Chandler Carruthedc2c642011-07-02 00:01:44 +00003083static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3084 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003085 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
3086 SourceLocation L = Attr.getLoc();
3087 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3088 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCall31168b02011-06-15 23:02:42 +00003089 return;
3090 }
3091
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003092 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003093 QualType type = vd->getType();
3094
3095 if (!type->isDependentType() &&
3096 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003097 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003098 << type;
3099 return;
3100 }
3101
3102 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3103
3104 // If we have no lifetime yet, check the lifetime we're presumably
3105 // going to infer.
3106 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3107 lifetime = type->getObjCARCImplicitLifetime();
3108
3109 switch (lifetime) {
3110 case Qualifiers::OCL_None:
3111 assert(type->isDependentType() &&
3112 "didn't infer lifetime for non-dependent type?");
3113 break;
3114
3115 case Qualifiers::OCL_Weak: // meaningful
3116 case Qualifiers::OCL_Strong: // meaningful
3117 break;
3118
3119 case Qualifiers::OCL_ExplicitNone:
3120 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003121 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003122 << (lifetime == Qualifiers::OCL_Autoreleasing);
3123 break;
3124 }
3125
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003126 D->addAttr(::new (S.Context)
3127 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003128}
3129
Charles Davis163855f2010-02-16 18:27:26 +00003130static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3131 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00003132 Attr.getKind() == AttributeList::AT_dllexport ||
3133 Attr.getKind() == AttributeList::AT_uuid;
3134}
3135
3136//===----------------------------------------------------------------------===//
3137// Microsoft specific attribute handlers.
3138//===----------------------------------------------------------------------===//
3139
Chandler Carruthedc2c642011-07-02 00:01:44 +00003140static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Picheta83957a2010-12-19 06:50:37 +00003141 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
3142 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003143 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003144 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003145
Francois Picheta83957a2010-12-19 06:50:37 +00003146 Expr *Arg = Attr.getArg(0);
3147 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003148 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3150 << "uuid" << 1;
3151 return;
3152 }
3153
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003154 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003155
3156 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3157 StrRef.back() == '}';
3158
3159 // Validate GUID length.
3160 if (IsCurly && StrRef.size() != 38) {
3161 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3162 return;
3163 }
3164 if (!IsCurly && StrRef.size() != 36) {
3165 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3166 return;
3167 }
3168
3169 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3170 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003171 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003172 if (IsCurly) // Skip the optional '{'
3173 ++I;
3174
3175 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003176 if (i == 8 || i == 13 || i == 18 || i == 23) {
3177 if (*I != '-') {
3178 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3179 return;
3180 }
3181 } else if (!isxdigit(*I)) {
3182 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3183 return;
3184 }
3185 I++;
3186 }
Francois Picheta83957a2010-12-19 06:50:37 +00003187
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003188 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003189 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003190 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003191 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003192}
3193
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003194//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003195// Top Level Sema Entry Points
3196//===----------------------------------------------------------------------===//
3197
Chandler Carruthedc2c642011-07-02 00:01:44 +00003198static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3199 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003200 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003201 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3202 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3203 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003204 default:
3205 break;
3206 }
3207}
Abramo Bagnara50099372010-04-30 13:10:51 +00003208
Chandler Carruthedc2c642011-07-02 00:01:44 +00003209static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3210 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003211 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003212 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3213 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00003214 case AttributeList::AT_IBOutletCollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003215 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003216 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003217 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003218 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003219 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003220 case AttributeList::AT_neon_vector_type:
3221 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003222 // Ignore these, these are type attributes, handled by
3223 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003224 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003225 case AttributeList::AT_device:
3226 case AttributeList::AT_host:
3227 case AttributeList::AT_overloadable:
3228 // Ignore, this is a non-inheritable attribute, handled
3229 // by ProcessNonInheritableDeclAttr.
3230 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003231 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3232 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003233 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003234 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003235 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003236 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3237 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3238 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003239 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003240 handleDependencyAttr (S, D, Attr); break;
3241 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3242 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3243 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3244 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3245 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003246 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003247 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003248 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003249 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3250 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3251 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3252 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003253 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003254 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003255 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003256 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3257 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3258 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3259 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3260 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003261 case AttributeList::AT_ownership_returns:
3262 case AttributeList::AT_ownership_takes:
3263 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003264 handleOwnershipAttr (S, D, Attr); break;
3265 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3266 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3267 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3268 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3269 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003270
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003271 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003272 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003273 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003274 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003275
John McCallcf166702011-07-22 08:53:00 +00003276 case AttributeList::AT_objc_returns_inner_pointer:
3277 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3278
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003279 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003280 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003281 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003282 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003283 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003284
3285 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003286 case AttributeList::AT_ns_returns_not_retained:
3287 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003288 case AttributeList::AT_ns_returns_retained:
3289 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003290 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003291
Nate Begemanf2758702009-06-26 06:32:41 +00003292 case AttributeList::AT_reqd_wg_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003293 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003294
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003295 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003296 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003297
Chandler Carruthedc2c642011-07-02 00:01:44 +00003298 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3299 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3300 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3301 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003302 case AttributeList::AT_arc_weakref_unavailable:
3303 handleArcWeakrefUnavailableAttr (S, D, Attr);
3304 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003305 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3306 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3307 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3308 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003309 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003310 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3311 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3312 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003313 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003314 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003315 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003316 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003317 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003318 break;
John McCall86bc21f2011-03-02 11:33:24 +00003319 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003320 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003321 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003322 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3323 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3324 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3325 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3326 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3327 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3328 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3329 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3330 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003331 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003332 // Just ignore
3333 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003334 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003335 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003336 break;
John McCallab26cfa2010-02-05 21:31:56 +00003337 case AttributeList::AT_stdcall:
3338 case AttributeList::AT_cdecl:
3339 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003340 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003341 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003342 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003343 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003344 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003345 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003346 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003347 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003348 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003349 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003350 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003351
3352 // Thread safety attributes:
3353 case AttributeList::AT_guarded_var:
3354 handleGuardedVarAttr(S, D, Attr);
3355 break;
3356 case AttributeList::AT_pt_guarded_var:
3357 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3358 break;
3359 case AttributeList::AT_scoped_lockable:
3360 handleLockableAttr(S, D, Attr, /*scoped = */true);
3361 break;
3362 case AttributeList::AT_no_thread_safety_analysis:
3363 handleNoThreadSafetyAttr(S, D, Attr);
3364 break;
3365 case AttributeList::AT_lockable:
3366 handleLockableAttr(S, D, Attr);
3367 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00003368 case AttributeList::AT_guarded_by:
3369 handleGuardedByAttr(S, D, Attr);
3370 break;
3371 case AttributeList::AT_pt_guarded_by:
3372 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3373 break;
3374 case AttributeList::AT_exclusive_lock_function:
3375 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3376 break;
3377 case AttributeList::AT_exclusive_locks_required:
3378 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3379 break;
3380 case AttributeList::AT_exclusive_trylock_function:
3381 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3382 break;
3383 case AttributeList::AT_lock_returned:
3384 handleLockReturnedAttr(S, D, Attr);
3385 break;
3386 case AttributeList::AT_locks_excluded:
3387 handleLocksExcludedAttr(S, D, Attr);
3388 break;
3389 case AttributeList::AT_shared_lock_function:
3390 handleLockFunAttr(S, D, Attr);
3391 break;
3392 case AttributeList::AT_shared_locks_required:
3393 handleLocksRequiredAttr(S, D, Attr);
3394 break;
3395 case AttributeList::AT_shared_trylock_function:
3396 handleTrylockFunAttr(S, D, Attr);
3397 break;
3398 case AttributeList::AT_unlock_function:
3399 handleUnlockFunAttr(S, D, Attr);
3400 break;
3401 case AttributeList::AT_acquired_before:
3402 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3403 break;
3404 case AttributeList::AT_acquired_after:
3405 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3406 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003407
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003408 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003409 // Ask target about the attribute.
3410 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3411 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003412 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3413 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003414 break;
3415 }
3416}
3417
Peter Collingbourneb331b262011-01-21 02:08:45 +00003418/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3419/// the attribute applies to decls. If the attribute is a type attribute, just
3420/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3421/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00003422static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3423 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003424 bool NonInheritable, bool Inheritable) {
3425 if (Attr.isInvalid())
3426 return;
3427
3428 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3429 // FIXME: Try to deal with other __declspec attributes!
3430 return;
3431
3432 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003433 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003434
3435 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003436 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003437}
3438
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003439/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3440/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00003441void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003442 const AttributeList *AttrList,
3443 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003444 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003445 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00003446 }
3447
3448 // GCC accepts
3449 // static int a9 __attribute__((weakref));
3450 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003451 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003452 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00003453 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00003454 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003455 }
3456}
3457
Ryan Flynn7d470f32009-07-30 03:15:39 +00003458/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3459/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00003460NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00003461 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003462 NamedDecl *NewD = 0;
3463 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3464 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003465 FD->getInnerLocStart(),
Ryan Flynn7d470f32009-07-30 03:15:39 +00003466 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00003467 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00003468 if (FD->getQualifier()) {
3469 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003470 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003471 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003472 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3473 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003474 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00003475 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003476 VD->getStorageClass(),
3477 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00003478 if (VD->getQualifier()) {
3479 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003480 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003481 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003482 }
3483 return NewD;
3484}
3485
3486/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3487/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00003488void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00003489 if (W.getUsed()) return; // only do this once
3490 W.setUsed(true);
3491 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3492 IdentifierInfo *NDId = ND->getIdentifier();
3493 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003494 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3495 NDId->getName()));
3496 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00003497 WeakTopLevelDecl.push_back(NewD);
3498 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3499 // to insert Decl at TU scope, sorry.
3500 DeclContext *SavedContext = CurContext;
3501 CurContext = Context.getTranslationUnitDecl();
3502 PushOnScopeChains(NewD, S);
3503 CurContext = SavedContext;
3504 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003505 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003506 }
3507}
3508
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003509/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3510/// it, apply them to D. This is a bit tricky because PD can have attributes
3511/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003512void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3513 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00003514 // It's valid to "forward-declare" #pragma weak, in which case we
3515 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003516 if (Inheritable) {
3517 LoadExternalWeakUndeclaredIdentifiers();
3518 if (!WeakUndeclaredIdentifiers.empty()) {
3519 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3520 if (IdentifierInfo *Id = ND->getIdentifier()) {
3521 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3522 = WeakUndeclaredIdentifiers.find(Id);
3523 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3524 WeakInfo W = I->second;
3525 DeclApplyPragmaWeak(S, ND, W);
3526 WeakUndeclaredIdentifiers[Id] = W;
3527 }
John McCall6fe02402010-10-27 00:59:00 +00003528 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003529 }
3530 }
3531 }
3532
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003533 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00003534 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003535 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003536
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003537 // Walk the declarator structure, applying decl attributes that were in a type
3538 // position to the decl itself. This handles cases like:
3539 // int *__attr__(x)** D;
3540 // when X is a decl attribute.
3541 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3542 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003543 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003544
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003545 // Finally, apply any attributes on the decl itself.
3546 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003547 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003548}
John McCall28a6aea2009-11-04 02:18:39 +00003549
John McCall31168b02011-06-15 23:02:42 +00003550/// Is the given declaration allowed to use a forbidden type?
3551static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3552 // Private ivars are always okay. Unfortunately, people don't
3553 // always properly make their ivars private, even in system headers.
3554 // Plus we need to make fields okay, too.
3555 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3556 return false;
3557
3558 // Require it to be declared in a system header.
3559 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3560}
3561
3562/// Handle a delayed forbidden-type diagnostic.
3563static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3564 Decl *decl) {
3565 if (decl && isForbiddenTypeAllowed(S, decl)) {
3566 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3567 "this system declaration uses an unsupported type"));
3568 return;
3569 }
3570
3571 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3572 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3573 diag.Triggered = true;
3574}
3575
John McCallc1465822011-02-14 07:13:47 +00003576// This duplicates a vector push_back but hides the need to know the
3577// size of the type.
3578void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3579 assert(StackSize <= StackCapacity);
3580
3581 // Grow the stack if necessary.
3582 if (StackSize == StackCapacity) {
3583 unsigned newCapacity = 2 * StackCapacity + 2;
3584 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3585 const char *oldBuffer = (const char*) Stack;
3586
3587 if (StackCapacity)
3588 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3589
3590 delete[] oldBuffer;
3591 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3592 StackCapacity = newCapacity;
3593 }
3594
3595 assert(StackSize < StackCapacity);
3596 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00003597}
3598
John McCallc1465822011-02-14 07:13:47 +00003599void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3600 Decl *decl) {
3601 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00003602
John McCallc1465822011-02-14 07:13:47 +00003603 // Check the invariants.
3604 assert(DD.StackSize >= state.SavedStackSize);
3605 assert(state.SavedStackSize >= DD.ActiveStackBase);
3606 assert(DD.ParsingDepth > 0);
3607
3608 // Drop the parsing depth.
3609 DD.ParsingDepth--;
3610
3611 // If there are no active diagnostics, we're done.
3612 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00003613 return;
3614
John McCall86121512010-01-27 03:50:35 +00003615 // We only want to actually emit delayed diagnostics when we
3616 // successfully parsed a decl.
Argyrios Kyrtzidisd8a27712011-06-24 19:59:27 +00003617 if (decl && !decl->isInvalidDecl()) {
John McCallc1465822011-02-14 07:13:47 +00003618 // We emit all the active diagnostics, not just those starting
3619 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00003620 // decl spec and another for each declarator; in a decl group like:
3621 // deprecated_typedef foo, *bar, baz();
3622 // only the declarator pops will be passed decls. This is correct;
3623 // we really do need to consider delayed diagnostics from the decl spec
3624 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00003625 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3626 DelayedDiagnostic &diag = DD.Stack[i];
3627 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00003628 continue;
3629
John McCallc1465822011-02-14 07:13:47 +00003630 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00003631 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00003632 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003633 break;
3634
3635 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00003636 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003637 break;
John McCall31168b02011-06-15 23:02:42 +00003638
3639 case DelayedDiagnostic::ForbiddenType:
3640 handleDelayedForbiddenType(S, diag, decl);
3641 break;
John McCall86121512010-01-27 03:50:35 +00003642 }
3643 }
3644 }
3645
John McCall1064d7e2010-03-16 05:22:47 +00003646 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00003647 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00003648 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00003649
John McCallc1465822011-02-14 07:13:47 +00003650 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00003651}
3652
3653static bool isDeclDeprecated(Decl *D) {
3654 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003655 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00003656 return true;
3657 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3658 return false;
3659}
3660
John McCallb45a1e72010-08-26 02:13:20 +00003661void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003662 Decl *Ctx) {
3663 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003664 return;
3665
John McCall86121512010-01-27 03:50:35 +00003666 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003667 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003668 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003669 << DD.getDeprecationDecl()->getDeclName()
3670 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003671 else
3672 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003673 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003674}
3675
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003676void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003677 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003678 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003679 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003680 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3681 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003682 return;
3683 }
3684
3685 // Otherwise, don't warn if our current context is deprecated.
3686 if (isDeclDeprecated(cast<Decl>(CurContext)))
3687 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003688 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003689 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3690 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003691 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003692 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003693 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003694 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003695 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003696 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3697 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003698 }
John McCall28a6aea2009-11-04 02:18:39 +00003699}