blob: 9fbd73f4b63b948137ba2f2e07fd5a8d3a21ea89 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000021#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000023#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000025using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000026using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000027
John McCall883cc2c2011-03-02 12:29:23 +000028/// These constants match the enumerated choices of
29/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000030enum AttributeDeclType {
John McCall883cc2c2011-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 Sadowskidb33e142011-07-28 20:12:35 +000045 ExpectedVariableFunctionOrLabel,
46 ExpectedFieldOrGlobalVar
John McCall883cc2c2011-03-02 12:29:23 +000047};
48
Chris Lattnere5c5ee12008-06-29 00:16:31 +000049//===----------------------------------------------------------------------===//
50// Helper functions
51//===----------------------------------------------------------------------===//
52
Chandler Carruth87c44602011-07-01 23:49:12 +000053static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000054 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000055 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000056 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000057 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000058 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000059 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000060 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000061 Ty = decl->getUnderlyingType();
62 else
63 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000064
Chris Lattner6b6b5372008-06-26 18:38:35 +000065 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000066 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000067 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000068 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000069
John McCall183700f2009-09-21 23:43:11 +000070 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000071}
72
Daniel Dunbar35682492008-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 Lopesd20254f2009-12-20 23:11:08 +000076/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000077/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000078static bool isFunction(const Decl *D) {
79 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000080}
81
82/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000083/// type (function or function-typed variable) or an Objective-C
84/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000085static bool isFunctionOrMethod(const Decl *D) {
86 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000087}
88
Fariborz Jahanian620d89c2009-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 Carruth87c44602011-07-01 23:49:12 +000092static bool isFunctionOrMethodOrBlock(const Decl *D) {
93 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000094 return true;
95 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000096 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000097 QualType Ty = V->getType();
98 return Ty->isBlockPointerType();
99 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000100 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000101}
102
John McCall711c52b2011-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 Carruth87c44602011-07-01 23:49:12 +0000105static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000106 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000107 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
108 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000109}
110
Daniel Dunbard3f2c102008-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 Jahanian620d89c2009-05-15 23:15:03 +0000113/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000114static bool hasFunctionProto(const Decl *D) {
115 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000116 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000117 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000118 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-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 Carruth87c44602011-07-01 23:49:12 +0000126static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000129 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000130 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000131 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000132}
133
Chandler Carruth87c44602011-07-01 23:49:12 +0000134static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000136 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000137 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000138 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000139
Chandler Carruth87c44602011-07-01 23:49:12 +0000140 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000141}
142
Chandler Carruth87c44602011-07-01 23:49:12 +0000143static QualType getFunctionOrMethodResultType(const Decl *D) {
144 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000145 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000146 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000147}
148
Chandler Carruth87c44602011-07-01 23:49:12 +0000149static bool isFunctionOrMethodVariadic(const Decl *D) {
150 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000151 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000152 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000153 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000154 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000155 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000157 }
158}
159
Chandler Carruth87c44602011-07-01 23:49:12 +0000160static bool isInstanceMethod(const Decl *D) {
161 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000162 return MethodDecl->isInstance();
163 return false;
164}
165
Chris Lattner6b6b5372008-06-26 18:38:35 +0000166static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000167 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000168 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000169 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000170
John McCall506b57e2010-05-17 21:00:27 +0000171 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
172 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000174
John McCall506b57e2010-05-17 21:00:27 +0000175 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000176
Chris Lattner6b6b5372008-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 Dunbar085e8f72008-09-26 03:32:58 +0000182static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000183 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000184 if (!PT)
185 return false;
186
Ted Kremenek6217b802009-07-29 21:53:49 +0000187 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000188 if (!RT)
189 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000190
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000191 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000192 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000193 return false;
194
195 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
196}
197
Chandler Carruth1731e202011-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 Sadowskifdde9e72011-07-28 17:21:07 +0000208///
Caitlin Sadowskidb33e142011-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 Sadowskifdde9e72011-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///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000240static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000241 if (isa<FieldDecl>(D))
242 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000243 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000244 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///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000254static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
255 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000256 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000257 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000258 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000259 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
260 << Attr.getName()->getName() << QT;
261 } else {
262 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
263 << Attr.getName();
264 }
265 return false;
266}
267
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000268//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000269// Attribute Implementations
270//===----------------------------------------------------------------------===//
271
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000272// FIXME: All this manual attribute parsing code is gross. At the
273// least add some helper functions to check most argument patterns (#
274// and types of args).
275
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000276static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
277 bool pointer = false) {
278 assert(!Attr.isInvalid());
279
280 if (!checkAttributeNumArgs(S, Attr, 0))
281 return;
282
283 // D must be either a member field or global (potentially shared) variable.
284 if (!mayBeSharedVariable(D)) {
285 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
286 << Attr.getName() << 15; /*fields and global vars*/;
287 return;
288 }
289
290 if (pointer && !checkIsPointer(S, D, Attr))
291 return;
292
293 if (pointer)
294 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getLoc(), S.Context));
295 else
296 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getLoc(), S.Context));
297}
298
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000299static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
300 bool pointer = false) {
301 assert(!Attr.isInvalid());
302
303 if (!checkAttributeNumArgsPlusParams(S, Attr, 1))
304 return;
305
306 // D must be either a member field or global (potentially shared) variable.
307 if (!mayBeSharedVariable(D)) {
308 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
309 << Attr.getName() << 15; /*fields and global vars*/;
310 return;
311 }
312
313 if (pointer && !checkIsPointer(S, D, Attr))
314 return;
315
316 if (pointer)
317 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getLoc(), S.Context));
318 else
319 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getLoc(), S.Context));
320}
321
322
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000323static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
324 bool scoped = false) {
325 assert(!Attr.isInvalid());
326
327 if (!checkAttributeNumArgs(S, Attr, 0))
328 return;
329
330 if (!isa<CXXRecordDecl>(D)) {
331 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
332 << Attr.getName() << ExpectedClass;
333 return;
334 }
335
336 if (scoped)
337 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getLoc(), S.Context));
338 else
339 D->addAttr(::new (S.Context) LockableAttr(Attr.getLoc(), S.Context));
340}
341
342static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
343 const AttributeList &Attr) {
344 assert(!Attr.isInvalid());
345
346 if (!checkAttributeNumArgs(S, Attr, 0))
347 return;
348
349 if (!isFunction(D)) {
350 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
351 << Attr.getName() << ExpectedFunctionOrMethod;
352 return;
353 }
354
355 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getLoc(),
356 S.Context));
357}
358
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000359static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
360 bool before) {
361 assert(!Attr.isInvalid());
362
363 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
364 return;
365
366 // D must be either a member field or global (potentially shared) variable.
367 if (!mayBeSharedVariable(D)) {
368 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
369 << Attr.getName() << 15; /*fields and global vars*/;
370 return;
371 }
372
373 if (before)
374 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getLoc(), S.Context));
375 else
376 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getLoc(), S.Context));
377}
378
379static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
380 bool exclusive = false) {
381 assert(!Attr.isInvalid());
382
383 // zero or more arguments ok
384
385 if (!isFunction(D)) {
386 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
387 << Attr.getName() << ExpectedFunctionOrMethod;
388 return;
389 }
390
391 if (exclusive)
392 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getLoc(),
393 S.Context));
394 else
395 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getLoc(),
396 S.Context));
397}
398
399static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
400 bool exclusive = false) {
401 assert(!Attr.isInvalid());
402
403 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
404 return;
405
406 if (!isFunction(D)) {
407 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
408 << Attr.getName() << ExpectedFunctionOrMethod;
409 return;
410 }
411
412 if (exclusive)
413 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getLoc(),
414 S.Context));
415 else
416 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getLoc(),
417 S.Context));
418
419}
420
421static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
422 bool exclusive = false) {
423 assert(!Attr.isInvalid());
424
425 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
426 return;
427
428 if (!isFunction(D)) {
429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
430 << Attr.getName() << ExpectedFunctionOrMethod;
431 return;
432 }
433
434 if (exclusive)
435 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getLoc(),
436 S.Context));
437 else
438 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getLoc(),
439 S.Context));
440}
441
442
443static void handleUnlockFunAttr(Sema &S, Decl *D,
444 const AttributeList &Attr) {
445 assert(!Attr.isInvalid());
446
447 // zero or more arguments ok
448
449 if (!isFunction(D)) {
450 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
451 << Attr.getName() << ExpectedFunctionOrMethod;
452 return;
453 }
454
455 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getLoc(), S.Context));
456}
457
458static void handleLockReturnedAttr(Sema &S, Decl *D,
459 const AttributeList &Attr) {
460 assert(!Attr.isInvalid());
461
462 if (!checkAttributeNumArgsPlusParams(S, Attr, 1))
463 return;
464
465 if (!isFunction(D)) {
466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
467 << Attr.getName() << ExpectedFunctionOrMethod;
468 return;
469 }
470
471 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getLoc(), S.Context));
472}
473
474static void handleLocksExcludedAttr(Sema &S, Decl *D,
475 const AttributeList &Attr) {
476 assert(!Attr.isInvalid());
477
478 if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true))
479 return;
480
481 if (!isFunction(D)) {
482 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
483 << Attr.getName() << ExpectedFunctionOrMethod;
484 return;
485 }
486
487 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getLoc(), S.Context));
488}
489
490
Chandler Carruth1b03c872011-07-02 00:01:44 +0000491static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
492 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000493 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000494 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000495 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000496 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000497 }
Mike Stumpbf916502009-07-24 19:02:52 +0000498
Chris Lattner6b6b5372008-06-26 18:38:35 +0000499 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000500
501 Expr *sizeExpr;
502
503 // Special case where the argument is a template id.
504 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000505 CXXScopeSpec SS;
506 UnqualifiedId id;
507 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000508
509 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
510 if (Size.isInvalid())
511 return;
512
513 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000514 } else {
515 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000516 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000517 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000518
Peter Collingbourne7a730022010-11-23 20:45:58 +0000519 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000520 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000521
522 // Instantiate/Install the vector type, and let Sema build the type for us.
523 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000524 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000525 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000526 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000527 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000528
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000529 // Remember this typedef decl, we will need it later for diagnostics.
530 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000531 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000532}
533
Chandler Carruth1b03c872011-07-02 00:01:44 +0000534static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000535 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000536 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000537 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000538
Chandler Carruth87c44602011-07-01 23:49:12 +0000539 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Sean Huntcf807c42010-08-18 23:23:40 +0000540 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000541 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000542 // If the alignment is less than or equal to 8 bits, the packed attribute
543 // has no effect.
544 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000545 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000546 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000547 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000548 else
Sean Huntcf807c42010-08-18 23:23:40 +0000549 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000550 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000551 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000552}
553
Chandler Carruth1b03c872011-07-02 00:01:44 +0000554static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000555 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000556 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
557 else
558 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
559}
560
Chandler Carruth1b03c872011-07-02 00:01:44 +0000561static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000562 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000563 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000564 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000565
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000566 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000567 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000568 if (MD->isInstanceMethod()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000569 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000570 return;
571 }
572
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000573 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000574}
575
Chandler Carruth1b03c872011-07-02 00:01:44 +0000576static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000577 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000578 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000579 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000580
581 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000582 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000583 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
584 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000585 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000586 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000587
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000588 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000589}
590
Chandler Carruth1b03c872011-07-02 00:01:44 +0000591static void handleIBOutletCollection(Sema &S, Decl *D,
592 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000593
594 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000595 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
597 return;
598 }
599
600 // The IBOutletCollection attributes only apply to instance variables of
601 // Objective-C classes.
Chandler Carruth87c44602011-07-01 23:49:12 +0000602 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000603 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000604 return;
605 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000606 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000607 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
608 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
609 << VD->getType() << 0;
610 return;
611 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000612 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000613 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
614 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
615 << PD->getType() << 1;
616 return;
617 }
618
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000619 IdentifierInfo *II = Attr.getParameterName();
620 if (!II)
621 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000622
John McCallb3d87482010-08-24 05:47:05 +0000623 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000624 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000625 if (!TypeRep) {
626 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
627 return;
628 }
John McCallb3d87482010-08-24 05:47:05 +0000629 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000630 // Diagnose use of non-object type in iboutletcollection attribute.
631 // FIXME. Gnu attribute extension ignores use of builtin types in
632 // attributes. So, __attribute__((iboutletcollection(char))) will be
633 // treated as __attribute__((iboutletcollection())).
634 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
635 !QT->isObjCObjectType()) {
636 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
637 return;
638 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000639 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +0000640 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000641}
642
Chandler Carruthd309c812011-07-01 23:49:16 +0000643static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000644 if (const RecordType *UT = T->getAsUnionType())
645 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
646 RecordDecl *UD = UT->getDecl();
647 for (RecordDecl::field_iterator it = UD->field_begin(),
648 itend = UD->field_end(); it != itend; ++it) {
649 QualType QT = it->getType();
650 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
651 T = QT;
652 return;
653 }
654 }
655 }
656}
657
Chandler Carruth1b03c872011-07-02 00:01:44 +0000658static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000659 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
660 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000661 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000663 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000664 return;
665 }
Mike Stumpbf916502009-07-24 19:02:52 +0000666
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000667 // In C++ the implicit 'this' function parameter also counts, and they are
668 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000669 bool HasImplicitThisParam = isInstanceMethod(D);
670 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000671
672 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000673 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000674
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000675 for (AttributeList::arg_iterator I=Attr.arg_begin(),
676 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000677
678
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000679 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000680 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000681 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000682 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
683 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
685 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000686 return;
687 }
Mike Stumpbf916502009-07-24 19:02:52 +0000688
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000689 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000690
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000691 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000692 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000693 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000694 return;
695 }
Mike Stumpbf916502009-07-24 19:02:52 +0000696
Ted Kremenek465172f2008-07-21 22:09:15 +0000697 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000698 if (HasImplicitThisParam) {
699 if (x == 0) {
700 S.Diag(Attr.getLoc(),
701 diag::err_attribute_invalid_implicit_this_argument)
702 << "nonnull" << Ex->getSourceRange();
703 return;
704 }
705 --x;
706 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000707
708 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000709 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000710 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000711
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000712 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000713 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000714 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000715 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000716 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000717 }
Mike Stumpbf916502009-07-24 19:02:52 +0000718
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000719 NonNullArgs.push_back(x);
720 }
Mike Stumpbf916502009-07-24 19:02:52 +0000721
722 // If no arguments were specified to __attribute__((nonnull)) then all pointer
723 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000724 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000725 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
726 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000727 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000728 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000729 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000730 }
Mike Stumpbf916502009-07-24 19:02:52 +0000731
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000732 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000733 if (NonNullArgs.empty()) {
734 // Warn the trivial case only if attribute is not coming from a
735 // macro instantiation.
736 if (Attr.getLoc().isFileID())
737 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000738 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000739 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000740 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000741
742 unsigned* start = &NonNullArgs[0];
743 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000744 llvm::array_pod_sort(start, start + size);
Chandler Carruth87c44602011-07-01 23:49:12 +0000745 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000746 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000747}
748
Chandler Carruth1b03c872011-07-02 00:01:44 +0000749static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000750 // This attribute must be applied to a function declaration.
751 // The first argument to the attribute must be a string,
752 // the name of the resource, for example "malloc".
753 // The following arguments must be argument indexes, the arguments must be
754 // of integer type for Returns, otherwise of pointer type.
755 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000756 // after being held. free() should be __attribute((ownership_takes)), whereas
757 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000758
759 if (!AL.getParameterName()) {
760 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
761 << AL.getName()->getName() << 1;
762 return;
763 }
764 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000765 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000766 switch (AL.getKind()) {
767 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000768 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000769 if (AL.getNumArgs() < 1) {
770 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
771 return;
772 }
Jordy Rose2a479922010-08-12 08:54:03 +0000773 break;
774 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000775 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000776 if (AL.getNumArgs() < 1) {
777 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
778 return;
779 }
Jordy Rose2a479922010-08-12 08:54:03 +0000780 break;
781 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000782 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000783 if (AL.getNumArgs() > 1) {
784 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
785 << AL.getNumArgs() + 1;
786 return;
787 }
Jordy Rose2a479922010-08-12 08:54:03 +0000788 break;
789 default:
790 // This should never happen given how we are called.
791 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000792 }
793
Chandler Carruth87c44602011-07-01 23:49:12 +0000794 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +0000795 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
796 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000797 return;
798 }
799
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000800 // In C++ the implicit 'this' function parameter also counts, and they are
801 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000802 bool HasImplicitThisParam = isInstanceMethod(D);
803 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000804
Chris Lattner5f9e2722011-07-23 10:55:15 +0000805 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000806
807 // Normalize the argument, __foo__ becomes foo.
808 if (Module.startswith("__") && Module.endswith("__"))
809 Module = Module.substr(2, Module.size() - 4);
810
Chris Lattner5f9e2722011-07-23 10:55:15 +0000811 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000812
Jordy Rose2a479922010-08-12 08:54:03 +0000813 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
814 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000815
Peter Collingbourne7a730022010-11-23 20:45:58 +0000816 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000817 llvm::APSInt ArgNum(32);
818 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
819 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
820 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
821 << AL.getName()->getName() << IdxExpr->getSourceRange();
822 continue;
823 }
824
825 unsigned x = (unsigned) ArgNum.getZExtValue();
826
827 if (x > NumArgs || x < 1) {
828 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
829 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
830 continue;
831 }
832 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000833 if (HasImplicitThisParam) {
834 if (x == 0) {
835 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
836 << "ownership" << IdxExpr->getSourceRange();
837 return;
838 }
839 --x;
840 }
841
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000842 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000843 case OwnershipAttr::Takes:
844 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000845 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000846 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000847 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
848 // FIXME: Should also highlight argument in decl.
849 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000850 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000851 << "pointer"
852 << IdxExpr->getSourceRange();
853 continue;
854 }
855 break;
856 }
Sean Huntcf807c42010-08-18 23:23:40 +0000857 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000858 if (AL.getNumArgs() > 1) {
859 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000860 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000861 llvm::APSInt ArgNum(32);
862 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
863 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
864 S.Diag(AL.getLoc(), diag::err_ownership_type)
865 << "ownership_returns" << "integer"
866 << IdxExpr->getSourceRange();
867 return;
868 }
869 }
870 break;
871 }
Jordy Rose2a479922010-08-12 08:54:03 +0000872 default:
873 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000874 } // switch
875
876 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000877 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +0000878 i = D->specific_attr_begin<OwnershipAttr>(),
879 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +0000880 i != e; ++i) {
881 if ((*i)->getOwnKind() != K) {
882 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
883 I!=E; ++I) {
884 if (x == *I) {
885 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
886 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000887 }
888 }
889 }
890 }
891 OwnershipArgs.push_back(x);
892 }
893
894 unsigned* start = OwnershipArgs.data();
895 unsigned size = OwnershipArgs.size();
896 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000897
898 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
899 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
900 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000901 }
Sean Huntcf807c42010-08-18 23:23:40 +0000902
Chandler Carruth87c44602011-07-01 23:49:12 +0000903 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +0000904 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000905}
906
John McCall332bb2a2011-02-08 22:35:49 +0000907/// Whether this declaration has internal linkage for the purposes of
908/// things that want to complain about things not have internal linkage.
909static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
910 switch (D->getLinkage()) {
911 case NoLinkage:
912 case InternalLinkage:
913 return true;
914
915 // Template instantiations that go from external to unique-external
916 // shouldn't get diagnosed.
917 case UniqueExternalLinkage:
918 return true;
919
920 case ExternalLinkage:
921 return false;
922 }
923 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000924 return false;
925}
926
Chandler Carruth1b03c872011-07-02 00:01:44 +0000927static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000928 // Check the attribute arguments.
929 if (Attr.getNumArgs() > 1) {
930 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
931 return;
932 }
933
Chandler Carruth87c44602011-07-01 23:49:12 +0000934 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +0000935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000936 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +0000937 return;
938 }
939
Chandler Carruth87c44602011-07-01 23:49:12 +0000940 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +0000941
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000942 // gcc rejects
943 // class c {
944 // static int a __attribute__((weakref ("v2")));
945 // static int b() __attribute__((weakref ("f3")));
946 // };
947 // and ignores the attributes of
948 // void f(void) {
949 // static int a __attribute__((weakref ("v2")));
950 // }
951 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +0000952 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000953 if (!Ctx->isFileContext()) {
954 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +0000955 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000956 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000957 }
958
959 // The GCC manual says
960 //
961 // At present, a declaration to which `weakref' is attached can only
962 // be `static'.
963 //
964 // It also says
965 //
966 // Without a TARGET,
967 // given as an argument to `weakref' or to `alias', `weakref' is
968 // equivalent to `weak'.
969 //
970 // gcc 4.4.1 will accept
971 // int a7 __attribute__((weakref));
972 // as
973 // int a7 __attribute__((weak));
974 // This looks like a bug in gcc. We reject that for now. We should revisit
975 // it if this behaviour is actually used.
976
John McCall332bb2a2011-02-08 22:35:49 +0000977 if (!hasEffectivelyInternalLinkage(nd)) {
978 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000979 return;
980 }
981
982 // GCC rejects
983 // static ((alias ("y"), weakref)).
984 // Should we? How to check that weakref is before or after alias?
985
986 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000987 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000988 Arg = Arg->IgnoreParenCasts();
989 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
990
Douglas Gregor5cee1192011-07-27 05:40:30 +0000991 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
993 << "weakref" << 1;
994 return;
995 }
996 // GCC will accept anything as the argument of weakref. Should we
997 // check for an existing decl?
Chandler Carruth87c44602011-07-01 23:49:12 +0000998 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +0000999 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001000 }
1001
Chandler Carruth87c44602011-07-01 23:49:12 +00001002 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001003}
1004
Chandler Carruth1b03c872011-07-02 00:01:44 +00001005static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001006 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001007 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001009 return;
1010 }
Mike Stumpbf916502009-07-24 19:02:52 +00001011
Peter Collingbourne7a730022010-11-23 20:45:58 +00001012 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001013 Arg = Arg->IgnoreParenCasts();
1014 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001015
Douglas Gregor5cee1192011-07-27 05:40:30 +00001016 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001018 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001019 return;
1020 }
Mike Stumpbf916502009-07-24 19:02:52 +00001021
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001022 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001023 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1024 return;
1025 }
1026
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001028
Chandler Carruth87c44602011-07-01 23:49:12 +00001029 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001030 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001031}
1032
Chandler Carruth1b03c872011-07-02 00:01:44 +00001033static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001034 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001035 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001036 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001037
Chandler Carruth87c44602011-07-01 23:49:12 +00001038 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001040 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001041 return;
1042 }
1043
Chandler Carruth87c44602011-07-01 23:49:12 +00001044 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001045}
1046
Chandler Carruth1b03c872011-07-02 00:01:44 +00001047static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1048 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001049 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001050 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001051 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1052 return;
1053 }
1054
Chandler Carruth87c44602011-07-01 23:49:12 +00001055 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001056 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001057 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001058 return;
1059 }
Mike Stumpbf916502009-07-24 19:02:52 +00001060
Chandler Carruth87c44602011-07-01 23:49:12 +00001061 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001062}
1063
Chandler Carruth1b03c872011-07-02 00:01:44 +00001064static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001065 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001066 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1068 return;
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chandler Carruth87c44602011-07-01 23:49:12 +00001071 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001072 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001073 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001074 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001075 return;
1076 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001077 }
1078
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001079 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001080}
1081
Chandler Carruth1b03c872011-07-02 00:01:44 +00001082static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001083 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001084 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001085 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001086
Chandler Carruth87c44602011-07-01 23:49:12 +00001087 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001088}
1089
Chandler Carruth1b03c872011-07-02 00:01:44 +00001090static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001091 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001092 if (isa<VarDecl>(D))
1093 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001094 else
1095 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001096 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001097}
1098
Chandler Carruth1b03c872011-07-02 00:01:44 +00001099static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001100 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001101 if (isa<VarDecl>(D))
1102 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001103 else
1104 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001105 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001106}
1107
Chandler Carruth1b03c872011-07-02 00:01:44 +00001108static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001109 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001110
1111 if (S.CheckNoReturnAttr(attr)) return;
1112
Chandler Carruth87c44602011-07-01 23:49:12 +00001113 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001114 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001115 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001116 return;
1117 }
1118
Chandler Carruth87c44602011-07-01 23:49:12 +00001119 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001120}
1121
1122bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001123 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001124 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1125 attr.setInvalid();
1126 return true;
1127 }
1128
1129 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001130}
1131
Chandler Carruth1b03c872011-07-02 00:01:44 +00001132static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1133 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001134
1135 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1136 // because 'analyzer_noreturn' does not impact the type.
1137
Chandler Carruth1731e202011-07-11 23:30:35 +00001138 if(!checkAttributeNumArgs(S, Attr, 0))
1139 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001140
Chandler Carruth87c44602011-07-01 23:49:12 +00001141 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1142 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001143 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1144 && !VD->getType()->isFunctionPointerType())) {
1145 S.Diag(Attr.getLoc(),
1146 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1147 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001148 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001149 return;
1150 }
1151 }
1152
Chandler Carruth87c44602011-07-01 23:49:12 +00001153 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001154}
1155
John Thompson35cc9622010-08-09 21:53:52 +00001156// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001157static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001158/*
1159 Returning a Vector Class in Registers
1160
Eric Christopherf48f3672010-12-01 22:13:54 +00001161 According to the PPU ABI specifications, a class with a single member of
1162 vector type is returned in memory when used as the return value of a function.
1163 This results in inefficient code when implementing vector classes. To return
1164 the value in a single vector register, add the vecreturn attribute to the
1165 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001166
1167 Example:
1168
1169 struct Vector
1170 {
1171 __vector float xyzw;
1172 } __attribute__((vecreturn));
1173
1174 Vector Add(Vector lhs, Vector rhs)
1175 {
1176 Vector result;
1177 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1178 return result; // This will be returned in a register
1179 }
1180*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001181 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001182 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001183 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001184 return;
1185 }
1186
Chandler Carruth87c44602011-07-01 23:49:12 +00001187 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001188 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1189 return;
1190 }
1191
Chandler Carruth87c44602011-07-01 23:49:12 +00001192 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001193 int count = 0;
1194
1195 if (!isa<CXXRecordDecl>(record)) {
1196 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1197 return;
1198 }
1199
1200 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1201 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1202 return;
1203 }
1204
Eric Christopherf48f3672010-12-01 22:13:54 +00001205 for (RecordDecl::field_iterator iter = record->field_begin();
1206 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001207 if ((count == 1) || !iter->getType()->isVectorType()) {
1208 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1209 return;
1210 }
1211 count++;
1212 }
1213
Chandler Carruth87c44602011-07-01 23:49:12 +00001214 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001215}
1216
Chandler Carruth1b03c872011-07-02 00:01:44 +00001217static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001218 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001219 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001220 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001221 return;
1222 }
1223 // FIXME: Actually store the attribute on the declaration
1224}
1225
Chandler Carruth1b03c872011-07-02 00:01:44 +00001226static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001227 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001228 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001229 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001230 return;
1231 }
Mike Stumpbf916502009-07-24 19:02:52 +00001232
Chandler Carruth87c44602011-07-01 23:49:12 +00001233 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1234 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001235 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001236 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001237 return;
1238 }
Mike Stumpbf916502009-07-24 19:02:52 +00001239
Chandler Carruth87c44602011-07-01 23:49:12 +00001240 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001241}
1242
Chandler Carruth1b03c872011-07-02 00:01:44 +00001243static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001244 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001245 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001246 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1247 return;
1248 }
Mike Stumpbf916502009-07-24 19:02:52 +00001249
Chandler Carruth87c44602011-07-01 23:49:12 +00001250 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001251 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001252 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1253 return;
1254 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001255 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001256 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001257 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001258 return;
1259 }
Mike Stumpbf916502009-07-24 19:02:52 +00001260
Chandler Carruth87c44602011-07-01 23:49:12 +00001261 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001262}
1263
Chandler Carruth1b03c872011-07-02 00:01:44 +00001264static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001265 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001266 if (Attr.getNumArgs() > 1) {
1267 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001268 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001269 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001270
1271 int priority = 65535; // FIXME: Do not hardcode such constants.
1272 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001273 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001274 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001275 if (E->isTypeDependent() || E->isValueDependent() ||
1276 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001278 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001279 return;
1280 }
1281 priority = Idx.getZExtValue();
1282 }
Mike Stumpbf916502009-07-24 19:02:52 +00001283
Chandler Carruth87c44602011-07-01 23:49:12 +00001284 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001285 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001286 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001287 return;
1288 }
1289
Chandler Carruth87c44602011-07-01 23:49:12 +00001290 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001291 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001292}
1293
Chandler Carruth1b03c872011-07-02 00:01:44 +00001294static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001295 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001296 if (Attr.getNumArgs() > 1) {
1297 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001298 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001299 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001300
1301 int priority = 65535; // FIXME: Do not hardcode such constants.
1302 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001303 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001304 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001305 if (E->isTypeDependent() || E->isValueDependent() ||
1306 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001307 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001308 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001309 return;
1310 }
1311 priority = Idx.getZExtValue();
1312 }
Mike Stumpbf916502009-07-24 19:02:52 +00001313
Chandler Carruth87c44602011-07-01 23:49:12 +00001314 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001316 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001317 return;
1318 }
1319
Chandler Carruth87c44602011-07-01 23:49:12 +00001320 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001321 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001322}
1323
Chandler Carruth1b03c872011-07-02 00:01:44 +00001324static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001325 unsigned NumArgs = Attr.getNumArgs();
1326 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001327 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001328 return;
1329 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001330
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001331 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001332 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001333 if (NumArgs == 1) {
1334 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001335 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001336 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1337 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001338 return;
1339 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001340 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001341 }
Mike Stumpbf916502009-07-24 19:02:52 +00001342
Chandler Carruth87c44602011-07-01 23:49:12 +00001343 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001344}
1345
Chandler Carruth1b03c872011-07-02 00:01:44 +00001346static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001347 unsigned NumArgs = Attr.getNumArgs();
1348 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001349 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001350 return;
1351 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001352
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001353 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001354 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001355 if (NumArgs == 1) {
1356 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001357 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001358 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001359 diag::err_attribute_not_string) << "unavailable";
1360 return;
1361 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001362 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001363 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001364 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001365}
1366
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001367static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1368 const AttributeList &Attr) {
1369 unsigned NumArgs = Attr.getNumArgs();
1370 if (NumArgs > 0) {
1371 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1372 return;
1373 }
1374
1375 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1376 Attr.getLoc(), S.Context));
1377}
1378
Chandler Carruth1b03c872011-07-02 00:01:44 +00001379static void handleAvailabilityAttr(Sema &S, Decl *D,
1380 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001381 IdentifierInfo *Platform = Attr.getParameterName();
1382 SourceLocation PlatformLoc = Attr.getParameterLoc();
1383
Chris Lattner5f9e2722011-07-23 10:55:15 +00001384 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001385 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1386 if (PlatformName.empty()) {
1387 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1388 << Platform;
1389
1390 PlatformName = Platform->getName();
1391 }
1392
1393 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1394 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1395 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001396 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001397
1398 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1399 // of these steps are needed).
1400 if (Introduced.isValid() && Deprecated.isValid() &&
1401 !(Introduced.Version < Deprecated.Version)) {
1402 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1403 << 1 << PlatformName << Deprecated.Version.getAsString()
1404 << 0 << Introduced.Version.getAsString();
1405 return;
1406 }
1407
1408 if (Introduced.isValid() && Obsoleted.isValid() &&
1409 !(Introduced.Version < Obsoleted.Version)) {
1410 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1411 << 2 << PlatformName << Obsoleted.Version.getAsString()
1412 << 0 << Introduced.Version.getAsString();
1413 return;
1414 }
1415
1416 if (Deprecated.isValid() && Obsoleted.isValid() &&
1417 !(Deprecated.Version < Obsoleted.Version)) {
1418 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1419 << 2 << PlatformName << Obsoleted.Version.getAsString()
1420 << 1 << Deprecated.Version.getAsString();
1421 return;
1422 }
1423
Chandler Carruth87c44602011-07-01 23:49:12 +00001424 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001425 Platform,
1426 Introduced.Version,
1427 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001428 Obsoleted.Version,
1429 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001430}
1431
Chandler Carruth1b03c872011-07-02 00:01:44 +00001432static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001433 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001434 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001436
Peter Collingbourne7a730022010-11-23 20:45:58 +00001437 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001438 Arg = Arg->IgnoreParenCasts();
1439 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001440
Douglas Gregor5cee1192011-07-27 05:40:30 +00001441 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001443 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444 return;
1445 }
Mike Stumpbf916502009-07-24 19:02:52 +00001446
Chris Lattner5f9e2722011-07-23 10:55:15 +00001447 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001448 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001449
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001450 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001451 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001452 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001453 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001454 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001455 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001456 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001457 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001458 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001459 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001460 return;
1461 }
Mike Stumpbf916502009-07-24 19:02:52 +00001462
Chandler Carruth87c44602011-07-01 23:49:12 +00001463 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001464}
1465
Chandler Carruth1b03c872011-07-02 00:01:44 +00001466static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1467 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001468 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1469 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001470 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001471 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001472 return;
1473 }
1474
Chandler Carruth87c44602011-07-01 23:49:12 +00001475 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1476 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001478 << "objc_method_family" << 1;
1479 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001481 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001482 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001483 return;
1484 }
1485
Chris Lattner5f9e2722011-07-23 10:55:15 +00001486 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001487 ObjCMethodFamilyAttr::FamilyKind family;
1488 if (param == "none")
1489 family = ObjCMethodFamilyAttr::OMF_None;
1490 else if (param == "alloc")
1491 family = ObjCMethodFamilyAttr::OMF_alloc;
1492 else if (param == "copy")
1493 family = ObjCMethodFamilyAttr::OMF_copy;
1494 else if (param == "init")
1495 family = ObjCMethodFamilyAttr::OMF_init;
1496 else if (param == "mutableCopy")
1497 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1498 else if (param == "new")
1499 family = ObjCMethodFamilyAttr::OMF_new;
1500 else {
1501 // Just warn and ignore it. This is future-proof against new
1502 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001503 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001504 return;
1505 }
1506
John McCallf85e1932011-06-15 23:02:42 +00001507 if (family == ObjCMethodFamilyAttr::OMF_init &&
1508 !method->getResultType()->isObjCObjectPointerType()) {
1509 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1510 << method->getResultType();
1511 // Ignore the attribute.
1512 return;
1513 }
1514
Chandler Carruth87c44602011-07-01 23:49:12 +00001515 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCallf85e1932011-06-15 23:02:42 +00001516 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001517}
1518
Chandler Carruth1b03c872011-07-02 00:01:44 +00001519static void handleObjCExceptionAttr(Sema &S, Decl *D,
1520 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001521 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001522 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001523
Chris Lattner0db29ec2009-02-14 08:09:34 +00001524 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1525 if (OCI == 0) {
1526 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1527 return;
1528 }
Mike Stumpbf916502009-07-24 19:02:52 +00001529
Sean Huntcf807c42010-08-18 23:23:40 +00001530 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001531}
1532
Chandler Carruth1b03c872011-07-02 00:01:44 +00001533static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001534 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001536 return;
1537 }
Richard Smith162e1c12011-04-15 14:24:37 +00001538 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001539 QualType T = TD->getUnderlyingType();
1540 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001541 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001542 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1543 return;
1544 }
1545 }
Sean Huntcf807c42010-08-18 23:23:40 +00001546 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001547}
1548
Mike Stumpbf916502009-07-24 19:02:52 +00001549static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001550handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001551 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001553 return;
1554 }
1555
1556 if (!isa<FunctionDecl>(D)) {
1557 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1558 return;
1559 }
1560
Sean Huntcf807c42010-08-18 23:23:40 +00001561 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001562}
1563
Chandler Carruth1b03c872011-07-02 00:01:44 +00001564static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001565 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001566 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001567 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001568 return;
1569 }
Mike Stumpbf916502009-07-24 19:02:52 +00001570
Steve Naroff9eae5762008-09-18 16:44:58 +00001571 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001573 return;
1574 }
Mike Stumpbf916502009-07-24 19:02:52 +00001575
Sean Huntcf807c42010-08-18 23:23:40 +00001576 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001577 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001578 type = BlocksAttr::ByRef;
1579 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001580 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001581 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001582 return;
1583 }
Mike Stumpbf916502009-07-24 19:02:52 +00001584
Chandler Carruth87c44602011-07-01 23:49:12 +00001585 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001586}
1587
Chandler Carruth1b03c872011-07-02 00:01:44 +00001588static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001589 // check the attribute arguments.
1590 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001591 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001592 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001593 }
1594
Anders Carlsson77091822008-10-05 18:05:59 +00001595 int sentinel = 0;
1596 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001597 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001598 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001599 if (E->isTypeDependent() || E->isValueDependent() ||
1600 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001602 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001603 return;
1604 }
1605 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001606
Anders Carlsson77091822008-10-05 18:05:59 +00001607 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001608 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1609 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001610 return;
1611 }
1612 }
1613
1614 int nullPos = 0;
1615 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001616 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001617 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001618 if (E->isTypeDependent() || E->isValueDependent() ||
1619 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001620 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001621 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001622 return;
1623 }
1624 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001625
Anders Carlsson77091822008-10-05 18:05:59 +00001626 if (nullPos > 1 || nullPos < 0) {
1627 // FIXME: This error message could be improved, it would be nice
1628 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001629 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1630 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001631 return;
1632 }
1633 }
1634
Chandler Carruth87c44602011-07-01 23:49:12 +00001635 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall183700f2009-09-21 23:43:11 +00001636 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001637 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001638
Chris Lattner897cd902009-03-17 23:03:47 +00001639 if (isa<FunctionNoProtoType>(FT)) {
1640 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1641 return;
1642 }
Mike Stumpbf916502009-07-24 19:02:52 +00001643
Chris Lattner897cd902009-03-17 23:03:47 +00001644 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001645 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001646 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001647 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001648 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001649 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001650 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001651 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001652 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001653 } else if (isa<BlockDecl>(D)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001654 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1655 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001656 ;
Chandler Carruth87c44602011-07-01 23:49:12 +00001657 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001658 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001659 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001660 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001661 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001662 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001663 int m = Ty->isFunctionPointerType() ? 0 : 1;
1664 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001665 return;
1666 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001667 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001669 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001670 return;
1671 }
Anders Carlsson77091822008-10-05 18:05:59 +00001672 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001673 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001674 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001675 return;
1676 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001677 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001678 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001679}
1680
Chandler Carruth1b03c872011-07-02 00:01:44 +00001681static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001682 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001683 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001684 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001685
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001686 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001687 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001688 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001689 return;
1690 }
Mike Stumpbf916502009-07-24 19:02:52 +00001691
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001692 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1693 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1694 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001695 return;
1696 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001697 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1698 if (MD->getResultType()->isVoidType()) {
1699 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1700 << Attr.getName() << 1;
1701 return;
1702 }
1703
Sean Huntcf807c42010-08-18 23:23:40 +00001704 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001705}
1706
Chandler Carruth1b03c872011-07-02 00:01:44 +00001707static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001708 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001709 if (Attr.hasParameterOrArguments()) {
1710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001711 return;
1712 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001713
Chandler Carruth87c44602011-07-01 23:49:12 +00001714 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1716 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001717 return;
1718 }
1719
Chandler Carruth87c44602011-07-01 23:49:12 +00001720 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001721
1722 // 'weak' only applies to declarations with external linkage.
1723 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001724 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001725 return;
1726 }
Mike Stumpbf916502009-07-24 19:02:52 +00001727
Chandler Carruth87c44602011-07-01 23:49:12 +00001728 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001729}
1730
Chandler Carruth1b03c872011-07-02 00:01:44 +00001731static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001732 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001733 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001734 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001735
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001736
1737 // weak_import only applies to variable & function declarations.
1738 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001739 if (!D->canBeWeakImported(isDef)) {
1740 if (isDef)
1741 S.Diag(Attr.getLoc(),
1742 diag::warn_attribute_weak_import_invalid_on_definition)
1743 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001744 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001745 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001746 isa<ObjCInterfaceDecl>(D))) {
1747 // Nothing to warn about here.
1748 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001750 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001751
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001752 return;
1753 }
1754
Sean Huntcf807c42010-08-18 23:23:40 +00001755 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001756}
1757
Chandler Carruth1b03c872011-07-02 00:01:44 +00001758static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1759 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001760 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001761 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00001762 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001763
1764 unsigned WGSize[3];
1765 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001766 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001767 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001768 if (E->isTypeDependent() || E->isValueDependent() ||
1769 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1771 << "reqd_work_group_size" << E->getSourceRange();
1772 return;
1773 }
1774 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1775 }
Sean Huntcf807c42010-08-18 23:23:40 +00001776 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1777 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001778 WGSize[2]));
1779}
1780
Chandler Carruth1b03c872011-07-02 00:01:44 +00001781static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001782 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001783 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001784 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001785
1786 // Make sure that there is a string literal as the sections's single
1787 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001788 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001789 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001790 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001791 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001792 return;
1793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Chris Lattner797c3c42009-08-10 19:03:04 +00001795 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001796 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001797 if (!Error.empty()) {
1798 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1799 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001800 return;
1801 }
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001803 // This attribute cannot be applied to local variables.
1804 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1805 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1806 return;
1807 }
1808
Eric Christopherf48f3672010-12-01 22:13:54 +00001809 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1810 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001811}
1812
Chris Lattner6b6b5372008-06-26 18:38:35 +00001813
Chandler Carruth1b03c872011-07-02 00:01:44 +00001814static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001815 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001816 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001818 return;
1819 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001820
Chandler Carruth87c44602011-07-01 23:49:12 +00001821 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001822 if (Existing->getLocation().isInvalid())
1823 Existing->setLocation(Attr.getLoc());
1824 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001825 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001826 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001827}
1828
Chandler Carruth1b03c872011-07-02 00:01:44 +00001829static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001830 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001831 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001832 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001833 return;
1834 }
Mike Stumpbf916502009-07-24 19:02:52 +00001835
Chandler Carruth87c44602011-07-01 23:49:12 +00001836 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001837 if (Existing->getLocation().isInvalid())
1838 Existing->setLocation(Attr.getLoc());
1839 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001840 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00001841 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001842}
1843
Chandler Carruth1b03c872011-07-02 00:01:44 +00001844static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001845 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001846 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001847 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001848
Chandler Carruth87c44602011-07-01 23:49:12 +00001849 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001850}
1851
Chandler Carruth1b03c872011-07-02 00:01:44 +00001852static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001853 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001854 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1855 return;
1856 }
Mike Stumpbf916502009-07-24 19:02:52 +00001857
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001858 if (Attr.getNumArgs() != 0) {
1859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1860 return;
1861 }
Mike Stumpbf916502009-07-24 19:02:52 +00001862
Chandler Carruth87c44602011-07-01 23:49:12 +00001863 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00001864
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001865 if (!VD || !VD->hasLocalStorage()) {
1866 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1867 return;
1868 }
Mike Stumpbf916502009-07-24 19:02:52 +00001869
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001870 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001871 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001872 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001873 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1874 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001875 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001876 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001877 Attr.getParameterName();
1878 return;
1879 }
Mike Stumpbf916502009-07-24 19:02:52 +00001880
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001881 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1882 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001883 S.Diag(Attr.getParameterLoc(),
1884 diag::err_attribute_cleanup_arg_not_function)
1885 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001886 return;
1887 }
1888
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001889 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001890 S.Diag(Attr.getParameterLoc(),
1891 diag::err_attribute_cleanup_func_must_take_one_arg)
1892 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001893 return;
1894 }
Mike Stumpbf916502009-07-24 19:02:52 +00001895
Anders Carlsson89941c12009-02-07 23:16:50 +00001896 // We're currently more strict than GCC about what function types we accept.
1897 // If this ever proves to be a problem it should be easy to fix.
1898 QualType Ty = S.Context.getPointerType(VD->getType());
1899 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001900 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1901 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001902 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001903 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1904 Attr.getParameterName() << ParamTy << Ty;
1905 return;
1906 }
Mike Stumpbf916502009-07-24 19:02:52 +00001907
Chandler Carruth87c44602011-07-01 23:49:12 +00001908 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001909 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001910}
1911
Mike Stumpbf916502009-07-24 19:02:52 +00001912/// Handle __attribute__((format_arg((idx)))) attribute based on
1913/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00001914static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001915 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001916 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00001917
Chandler Carruth87c44602011-07-01 23:49:12 +00001918 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001919 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001920 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001921 return;
1922 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001923
1924 // In C++ the implicit 'this' function parameter also counts, and they are
1925 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001926 bool HasImplicitThisParam = isInstanceMethod(D);
1927 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001928 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001929
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001930 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001931 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001932 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001933 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1934 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001935 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1936 << "format" << 2 << IdxExpr->getSourceRange();
1937 return;
1938 }
Mike Stumpbf916502009-07-24 19:02:52 +00001939
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001940 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1941 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1942 << "format" << 2 << IdxExpr->getSourceRange();
1943 return;
1944 }
Mike Stumpbf916502009-07-24 19:02:52 +00001945
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001946 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001948 if (HasImplicitThisParam) {
1949 if (ArgIdx == 0) {
1950 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1951 << "format_arg" << IdxExpr->getSourceRange();
1952 return;
1953 }
1954 ArgIdx--;
1955 }
1956
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001957 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00001958 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001959
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001960 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1961 if (not_nsstring_type &&
1962 !isCFStringType(Ty, S.Context) &&
1963 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001964 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001965 // FIXME: Should highlight the actual expression that has the wrong type.
1966 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001967 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001968 << IdxExpr->getSourceRange();
1969 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001970 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001971 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001972 if (!isNSStringType(Ty, S.Context) &&
1973 !isCFStringType(Ty, S.Context) &&
1974 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001975 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001976 // FIXME: Should highlight the actual expression that has the wrong type.
1977 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001978 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001979 << IdxExpr->getSourceRange();
1980 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001981 }
1982
Chandler Carruth87c44602011-07-01 23:49:12 +00001983 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001984 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001985}
1986
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001987enum FormatAttrKind {
1988 CFStringFormat,
1989 NSStringFormat,
1990 StrftimeFormat,
1991 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001992 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001993 InvalidFormat
1994};
1995
1996/// getFormatAttrKind - Map from format attribute names to supported format
1997/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001998static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001999 // Check for formats that get handled specially.
2000 if (Format == "NSString")
2001 return NSStringFormat;
2002 if (Format == "CFString")
2003 return CFStringFormat;
2004 if (Format == "strftime")
2005 return StrftimeFormat;
2006
2007 // Otherwise, check for supported formats.
2008 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2009 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
2010 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002011 Format == "zcmn_err" ||
2012 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002013 return SupportedFormat;
2014
Duncan Sandsbc525952010-03-23 14:44:19 +00002015 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2016 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002017 return IgnoredFormat;
2018
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002019 return InvalidFormat;
2020}
2021
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002022/// Handle __attribute__((init_priority(priority))) attributes based on
2023/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002024static void handleInitPriorityAttr(Sema &S, Decl *D,
2025 const AttributeList &Attr) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002026 if (!S.getLangOptions().CPlusPlus) {
2027 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2028 return;
2029 }
2030
Chandler Carruth87c44602011-07-01 23:49:12 +00002031 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002032 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2033 Attr.setInvalid();
2034 return;
2035 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002036 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002037 if (S.Context.getAsArrayType(T))
2038 T = S.Context.getBaseElementType(T);
2039 if (!T->getAs<RecordType>()) {
2040 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2041 Attr.setInvalid();
2042 return;
2043 }
2044
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002045 if (Attr.getNumArgs() != 1) {
2046 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2047 Attr.setInvalid();
2048 return;
2049 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002050 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002051
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002052 llvm::APSInt priority(32);
2053 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2054 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2055 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2056 << "init_priority" << priorityExpr->getSourceRange();
2057 Attr.setInvalid();
2058 return;
2059 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002060 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002061 if (prioritynum < 101 || prioritynum > 65535) {
2062 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2063 << priorityExpr->getSourceRange();
2064 Attr.setInvalid();
2065 return;
2066 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002067 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002068 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002069}
2070
Mike Stumpbf916502009-07-24 19:02:52 +00002071/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2072/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002073static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002074
Chris Lattner545dd342008-06-28 23:36:30 +00002075 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002076 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002077 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002078 return;
2079 }
2080
Chris Lattner545dd342008-06-28 23:36:30 +00002081 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002083 return;
2084 }
2085
Chandler Carruth87c44602011-07-01 23:49:12 +00002086 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002088 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002089 return;
2090 }
2091
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002092 // In C++ the implicit 'this' function parameter also counts, and they are
2093 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002094 bool HasImplicitThisParam = isInstanceMethod(D);
2095 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002096 unsigned FirstIdx = 1;
2097
Chris Lattner5f9e2722011-07-23 10:55:15 +00002098 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002099
2100 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002101 if (Format.startswith("__") && Format.endswith("__"))
2102 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002103
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002104 // Check for supported formats.
2105 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002106
2107 if (Kind == IgnoredFormat)
2108 return;
2109
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002110 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002111 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002112 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002113 return;
2114 }
2115
2116 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002117 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002118 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002119 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2120 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002121 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002122 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002123 return;
2124 }
2125
2126 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002127 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002128 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002129 return;
2130 }
2131
2132 // FIXME: Do we need to bounds check?
2133 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002134
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002135 if (HasImplicitThisParam) {
2136 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002137 S.Diag(Attr.getLoc(),
2138 diag::err_format_attribute_implicit_this_format_string)
2139 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002140 return;
2141 }
2142 ArgIdx--;
2143 }
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Chris Lattner6b6b5372008-06-26 18:38:35 +00002145 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002146 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002147
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002148 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002149 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002150 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2151 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002152 return;
2153 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002154 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002155 // FIXME: do we need to check if the type is NSString*? What are the
2156 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002157 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002158 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002159 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2160 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002161 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002162 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002163 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002164 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002165 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002166 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2167 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002168 return;
2169 }
2170
2171 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002172 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002173 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002174 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2175 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002176 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002177 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002178 return;
2179 }
2180
2181 // check if the function is variadic if the 3rd argument non-zero
2182 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002183 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002184 ++NumArgs; // +1 for ...
2185 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002186 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002187 return;
2188 }
2189 }
2190
Chris Lattner3c73c412008-11-19 08:23:25 +00002191 // strftime requires FirstArg to be 0 because it doesn't read from any
2192 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002193 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002194 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002195 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2196 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002197 return;
2198 }
2199 // if 0 it disables parameter checking (to use with e.g. va_list)
2200 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002202 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002203 return;
2204 }
2205
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002206 // Check whether we already have an equivalent format attribute.
2207 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002208 i = D->specific_attr_begin<FormatAttr>(),
2209 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002210 i != e ; ++i) {
2211 FormatAttr *f = *i;
2212 if (f->getType() == Format &&
2213 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2214 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2215 // If we don't have a valid location for this attribute, adopt the
2216 // location.
2217 if (f->getLocation().isInvalid())
2218 f->setLocation(Attr.getLoc());
2219 return;
2220 }
2221 }
2222
Chandler Carruth87c44602011-07-01 23:49:12 +00002223 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002224 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002225 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002226}
2227
Chandler Carruth1b03c872011-07-02 00:01:44 +00002228static void handleTransparentUnionAttr(Sema &S, Decl *D,
2229 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002230 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002231 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002232 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002233
Chris Lattner6b6b5372008-06-26 18:38:35 +00002234
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002235 // Try to find the underlying union declaration.
2236 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002237 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002238 if (TD && TD->getUnderlyingType()->isUnionType())
2239 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2240 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002241 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002242
2243 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002244 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002245 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002246 return;
2247 }
2248
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002249 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002250 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002251 diag::warn_transparent_union_attribute_not_definition);
2252 return;
2253 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002254
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002255 RecordDecl::field_iterator Field = RD->field_begin(),
2256 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002257 if (Field == FieldEnd) {
2258 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2259 return;
2260 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002261
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002262 FieldDecl *FirstField = *Field;
2263 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002264 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002265 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002266 diag::warn_transparent_union_attribute_floating)
2267 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002268 return;
2269 }
2270
2271 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2272 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2273 for (; Field != FieldEnd; ++Field) {
2274 QualType FieldType = Field->getType();
2275 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2276 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2277 // Warn if we drop the attribute.
2278 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002279 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002280 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002281 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002282 diag::warn_transparent_union_attribute_field_size_align)
2283 << isSize << Field->getDeclName() << FieldBits;
2284 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002285 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002286 diag::note_transparent_union_first_field_size_align)
2287 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002288 return;
2289 }
2290 }
2291
Sean Huntcf807c42010-08-18 23:23:40 +00002292 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002293}
2294
Chandler Carruth1b03c872011-07-02 00:01:44 +00002295static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002296 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002297 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002298 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002299
Peter Collingbourne7a730022010-11-23 20:45:58 +00002300 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002301 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002302
Chris Lattner6b6b5372008-06-26 18:38:35 +00002303 // Make sure that there is a string literal as the annotation's single
2304 // argument.
2305 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002306 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002307 return;
2308 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002309 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002310 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002311}
2312
Chandler Carruth1b03c872011-07-02 00:01:44 +00002313static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002314 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002315 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002317 return;
2318 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002319
2320 //FIXME: The C++0x version of this attribute has more limited applicabilty
2321 // than GNU's, and should error out when it is used to specify a
2322 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002323
Chris Lattner545dd342008-06-28 23:36:30 +00002324 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002325 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002326 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002327 }
Mike Stumpbf916502009-07-24 19:02:52 +00002328
Peter Collingbourne7a730022010-11-23 20:45:58 +00002329 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002330}
2331
2332void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2333 if (E->isTypeDependent() || E->isValueDependent()) {
2334 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002335 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002336 return;
2337 }
2338
Sean Huntcf807c42010-08-18 23:23:40 +00002339 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002340 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002341 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2342 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2343 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002344 return;
2345 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002346 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002347 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2348 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002349 return;
2350 }
2351
Sean Huntcf807c42010-08-18 23:23:40 +00002352 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2353}
2354
2355void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2356 // FIXME: Cache the number on the Attr object if non-dependent?
2357 // FIXME: Perform checking of type validity
2358 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2359 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002360}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002361
Chandler Carruthd309c812011-07-01 23:49:16 +00002362/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002363/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002364///
Mike Stumpbf916502009-07-24 19:02:52 +00002365/// Despite what would be logical, the mode attribute is a decl attribute, not a
2366/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2367/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002368static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002369 // This attribute isn't documented, but glibc uses it. It changes
2370 // the width of an int or unsigned int to the specified size.
2371
2372 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002373 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002374 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002375
Chris Lattnerfbf13472008-06-27 22:18:37 +00002376
2377 IdentifierInfo *Name = Attr.getParameterName();
2378 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002379 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002380 return;
2381 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002382
Chris Lattner5f9e2722011-07-23 10:55:15 +00002383 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002384
2385 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002386 if (Str.startswith("__") && Str.endswith("__"))
2387 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002388
2389 unsigned DestWidth = 0;
2390 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002391 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002392 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002393 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002394 switch (Str[0]) {
2395 case 'Q': DestWidth = 8; break;
2396 case 'H': DestWidth = 16; break;
2397 case 'S': DestWidth = 32; break;
2398 case 'D': DestWidth = 64; break;
2399 case 'X': DestWidth = 96; break;
2400 case 'T': DestWidth = 128; break;
2401 }
2402 if (Str[1] == 'F') {
2403 IntegerMode = false;
2404 } else if (Str[1] == 'C') {
2405 IntegerMode = false;
2406 ComplexMode = true;
2407 } else if (Str[1] != 'I') {
2408 DestWidth = 0;
2409 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002410 break;
2411 case 4:
2412 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2413 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002414 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002415 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002416 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002417 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002418 break;
2419 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002420 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002421 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002422 break;
2423 }
2424
2425 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002426 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002427 OldTy = TD->getUnderlyingType();
2428 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2429 OldTy = VD->getType();
2430 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002431 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2432 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002433 return;
2434 }
Eli Friedman73397492009-03-03 06:41:03 +00002435
John McCall183700f2009-09-21 23:43:11 +00002436 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002437 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2438 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002439 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002440 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2441 } else if (ComplexMode) {
2442 if (!OldTy->isComplexType())
2443 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2444 } else {
2445 if (!OldTy->isFloatingType())
2446 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2447 }
2448
Mike Stump390b4cc2009-05-16 07:39:55 +00002449 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2450 // and friends, at least with glibc.
2451 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2452 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002453 // FIXME: Make sure floating-point mappings are accurate
2454 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002455 QualType NewTy;
2456 switch (DestWidth) {
2457 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002458 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002459 return;
2460 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002461 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002462 return;
2463 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002464 if (!IntegerMode) {
2465 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2466 return;
2467 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002468 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002469 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002470 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002471 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002472 break;
2473 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002474 if (!IntegerMode) {
2475 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2476 return;
2477 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002478 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002479 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002480 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002481 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002482 break;
2483 case 32:
2484 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002485 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002486 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002487 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002488 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002489 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002490 break;
2491 case 64:
2492 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002493 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002494 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002495 if (S.Context.Target.getLongWidth() == 64)
2496 NewTy = S.Context.LongTy;
2497 else
2498 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002499 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002500 if (S.Context.Target.getLongWidth() == 64)
2501 NewTy = S.Context.UnsignedLongTy;
2502 else
2503 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002504 break;
Eli Friedman73397492009-03-03 06:41:03 +00002505 case 96:
2506 NewTy = S.Context.LongDoubleTy;
2507 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002508 case 128:
2509 if (!IntegerMode) {
2510 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2511 return;
2512 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002513 if (OldTy->isSignedIntegerType())
2514 NewTy = S.Context.Int128Ty;
2515 else
2516 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002517 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002518 }
2519
Eli Friedman73397492009-03-03 06:41:03 +00002520 if (ComplexMode) {
2521 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002522 }
2523
2524 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002525 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002526 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002527 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002528 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002529 cast<ValueDecl>(D)->setType(NewTy);
2530}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002531
Chandler Carruth1b03c872011-07-02 00:01:44 +00002532static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002533 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002534 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002535 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002536
Chandler Carruth87c44602011-07-01 23:49:12 +00002537 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002538 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002539 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002540 return;
2541 }
Mike Stumpbf916502009-07-24 19:02:52 +00002542
Chandler Carruth87c44602011-07-01 23:49:12 +00002543 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002544}
2545
Chandler Carruth1b03c872011-07-02 00:01:44 +00002546static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002547 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002548 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002549 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002550
Mike Stumpbf916502009-07-24 19:02:52 +00002551
Chandler Carruth87c44602011-07-01 23:49:12 +00002552 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002553 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002554 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002555 return;
2556 }
Mike Stumpbf916502009-07-24 19:02:52 +00002557
Chandler Carruth87c44602011-07-01 23:49:12 +00002558 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002559}
2560
Chandler Carruth1b03c872011-07-02 00:01:44 +00002561static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2562 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002563 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002564 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002565 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002566
Chris Lattner7255a2d2010-06-22 00:03:40 +00002567
Chandler Carruth87c44602011-07-01 23:49:12 +00002568 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002569 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002570 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002571 return;
2572 }
2573
Chandler Carruth87c44602011-07-01 23:49:12 +00002574 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002575 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002576}
2577
Chandler Carruth1b03c872011-07-02 00:01:44 +00002578static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002579 if (S.LangOpts.CUDA) {
2580 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002581 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2583 return;
2584 }
2585
Chandler Carruth87c44602011-07-01 23:49:12 +00002586 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002588 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002589 return;
2590 }
2591
Chandler Carruth87c44602011-07-01 23:49:12 +00002592 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002593 } else {
2594 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2595 }
2596}
2597
Chandler Carruth1b03c872011-07-02 00:01:44 +00002598static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002599 if (S.LangOpts.CUDA) {
2600 // check the attribute arguments.
2601 if (Attr.getNumArgs() != 0) {
2602 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2603 return;
2604 }
2605
Chandler Carruth87c44602011-07-01 23:49:12 +00002606 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002608 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002609 return;
2610 }
2611
Chandler Carruth87c44602011-07-01 23:49:12 +00002612 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002613 } else {
2614 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2615 }
2616}
2617
Chandler Carruth1b03c872011-07-02 00:01:44 +00002618static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002619 if (S.LangOpts.CUDA) {
2620 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002621 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002622 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002623
Chandler Carruth87c44602011-07-01 23:49:12 +00002624 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002626 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002627 return;
2628 }
2629
Chandler Carruth87c44602011-07-01 23:49:12 +00002630 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002631 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002632 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002633 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2634 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2635 << FD->getType()
2636 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2637 "void");
2638 } else {
2639 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2640 << FD->getType();
2641 }
2642 return;
2643 }
2644
Chandler Carruth87c44602011-07-01 23:49:12 +00002645 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002646 } else {
2647 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2648 }
2649}
2650
Chandler Carruth1b03c872011-07-02 00:01:44 +00002651static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002652 if (S.LangOpts.CUDA) {
2653 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002654 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002655 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002656
Peter Collingbourneced76712010-12-01 03:15:31 +00002657
Chandler Carruth87c44602011-07-01 23:49:12 +00002658 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002660 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002661 return;
2662 }
2663
Chandler Carruth87c44602011-07-01 23:49:12 +00002664 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002665 } else {
2666 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2667 }
2668}
2669
Chandler Carruth1b03c872011-07-02 00:01:44 +00002670static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002671 if (S.LangOpts.CUDA) {
2672 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002673 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002674 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002675
Peter Collingbourneced76712010-12-01 03:15:31 +00002676
Chandler Carruth87c44602011-07-01 23:49:12 +00002677 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002679 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002680 return;
2681 }
2682
Chandler Carruth87c44602011-07-01 23:49:12 +00002683 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002684 } else {
2685 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2686 }
2687}
2688
Chandler Carruth1b03c872011-07-02 00:01:44 +00002689static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002690 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002691 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002692 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002693
Chandler Carruth87c44602011-07-01 23:49:12 +00002694 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002695 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002696 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002697 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002698 return;
2699 }
Mike Stumpbf916502009-07-24 19:02:52 +00002700
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002701 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002702 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002703 return;
2704 }
Mike Stumpbf916502009-07-24 19:02:52 +00002705
Chandler Carruth87c44602011-07-01 23:49:12 +00002706 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002707}
2708
Chandler Carruth1b03c872011-07-02 00:01:44 +00002709static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002710 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002711
Chandler Carruth87c44602011-07-01 23:49:12 +00002712 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00002713 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2714 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00002715 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00002716 return;
2717
Chandler Carruth87c44602011-07-01 23:49:12 +00002718 if (!isa<ObjCMethodDecl>(D)) {
2719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2720 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002721 return;
2722 }
2723
Chandler Carruth87c44602011-07-01 23:49:12 +00002724 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002725 case AttributeList::AT_fastcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002726 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002727 return;
2728 case AttributeList::AT_stdcall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002729 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002730 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002731 case AttributeList::AT_thiscall:
Chandler Carruth87c44602011-07-01 23:49:12 +00002732 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002733 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002734 case AttributeList::AT_cdecl:
Chandler Carruth87c44602011-07-01 23:49:12 +00002735 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002736 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002737 case AttributeList::AT_pascal:
Chandler Carruth87c44602011-07-01 23:49:12 +00002738 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002739 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002740 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00002741 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002742 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002743 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002744 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002745 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00002746 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002747 return;
2748 }
2749
Chris Lattner5f9e2722011-07-23 10:55:15 +00002750 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002751 PcsAttr::PCSType PCS;
2752 if (StrRef == "aapcs")
2753 PCS = PcsAttr::AAPCS;
2754 else if (StrRef == "aapcs-vfp")
2755 PCS = PcsAttr::AAPCS_VFP;
2756 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002757 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2758 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002759 return;
2760 }
2761
Chandler Carruth87c44602011-07-01 23:49:12 +00002762 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002763 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002764 default:
2765 llvm_unreachable("unexpected attribute kind");
2766 return;
2767 }
2768}
2769
Chandler Carruth1b03c872011-07-02 00:01:44 +00002770static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00002771 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00002772 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00002773}
2774
John McCall711c52b2011-01-05 12:14:39 +00002775bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2776 if (attr.isInvalid())
2777 return true;
2778
Ted Kremenek831efae2011-04-15 05:49:29 +00002779 if ((attr.getNumArgs() != 0 &&
2780 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2781 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002782 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2783 attr.setInvalid();
2784 return true;
2785 }
2786
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002787 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2788 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002789 switch (attr.getKind()) {
2790 case AttributeList::AT_cdecl: CC = CC_C; break;
2791 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2792 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2793 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2794 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002795 case AttributeList::AT_pcs: {
2796 Expr *Arg = attr.getArg(0);
2797 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00002798 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002799 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2800 << "pcs" << 1;
2801 attr.setInvalid();
2802 return true;
2803 }
2804
Chris Lattner5f9e2722011-07-23 10:55:15 +00002805 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002806 if (StrRef == "aapcs") {
2807 CC = CC_AAPCS;
2808 break;
2809 } else if (StrRef == "aapcs-vfp") {
2810 CC = CC_AAPCS_VFP;
2811 break;
2812 }
2813 // FALLS THROUGH
2814 }
John McCall711c52b2011-01-05 12:14:39 +00002815 default: llvm_unreachable("unexpected attribute kind"); return true;
2816 }
2817
2818 return false;
2819}
2820
Chandler Carruth1b03c872011-07-02 00:01:44 +00002821static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002822 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00002823
2824 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00002825 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00002826 return;
2827
Chandler Carruth87c44602011-07-01 23:49:12 +00002828 if (!isa<ObjCMethodDecl>(D)) {
2829 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2830 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002831 return;
2832 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002833
Chandler Carruth87c44602011-07-01 23:49:12 +00002834 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00002835}
2836
2837/// Checks a regparm attribute, returning true if it is ill-formed and
2838/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00002839bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2840 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00002841 return true;
2842
Chandler Carruth87c44602011-07-01 23:49:12 +00002843 if (Attr.getNumArgs() != 1) {
2844 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2845 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002846 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002847 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002848
Chandler Carruth87c44602011-07-01 23:49:12 +00002849 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002850 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002851 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002852 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002853 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002854 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002855 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002856 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002857 }
2858
John McCall711c52b2011-01-05 12:14:39 +00002859 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002860 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002861 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002862 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002863 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002864 }
2865
John McCall711c52b2011-01-05 12:14:39 +00002866 numParams = NumParams.getZExtValue();
2867 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002868 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall711c52b2011-01-05 12:14:39 +00002869 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00002870 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00002871 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002872 }
2873
John McCall711c52b2011-01-05 12:14:39 +00002874 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002875}
2876
Chandler Carruth1b03c872011-07-02 00:01:44 +00002877static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00002878 if (S.LangOpts.CUDA) {
2879 // check the attribute arguments.
2880 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002881 // FIXME: 0 is not okay.
2882 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002883 return;
2884 }
2885
Chandler Carruth87c44602011-07-01 23:49:12 +00002886 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00002887 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002888 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002889 return;
2890 }
2891
2892 Expr *MaxThreadsExpr = Attr.getArg(0);
2893 llvm::APSInt MaxThreads(32);
2894 if (MaxThreadsExpr->isTypeDependent() ||
2895 MaxThreadsExpr->isValueDependent() ||
2896 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2897 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2898 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2899 return;
2900 }
2901
2902 llvm::APSInt MinBlocks(32);
2903 if (Attr.getNumArgs() > 1) {
2904 Expr *MinBlocksExpr = Attr.getArg(1);
2905 if (MinBlocksExpr->isTypeDependent() ||
2906 MinBlocksExpr->isValueDependent() ||
2907 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2908 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2909 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2910 return;
2911 }
2912 }
2913
Chandler Carruth87c44602011-07-01 23:49:12 +00002914 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00002915 MaxThreads.getZExtValue(),
2916 MinBlocks.getZExtValue()));
2917 } else {
2918 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2919 }
2920}
2921
Chris Lattner0744e5f2008-06-29 00:23:49 +00002922//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002923// Checker-specific attribute handlers.
2924//===----------------------------------------------------------------------===//
2925
John McCallc7ad3812011-01-25 03:31:58 +00002926static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2927 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2928}
2929static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2930 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2931}
2932
Chandler Carruth1b03c872011-07-02 00:01:44 +00002933static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002934 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00002935 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002936 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2937 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002938 return;
2939 }
2940
2941 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002942 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00002943 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2944 cf = false;
2945 } else {
2946 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2947 cf = true;
2948 }
2949
2950 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002951 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2952 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00002953 return;
2954 }
2955
2956 if (cf)
Chandler Carruth87c44602011-07-01 23:49:12 +00002957 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002958 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002959 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002960}
2961
Chandler Carruth1b03c872011-07-02 00:01:44 +00002962static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
2963 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002964 if (!isa<ObjCMethodDecl>(D)) {
2965 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2966 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002967 return;
2968 }
2969
Chandler Carruth87c44602011-07-01 23:49:12 +00002970 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00002971}
2972
Chandler Carruth1b03c872011-07-02 00:01:44 +00002973static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
2974 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002975
John McCallc7ad3812011-01-25 03:31:58 +00002976 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002977
Chandler Carruth87c44602011-07-01 23:49:12 +00002978 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002979 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002980 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00002981 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00002982 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2983 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00002984 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00002985 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00002986 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002987 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002988 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2989 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002990 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002991 return;
2992 }
Mike Stumpbf916502009-07-24 19:02:52 +00002993
John McCallc7ad3812011-01-25 03:31:58 +00002994 bool typeOK;
2995 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00002996 switch (Attr.getKind()) {
John McCallc7ad3812011-01-25 03:31:58 +00002997 default: llvm_unreachable("invalid ownership attribute"); return;
2998 case AttributeList::AT_ns_returns_autoreleased:
2999 case AttributeList::AT_ns_returns_retained:
3000 case AttributeList::AT_ns_returns_not_retained:
3001 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3002 cf = false;
3003 break;
3004
3005 case AttributeList::AT_cf_returns_retained:
3006 case AttributeList::AT_cf_returns_not_retained:
3007 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3008 cf = true;
3009 break;
3010 }
3011
3012 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003013 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3014 << SourceRange(Attr.getLoc())
3015 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003016 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003017 }
Mike Stumpbf916502009-07-24 19:02:52 +00003018
Chandler Carruth87c44602011-07-01 23:49:12 +00003019 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003020 default:
3021 assert(0 && "invalid ownership attribute");
3022 return;
John McCallc7ad3812011-01-25 03:31:58 +00003023 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruth87c44602011-07-01 23:49:12 +00003024 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCallc7ad3812011-01-25 03:31:58 +00003025 S.Context));
3026 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003027 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003028 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003029 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003030 return;
3031 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003032 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003033 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003034 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003035 case AttributeList::AT_cf_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003036 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003037 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003038 return;
3039 case AttributeList::AT_ns_returns_retained:
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003041 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003042 return;
3043 };
3044}
3045
John McCalldc7c5ad2011-07-22 08:53:00 +00003046static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3047 const AttributeList &attr) {
3048 SourceLocation loc = attr.getLoc();
3049
3050 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3051
3052 if (!isa<ObjCMethodDecl>(method)) {
3053 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3054 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
3055 return;
3056 }
3057
3058 // Check that the method returns a normal pointer.
3059 QualType resultType = method->getResultType();
3060 if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
3061 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3062 << SourceRange(loc)
3063 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3064
3065 // Drop the attribute.
3066 return;
3067 }
3068
3069 method->addAttr(
3070 ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context));
3071}
3072
Chandler Carruth1b03c872011-07-02 00:01:44 +00003073static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3074 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003075 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003076
Chandler Carruth87c44602011-07-01 23:49:12 +00003077 SourceLocation L = Attr.getLoc();
3078 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3079 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003080}
3081
Chandler Carruth1b03c872011-07-02 00:01:44 +00003082static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3083 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003084 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
3085 SourceLocation L = Attr.getLoc();
3086 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3087 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCallf85e1932011-06-15 23:02:42 +00003088 return;
3089 }
3090
Chandler Carruth87c44602011-07-01 23:49:12 +00003091 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003092 QualType type = vd->getType();
3093
3094 if (!type->isDependentType() &&
3095 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003096 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003097 << type;
3098 return;
3099 }
3100
3101 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3102
3103 // If we have no lifetime yet, check the lifetime we're presumably
3104 // going to infer.
3105 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3106 lifetime = type->getObjCARCImplicitLifetime();
3107
3108 switch (lifetime) {
3109 case Qualifiers::OCL_None:
3110 assert(type->isDependentType() &&
3111 "didn't infer lifetime for non-dependent type?");
3112 break;
3113
3114 case Qualifiers::OCL_Weak: // meaningful
3115 case Qualifiers::OCL_Strong: // meaningful
3116 break;
3117
3118 case Qualifiers::OCL_ExplicitNone:
3119 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003120 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003121 << (lifetime == Qualifiers::OCL_Autoreleasing);
3122 break;
3123 }
3124
Chandler Carruth87c44602011-07-01 23:49:12 +00003125 D->addAttr(::new (S.Context)
3126 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003127}
3128
Charles Davisf0122fe2010-02-16 18:27:26 +00003129static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3130 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00003131 Attr.getKind() == AttributeList::AT_dllexport ||
3132 Attr.getKind() == AttributeList::AT_uuid;
3133}
3134
3135//===----------------------------------------------------------------------===//
3136// Microsoft specific attribute handlers.
3137//===----------------------------------------------------------------------===//
3138
Chandler Carruth1b03c872011-07-02 00:01:44 +00003139static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet11542142010-12-19 06:50:37 +00003140 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
3141 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003142 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003143 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003144
Francois Pichet11542142010-12-19 06:50:37 +00003145 Expr *Arg = Attr.getArg(0);
3146 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003147 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3149 << "uuid" << 1;
3150 return;
3151 }
3152
Chris Lattner5f9e2722011-07-23 10:55:15 +00003153 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003154
3155 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3156 StrRef.back() == '}';
3157
3158 // Validate GUID length.
3159 if (IsCurly && StrRef.size() != 38) {
3160 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3161 return;
3162 }
3163 if (!IsCurly && StrRef.size() != 36) {
3164 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3165 return;
3166 }
3167
3168 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3169 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003170 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003171 if (IsCurly) // Skip the optional '{'
3172 ++I;
3173
3174 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003175 if (i == 8 || i == 13 || i == 18 || i == 23) {
3176 if (*I != '-') {
3177 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3178 return;
3179 }
3180 } else if (!isxdigit(*I)) {
3181 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3182 return;
3183 }
3184 I++;
3185 }
Francois Pichet11542142010-12-19 06:50:37 +00003186
Chandler Carruth87c44602011-07-01 23:49:12 +00003187 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003188 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003189 } else
Francois Pichet11542142010-12-19 06:50:37 +00003190 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003191}
3192
Ted Kremenekb71368d2009-05-09 02:44:38 +00003193//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003194// Top Level Sema Entry Points
3195//===----------------------------------------------------------------------===//
3196
Chandler Carruth1b03c872011-07-02 00:01:44 +00003197static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3198 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003199 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003200 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3201 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3202 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003203 default:
3204 break;
3205 }
3206}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003207
Chandler Carruth1b03c872011-07-02 00:01:44 +00003208static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3209 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003210 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003211 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3212 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00003213 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003214 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003215 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003216 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003217 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003218 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003219 case AttributeList::AT_neon_vector_type:
3220 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003221 // Ignore these, these are type attributes, handled by
3222 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003223 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003224 case AttributeList::AT_device:
3225 case AttributeList::AT_host:
3226 case AttributeList::AT_overloadable:
3227 // Ignore, this is a non-inheritable attribute, handled
3228 // by ProcessNonInheritableDeclAttr.
3229 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003230 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3231 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003232 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003233 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003234 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003235 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3236 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3237 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003238 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003239 handleDependencyAttr (S, D, Attr); break;
3240 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3241 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3242 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3243 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3244 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003245 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003246 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003247 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003248 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3249 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3250 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3251 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003252 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003253 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003254 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003255 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3256 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3257 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3258 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3259 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003260 case AttributeList::AT_ownership_returns:
3261 case AttributeList::AT_ownership_takes:
3262 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003263 handleOwnershipAttr (S, D, Attr); break;
3264 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3265 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3266 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3267 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3268 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003269
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003270 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003271 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003272 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003273 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003274
John McCalldc7c5ad2011-07-22 08:53:00 +00003275 case AttributeList::AT_objc_returns_inner_pointer:
3276 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3277
Ted Kremenekb71368d2009-05-09 02:44:38 +00003278 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003279 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003280 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003281 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003282 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003283
3284 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003285 case AttributeList::AT_ns_returns_not_retained:
3286 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003287 case AttributeList::AT_ns_returns_retained:
3288 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003289 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003290
Nate Begeman6f3d8382009-06-26 06:32:41 +00003291 case AttributeList::AT_reqd_wg_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003292 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003293
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003294 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003295 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003296
Chandler Carruth1b03c872011-07-02 00:01:44 +00003297 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3298 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3299 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3300 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003301 case AttributeList::AT_arc_weakref_unavailable:
3302 handleArcWeakrefUnavailableAttr (S, D, Attr);
3303 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003304 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3305 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3306 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3307 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003308 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003309 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3310 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3311 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003312 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003313 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003314 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003315 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003316 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003317 break;
John McCalld5313b02011-03-02 11:33:24 +00003318 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003319 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003320 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003321 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3322 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3323 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3324 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3325 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3326 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3327 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3328 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3329 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003330 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003331 // Just ignore
3332 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003333 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003334 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003335 break;
John McCall04a67a62010-02-05 21:31:56 +00003336 case AttributeList::AT_stdcall:
3337 case AttributeList::AT_cdecl:
3338 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003339 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003340 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003341 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003342 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003343 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003344 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003345 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003346 break;
Francois Pichet11542142010-12-19 06:50:37 +00003347 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003348 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003349 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003350
3351 // Thread safety attributes:
3352 case AttributeList::AT_guarded_var:
3353 handleGuardedVarAttr(S, D, Attr);
3354 break;
3355 case AttributeList::AT_pt_guarded_var:
3356 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3357 break;
3358 case AttributeList::AT_scoped_lockable:
3359 handleLockableAttr(S, D, Attr, /*scoped = */true);
3360 break;
3361 case AttributeList::AT_no_thread_safety_analysis:
3362 handleNoThreadSafetyAttr(S, D, Attr);
3363 break;
3364 case AttributeList::AT_lockable:
3365 handleLockableAttr(S, D, Attr);
3366 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003367 case AttributeList::AT_guarded_by:
3368 handleGuardedByAttr(S, D, Attr);
3369 break;
3370 case AttributeList::AT_pt_guarded_by:
3371 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3372 break;
3373 case AttributeList::AT_exclusive_lock_function:
3374 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3375 break;
3376 case AttributeList::AT_exclusive_locks_required:
3377 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3378 break;
3379 case AttributeList::AT_exclusive_trylock_function:
3380 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3381 break;
3382 case AttributeList::AT_lock_returned:
3383 handleLockReturnedAttr(S, D, Attr);
3384 break;
3385 case AttributeList::AT_locks_excluded:
3386 handleLocksExcludedAttr(S, D, Attr);
3387 break;
3388 case AttributeList::AT_shared_lock_function:
3389 handleLockFunAttr(S, D, Attr);
3390 break;
3391 case AttributeList::AT_shared_locks_required:
3392 handleLocksRequiredAttr(S, D, Attr);
3393 break;
3394 case AttributeList::AT_shared_trylock_function:
3395 handleTrylockFunAttr(S, D, Attr);
3396 break;
3397 case AttributeList::AT_unlock_function:
3398 handleUnlockFunAttr(S, D, Attr);
3399 break;
3400 case AttributeList::AT_acquired_before:
3401 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3402 break;
3403 case AttributeList::AT_acquired_after:
3404 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3405 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003406
Chris Lattner803d0802008-06-29 00:43:07 +00003407 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003408 // Ask target about the attribute.
3409 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3410 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003411 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3412 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003413 break;
3414 }
3415}
3416
Peter Collingbourne60700392011-01-21 02:08:45 +00003417/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3418/// the attribute applies to decls. If the attribute is a type attribute, just
3419/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3420/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003421static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3422 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003423 bool NonInheritable, bool Inheritable) {
3424 if (Attr.isInvalid())
3425 return;
3426
3427 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3428 // FIXME: Try to deal with other __declspec attributes!
3429 return;
3430
3431 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003432 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003433
3434 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003435 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003436}
3437
Chris Lattner803d0802008-06-29 00:43:07 +00003438/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3439/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003440void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003441 const AttributeList *AttrList,
3442 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003443 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003444 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003445 }
3446
3447 // GCC accepts
3448 // static int a9 __attribute__((weakref));
3449 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003450 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003451 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003452 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003453 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003454 }
3455}
3456
Ryan Flynne25ff832009-07-30 03:15:39 +00003457/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3458/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003459NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003460 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003461 NamedDecl *NewD = 0;
3462 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3463 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003464 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003465 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003466 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003467 if (FD->getQualifier()) {
3468 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003469 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003470 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003471 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3472 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003473 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003474 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003475 VD->getStorageClass(),
3476 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003477 if (VD->getQualifier()) {
3478 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003479 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003480 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003481 }
3482 return NewD;
3483}
3484
3485/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3486/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003487void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003488 if (W.getUsed()) return; // only do this once
3489 W.setUsed(true);
3490 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3491 IdentifierInfo *NDId = ND->getIdentifier();
3492 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003493 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3494 NDId->getName()));
3495 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003496 WeakTopLevelDecl.push_back(NewD);
3497 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3498 // to insert Decl at TU scope, sorry.
3499 DeclContext *SavedContext = CurContext;
3500 CurContext = Context.getTranslationUnitDecl();
3501 PushOnScopeChains(NewD, S);
3502 CurContext = SavedContext;
3503 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003504 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003505 }
3506}
3507
Chris Lattner0744e5f2008-06-29 00:23:49 +00003508/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3509/// it, apply them to D. This is a bit tricky because PD can have attributes
3510/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003511void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3512 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003513 // It's valid to "forward-declare" #pragma weak, in which case we
3514 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003515 if (Inheritable) {
3516 LoadExternalWeakUndeclaredIdentifiers();
3517 if (!WeakUndeclaredIdentifiers.empty()) {
3518 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3519 if (IdentifierInfo *Id = ND->getIdentifier()) {
3520 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3521 = WeakUndeclaredIdentifiers.find(Id);
3522 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3523 WeakInfo W = I->second;
3524 DeclApplyPragmaWeak(S, ND, W);
3525 WeakUndeclaredIdentifiers[Id] = W;
3526 }
John McCalld4aff0e2010-10-27 00:59:00 +00003527 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003528 }
3529 }
3530 }
3531
Chris Lattner0744e5f2008-06-29 00:23:49 +00003532 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003533 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003534 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003535
Chris Lattner0744e5f2008-06-29 00:23:49 +00003536 // Walk the declarator structure, applying decl attributes that were in a type
3537 // position to the decl itself. This handles cases like:
3538 // int *__attr__(x)** D;
3539 // when X is a decl attribute.
3540 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3541 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003542 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003543
Chris Lattner0744e5f2008-06-29 00:23:49 +00003544 // Finally, apply any attributes on the decl itself.
3545 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003546 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003547}
John McCall54abf7d2009-11-04 02:18:39 +00003548
John McCallf85e1932011-06-15 23:02:42 +00003549/// Is the given declaration allowed to use a forbidden type?
3550static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3551 // Private ivars are always okay. Unfortunately, people don't
3552 // always properly make their ivars private, even in system headers.
3553 // Plus we need to make fields okay, too.
3554 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3555 return false;
3556
3557 // Require it to be declared in a system header.
3558 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3559}
3560
3561/// Handle a delayed forbidden-type diagnostic.
3562static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3563 Decl *decl) {
3564 if (decl && isForbiddenTypeAllowed(S, decl)) {
3565 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3566 "this system declaration uses an unsupported type"));
3567 return;
3568 }
3569
3570 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3571 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3572 diag.Triggered = true;
3573}
3574
John McCalleee1d542011-02-14 07:13:47 +00003575// This duplicates a vector push_back but hides the need to know the
3576// size of the type.
3577void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3578 assert(StackSize <= StackCapacity);
3579
3580 // Grow the stack if necessary.
3581 if (StackSize == StackCapacity) {
3582 unsigned newCapacity = 2 * StackCapacity + 2;
3583 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3584 const char *oldBuffer = (const char*) Stack;
3585
3586 if (StackCapacity)
3587 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3588
3589 delete[] oldBuffer;
3590 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3591 StackCapacity = newCapacity;
3592 }
3593
3594 assert(StackSize < StackCapacity);
3595 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003596}
3597
John McCalleee1d542011-02-14 07:13:47 +00003598void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3599 Decl *decl) {
3600 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003601
John McCalleee1d542011-02-14 07:13:47 +00003602 // Check the invariants.
3603 assert(DD.StackSize >= state.SavedStackSize);
3604 assert(state.SavedStackSize >= DD.ActiveStackBase);
3605 assert(DD.ParsingDepth > 0);
3606
3607 // Drop the parsing depth.
3608 DD.ParsingDepth--;
3609
3610 // If there are no active diagnostics, we're done.
3611 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003612 return;
3613
John McCall2f514482010-01-27 03:50:35 +00003614 // We only want to actually emit delayed diagnostics when we
3615 // successfully parsed a decl.
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +00003616 if (decl && !decl->isInvalidDecl()) {
John McCalleee1d542011-02-14 07:13:47 +00003617 // We emit all the active diagnostics, not just those starting
3618 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003619 // decl spec and another for each declarator; in a decl group like:
3620 // deprecated_typedef foo, *bar, baz();
3621 // only the declarator pops will be passed decls. This is correct;
3622 // we really do need to consider delayed diagnostics from the decl spec
3623 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003624 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3625 DelayedDiagnostic &diag = DD.Stack[i];
3626 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003627 continue;
3628
John McCalleee1d542011-02-14 07:13:47 +00003629 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003630 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003631 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003632 break;
3633
3634 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003635 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003636 break;
John McCallf85e1932011-06-15 23:02:42 +00003637
3638 case DelayedDiagnostic::ForbiddenType:
3639 handleDelayedForbiddenType(S, diag, decl);
3640 break;
John McCall2f514482010-01-27 03:50:35 +00003641 }
3642 }
3643 }
3644
John McCall58e6f342010-03-16 05:22:47 +00003645 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003646 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003647 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003648
John McCalleee1d542011-02-14 07:13:47 +00003649 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003650}
3651
3652static bool isDeclDeprecated(Decl *D) {
3653 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003654 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003655 return true;
3656 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3657 return false;
3658}
3659
John McCall9c3087b2010-08-26 02:13:20 +00003660void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003661 Decl *Ctx) {
3662 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003663 return;
3664
John McCall2f514482010-01-27 03:50:35 +00003665 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003666 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003667 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003668 << DD.getDeprecationDecl()->getDeclName()
3669 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003670 else
3671 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003672 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003673}
3674
Chris Lattner5f9e2722011-07-23 10:55:15 +00003675void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003676 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003677 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003678 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003679 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3680 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003681 return;
3682 }
3683
3684 // Otherwise, don't warn if our current context is deprecated.
3685 if (isDeclDeprecated(cast<Decl>(CurContext)))
3686 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003687 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003688 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3689 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003690 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003691 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003692 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003693 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003694 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003695 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3696 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003697 }
John McCall54abf7d2009-11-04 02:18:39 +00003698}