blob: 94a6f74a32f492cc35c075619ac31d044e943480 [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"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000028using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030
John McCall883cc2c2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
46 ExpectedStruct
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
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000198/// \brief Check if the attribute has exactly as many args as Num. May
199/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000200static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
201 unsigned int Num) {
202 if (Attr.getNumArgs() != Num) {
203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
204 return false;
205 }
206
207 return true;
208}
209
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000210
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000211/// \brief Check if the attribute has at least as many args as Num. May
212/// output an error.
213static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
214 unsigned int Num) {
215 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000216 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
217 return false;
218 }
219
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000220 return true;
221}
222
223///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000224/// \brief Check if passed in Decl is a field or potentially shared global var
225/// \return true if the Decl is a field or potentially shared global variable
226///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000227static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000228 if (isa<FieldDecl>(D))
229 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000230 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000231 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
232
233 return false;
234}
235
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000236/// \brief Check if the passed-in expression is of type int or bool.
237static bool isIntOrBool(Expr *Exp) {
238 QualType QT = Exp->getType();
239 return QT->isBooleanType() || QT->isIntegerType();
240}
241
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000242
243// Check to see if the type is a smart pointer of some kind. We assume
244// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000245static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
246 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
247 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
248 if (Res1.first == Res1.second)
249 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000250
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000251 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
252 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
253 if (Res2.first == Res2.second)
254 return false;
255
256 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000257}
258
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000259/// \brief Check if passed in Decl is a pointer type.
260/// Note that this function may produce an error message.
261/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000262static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
263 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000264 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000265 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000266 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000267 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000268
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000269 if (const RecordType *RT = QT->getAs<RecordType>()) {
270 // If it's an incomplete type, it could be a smart pointer; skip it.
271 // (We don't want to force template instantiation if we can avoid it,
272 // since that would alter the order in which templates are instantiated.)
273 if (RT->isIncompleteType())
274 return true;
275
276 if (threadSafetyCheckIsSmartPointer(S, RT))
277 return true;
278 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000279
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000280 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000281 << Attr.getName()->getName() << QT;
282 } else {
283 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
284 << Attr.getName();
285 }
286 return false;
287}
288
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000289/// \brief Checks that the passed in QualType either is of RecordType or points
290/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000291static const RecordType *getRecordType(QualType QT) {
292 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000293 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000294
295 // Now check if we point to record type.
296 if (const PointerType *PT = QT->getAs<PointerType>())
297 return PT->getPointeeType()->getAs<RecordType>();
298
299 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000300}
301
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000302
Jordy Rosefad5de92012-05-08 03:27:22 +0000303static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
304 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000305 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
306 if (RT->getDecl()->getAttr<LockableAttr>())
307 return true;
308 return false;
309}
310
311
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000312/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000313/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000314static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
315 QualType Ty) {
316 const RecordType *RT = getRecordType(Ty);
317
318 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000319 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000320 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000321 << Attr.getName() << Ty.getAsString();
322 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000323 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000324
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000325 // Don't check for lockable if the class hasn't been defined yet.
326 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000327 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000328
329 // Allow smart pointers to be used as lockable objects.
330 // FIXME -- Check the type that the smart pointer points to.
331 if (threadSafetyCheckIsSmartPointer(S, RT))
332 return;
333
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000334 // Check if the type is lockable.
335 RecordDecl *RD = RT->getDecl();
336 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000337 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000338
339 // Else check if any base classes are lockable.
340 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
341 CXXBasePaths BPaths(false, false);
342 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
343 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000344 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000345
346 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
347 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000348}
349
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000351/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000352/// \param Sidx The attribute argument index to start checking with.
353/// \param ParamIdxOk Whether an argument can be indexing into a function
354/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000355static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000356 const AttributeList &Attr,
357 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000358 int Sidx = 0,
359 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000360 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000361 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000362
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000363 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000364 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000365 Args.push_back(ArgExp);
366 continue;
367 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000368
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000369 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
370 // Ignore empty strings without warnings
371 if (StrLit->getLength() == 0)
372 continue;
373
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000374 // We allow constant strings to be used as a placeholder for expressions
375 // that are not valid C++ syntax, but warn that they are ignored.
376 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
377 Attr.getName();
378 continue;
379 }
380
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000381 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000382
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000383 // A pointer to member expression of the form &MyClass::mu is treated
384 // specially -- we need to look at the type of the member.
385 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
386 if (UOp->getOpcode() == UO_AddrOf)
387 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
388 if (DRE->getDecl()->isCXXInstanceMember())
389 ArgTy = DRE->getDecl()->getType();
390
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000391 // First see if we can just cast to record type, or point to record type.
392 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000393
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000394 // Now check if we index into a record type function param.
395 if(!RT && ParamIdxOk) {
396 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000397 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
398 if(FD && IL) {
399 unsigned int NumParams = FD->getNumParams();
400 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000401 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
402 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
403 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
405 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000406 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000407 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000408 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000409 }
410 }
411
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000412 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000413
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000414 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000415 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000416}
417
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000418//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000419// Attribute Implementations
420//===----------------------------------------------------------------------===//
421
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000422// FIXME: All this manual attribute parsing code is gross. At the
423// least add some helper functions to check most argument patterns (#
424// and types of args).
425
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000426static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
427 bool pointer = false) {
428 assert(!Attr.isInvalid());
429
430 if (!checkAttributeNumArgs(S, Attr, 0))
431 return;
432
433 // D must be either a member field or global (potentially shared) variable.
434 if (!mayBeSharedVariable(D)) {
435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000436 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000437 return;
438 }
439
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000440 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000441 return;
442
443 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000444 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000445 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000446 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000447}
448
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000449static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000450 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000451 assert(!Attr.isInvalid());
452
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000453 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000454 return;
455
456 // D must be either a member field or global (potentially shared) variable.
457 if (!mayBeSharedVariable(D)) {
458 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000460 return;
461 }
462
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000463 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000464 return;
465
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000466 SmallVector<Expr*, 1> Args;
467 // check that all arguments are lockable objects
468 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
469 unsigned Size = Args.size();
470 if (Size != 1)
471 return;
472 Expr *Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000473
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000474 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000475 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000476 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000477 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000478 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000479}
480
481
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000482static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
483 bool scoped = false) {
484 assert(!Attr.isInvalid());
485
486 if (!checkAttributeNumArgs(S, Attr, 0))
487 return;
488
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000489 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000490 if (!isa<CXXRecordDecl>(D)) {
491 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
492 << Attr.getName() << ExpectedClass;
493 return;
494 }
495
496 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000497 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000498 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000499 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500}
501
502static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
503 const AttributeList &Attr) {
504 assert(!Attr.isInvalid());
505
506 if (!checkAttributeNumArgs(S, Attr, 0))
507 return;
508
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000509 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000510 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
511 << Attr.getName() << ExpectedFunctionOrMethod;
512 return;
513 }
514
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000515 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000516 S.Context));
517}
518
Kostya Serebryany71efba02012-01-24 19:25:38 +0000519static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000520 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000521 assert(!Attr.isInvalid());
522
523 if (!checkAttributeNumArgs(S, Attr, 0))
524 return;
525
526 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
528 << Attr.getName() << ExpectedFunctionOrMethod;
529 return;
530 }
531
532 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
533 S.Context));
534}
535
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000536static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
537 bool before) {
538 assert(!Attr.isInvalid());
539
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000540 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000541 return;
542
543 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000544 ValueDecl *VD = dyn_cast<ValueDecl>(D);
545 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000547 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000548 return;
549 }
550
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000551 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000552 QualType QT = VD->getType();
553 if (!QT->isDependentType()) {
554 const RecordType *RT = getRecordType(QT);
555 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000556 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000557 << Attr.getName();
558 return;
559 }
560 }
561
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000562 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000563 // Check that all arguments are lockable objects.
564 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000565 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000566 if (Size == 0)
567 return;
568 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000569
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000570 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000571 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000572 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000573 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000574 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000575 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000576}
577
578static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000579 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000580 assert(!Attr.isInvalid());
581
582 // zero or more arguments ok
583
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000584 // check that the attribute is applied to a function
585 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
587 << Attr.getName() << ExpectedFunctionOrMethod;
588 return;
589 }
590
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000591 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000592 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000593 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000594 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000595 Expr **StartArg = Size == 0 ? 0 : &Args[0];
596
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000597 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000598 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000599 S.Context, StartArg,
600 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000601 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000602 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000603 S.Context, StartArg,
604 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000605}
606
607static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000608 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000609 assert(!Attr.isInvalid());
610
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000611 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000612 return;
613
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000614 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
616 << Attr.getName() << ExpectedFunctionOrMethod;
617 return;
618 }
619
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000620 if (!isIntOrBool(Attr.getArg(0))) {
621 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
622 << Attr.getName();
623 return;
624 }
625
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000626 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000627 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000628 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000629 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000630 Expr **StartArg = Size == 0 ? 0 : &Args[0];
631
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000632 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000633 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000634 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000635 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000636 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000637 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000638 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000639 S.Context,
640 Attr.getArg(0),
641 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000642}
643
644static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000645 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000646 assert(!Attr.isInvalid());
647
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000648 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000649 return;
650
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000651 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
653 << Attr.getName() << ExpectedFunctionOrMethod;
654 return;
655 }
656
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000657 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000658 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000659 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000660 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000661 if (Size == 0)
662 return;
663 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000664
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000665 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000666 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000667 S.Context, StartArg,
668 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000669 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000670 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000671 S.Context, StartArg,
672 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000673}
674
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000675static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000676 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000677 assert(!Attr.isInvalid());
678
679 // zero or more arguments ok
680
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000681 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000682 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
683 << Attr.getName() << ExpectedFunctionOrMethod;
684 return;
685 }
686
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000687 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000688 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000689 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000690 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000691 Expr **StartArg = Size == 0 ? 0 : &Args[0];
692
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000693 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000694 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000695}
696
697static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000699 assert(!Attr.isInvalid());
700
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000701 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000702 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000703 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000704
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
707 << Attr.getName() << ExpectedFunctionOrMethod;
708 return;
709 }
710
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000711 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000712 return;
713
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000714 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000715 SmallVector<Expr*, 1> Args;
716 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
717 unsigned Size = Args.size();
718 if (Size == 0)
719 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000720
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000721 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
722 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000723}
724
725static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000726 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000727 assert(!Attr.isInvalid());
728
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000729 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000730 return;
731
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000732 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000733 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
734 << Attr.getName() << ExpectedFunctionOrMethod;
735 return;
736 }
737
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000738 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000739 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000740 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000741 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000742 if (Size == 0)
743 return;
744 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000745
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000746 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000747 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000748}
749
750
Chandler Carruth1b03c872011-07-02 00:01:44 +0000751static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
752 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000753 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000754 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000755 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000756 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000757 }
Mike Stumpbf916502009-07-24 19:02:52 +0000758
Chris Lattner6b6b5372008-06-26 18:38:35 +0000759 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000760
761 Expr *sizeExpr;
762
763 // Special case where the argument is a template id.
764 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000765 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000766 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000767 UnqualifiedId id;
768 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000769
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000770 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
771 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000772 if (Size.isInvalid())
773 return;
774
775 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000776 } else {
777 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000778 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000779 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000780
Peter Collingbourne7a730022010-11-23 20:45:58 +0000781 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000782 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000783
784 // Instantiate/Install the vector type, and let Sema build the type for us.
785 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000786 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000787 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000788 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000789 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000790
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000791 // Remember this typedef decl, we will need it later for diagnostics.
792 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000793 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000794}
795
Chandler Carruth1b03c872011-07-02 00:01:44 +0000796static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000797 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000798 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000799 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000800
Chandler Carruth87c44602011-07-01 23:49:12 +0000801 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000802 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000803 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000804 // If the alignment is less than or equal to 8 bits, the packed attribute
805 // has no effect.
806 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000807 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000808 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000809 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000810 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000811 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000812 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000813 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000814}
815
Chandler Carruth1b03c872011-07-02 00:01:44 +0000816static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000817 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000818 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000819 else
820 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
821}
822
Chandler Carruth1b03c872011-07-02 00:01:44 +0000823static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000824 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000825 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000826 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000827
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000828 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000829 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000830 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000831 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000832 return;
833 }
834
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000835 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000836}
837
Ted Kremenek2f041d02011-09-29 07:02:25 +0000838static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
839 // The IBOutlet/IBOutletCollection attributes only apply to instance
840 // variables or properties of Objective-C classes. The outlet must also
841 // have an object reference type.
842 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
843 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000844 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000845 << Attr.getName() << VD->getType() << 0;
846 return false;
847 }
848 }
849 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
850 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000851 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000852 << Attr.getName() << PD->getType() << 1;
853 return false;
854 }
855 }
856 else {
857 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
858 return false;
859 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000860
Ted Kremenek2f041d02011-09-29 07:02:25 +0000861 return true;
862}
863
Chandler Carruth1b03c872011-07-02 00:01:44 +0000864static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000865 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000866 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000867 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000868
869 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000870 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000871
Ted Kremenek2f041d02011-09-29 07:02:25 +0000872 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000873}
874
Chandler Carruth1b03c872011-07-02 00:01:44 +0000875static void handleIBOutletCollection(Sema &S, Decl *D,
876 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000877
878 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000879 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000880 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
881 return;
882 }
883
Ted Kremenek2f041d02011-09-29 07:02:25 +0000884 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000885 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000886
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000887 IdentifierInfo *II = Attr.getParameterName();
888 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000889 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000890
John McCallb3d87482010-08-24 05:47:05 +0000891 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000892 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000893 if (!TypeRep) {
894 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
895 return;
896 }
John McCallb3d87482010-08-24 05:47:05 +0000897 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000898 // Diagnose use of non-object type in iboutletcollection attribute.
899 // FIXME. Gnu attribute extension ignores use of builtin types in
900 // attributes. So, __attribute__((iboutletcollection(char))) will be
901 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000902 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000903 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
904 return;
905 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000906 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
907 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000908}
909
Chandler Carruthd309c812011-07-01 23:49:16 +0000910static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000911 if (const RecordType *UT = T->getAsUnionType())
912 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
913 RecordDecl *UD = UT->getDecl();
914 for (RecordDecl::field_iterator it = UD->field_begin(),
915 itend = UD->field_end(); it != itend; ++it) {
916 QualType QT = it->getType();
917 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
918 T = QT;
919 return;
920 }
921 }
922 }
923}
924
Nuno Lopes587de5b2012-05-24 00:22:00 +0000925static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +0000926 if (!isFunctionOrMethod(D)) {
927 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
928 << "alloc_size" << ExpectedFunctionOrMethod;
929 return;
930 }
931
Nuno Lopes587de5b2012-05-24 00:22:00 +0000932 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
933 return;
934
935 // In C++ the implicit 'this' function parameter also counts, and they are
936 // counted from one.
937 bool HasImplicitThisParam = isInstanceMethod(D);
938 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
939
940 SmallVector<unsigned, 8> SizeArgs;
941
942 for (AttributeList::arg_iterator I = Attr.arg_begin(),
943 E = Attr.arg_end(); I!=E; ++I) {
944 // The argument must be an integer constant expression.
945 Expr *Ex = *I;
946 llvm::APSInt ArgNum;
947 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
948 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
949 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
950 << "alloc_size" << Ex->getSourceRange();
951 return;
952 }
953
954 uint64_t x = ArgNum.getZExtValue();
955
956 if (x < 1 || x > NumArgs) {
957 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
958 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
959 return;
960 }
961
962 --x;
963 if (HasImplicitThisParam) {
964 if (x == 0) {
965 S.Diag(Attr.getLoc(),
966 diag::err_attribute_invalid_implicit_this_argument)
967 << "alloc_size" << Ex->getSourceRange();
968 return;
969 }
970 --x;
971 }
972
973 // check if the function argument is of an integer type
974 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
975 if (!T->isIntegerType()) {
976 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
977 << "alloc_size" << Ex->getSourceRange();
978 return;
979 }
980
Nuno Lopes587de5b2012-05-24 00:22:00 +0000981 SizeArgs.push_back(x);
982 }
983
984 // check if the function returns a pointer
985 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
986 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
987 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
988 }
989
Nuno Lopes96c67d12012-06-18 16:27:56 +0000990 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
991 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +0000992}
993
Chandler Carruth1b03c872011-07-02 00:01:44 +0000994static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000995 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
996 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000997 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000998 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000999 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001000 return;
1001 }
Mike Stumpbf916502009-07-24 19:02:52 +00001002
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001003 // In C++ the implicit 'this' function parameter also counts, and they are
1004 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001005 bool HasImplicitThisParam = isInstanceMethod(D);
1006 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001007
1008 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001009 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001010
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001011 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1012 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001013
1014
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001015 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001016 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001017 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001018 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1019 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1021 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001022 return;
1023 }
Mike Stumpbf916502009-07-24 19:02:52 +00001024
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001025 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001026
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001027 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001029 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001030 return;
1031 }
Mike Stumpbf916502009-07-24 19:02:52 +00001032
Ted Kremenek465172f2008-07-21 22:09:15 +00001033 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001034 if (HasImplicitThisParam) {
1035 if (x == 0) {
1036 S.Diag(Attr.getLoc(),
1037 diag::err_attribute_invalid_implicit_this_argument)
1038 << "nonnull" << Ex->getSourceRange();
1039 return;
1040 }
1041 --x;
1042 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001043
1044 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001045 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001046 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001047
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001048 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001049 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001050 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001051 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001052 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001053 }
Mike Stumpbf916502009-07-24 19:02:52 +00001054
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001055 NonNullArgs.push_back(x);
1056 }
Mike Stumpbf916502009-07-24 19:02:52 +00001057
1058 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1059 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001060 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001061 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1062 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001063 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001064 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001065 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001066 }
Mike Stumpbf916502009-07-24 19:02:52 +00001067
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001068 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001069 if (NonNullArgs.empty()) {
1070 // Warn the trivial case only if attribute is not coming from a
1071 // macro instantiation.
1072 if (Attr.getLoc().isFileID())
1073 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001074 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001075 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001076 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001077
1078 unsigned* start = &NonNullArgs[0];
1079 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001080 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001081 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001082 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001083}
1084
Chandler Carruth1b03c872011-07-02 00:01:44 +00001085static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001086 // This attribute must be applied to a function declaration.
1087 // The first argument to the attribute must be a string,
1088 // the name of the resource, for example "malloc".
1089 // The following arguments must be argument indexes, the arguments must be
1090 // of integer type for Returns, otherwise of pointer type.
1091 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001092 // after being held. free() should be __attribute((ownership_takes)), whereas
1093 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001094
1095 if (!AL.getParameterName()) {
1096 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1097 << AL.getName()->getName() << 1;
1098 return;
1099 }
1100 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001101 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001102 switch (AL.getKind()) {
1103 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001104 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001105 if (AL.getNumArgs() < 1) {
1106 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1107 return;
1108 }
Jordy Rose2a479922010-08-12 08:54:03 +00001109 break;
1110 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001111 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001112 if (AL.getNumArgs() < 1) {
1113 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1114 return;
1115 }
Jordy Rose2a479922010-08-12 08:54:03 +00001116 break;
1117 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001118 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001119 if (AL.getNumArgs() > 1) {
1120 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1121 << AL.getNumArgs() + 1;
1122 return;
1123 }
Jordy Rose2a479922010-08-12 08:54:03 +00001124 break;
1125 default:
1126 // This should never happen given how we are called.
1127 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001128 }
1129
Chandler Carruth87c44602011-07-01 23:49:12 +00001130 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001131 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1132 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001133 return;
1134 }
1135
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001136 // In C++ the implicit 'this' function parameter also counts, and they are
1137 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001138 bool HasImplicitThisParam = isInstanceMethod(D);
1139 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001140
Chris Lattner5f9e2722011-07-23 10:55:15 +00001141 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001142
1143 // Normalize the argument, __foo__ becomes foo.
1144 if (Module.startswith("__") && Module.endswith("__"))
1145 Module = Module.substr(2, Module.size() - 4);
1146
Chris Lattner5f9e2722011-07-23 10:55:15 +00001147 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001148
Jordy Rose2a479922010-08-12 08:54:03 +00001149 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1150 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001151
Peter Collingbourne7a730022010-11-23 20:45:58 +00001152 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001153 llvm::APSInt ArgNum(32);
1154 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1155 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1156 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1157 << AL.getName()->getName() << IdxExpr->getSourceRange();
1158 continue;
1159 }
1160
1161 unsigned x = (unsigned) ArgNum.getZExtValue();
1162
1163 if (x > NumArgs || x < 1) {
1164 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1165 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1166 continue;
1167 }
1168 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001169 if (HasImplicitThisParam) {
1170 if (x == 0) {
1171 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1172 << "ownership" << IdxExpr->getSourceRange();
1173 return;
1174 }
1175 --x;
1176 }
1177
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001178 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001179 case OwnershipAttr::Takes:
1180 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001181 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001182 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001183 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1184 // FIXME: Should also highlight argument in decl.
1185 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001186 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001187 << "pointer"
1188 << IdxExpr->getSourceRange();
1189 continue;
1190 }
1191 break;
1192 }
Sean Huntcf807c42010-08-18 23:23:40 +00001193 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001194 if (AL.getNumArgs() > 1) {
1195 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001196 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001197 llvm::APSInt ArgNum(32);
1198 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1199 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1200 S.Diag(AL.getLoc(), diag::err_ownership_type)
1201 << "ownership_returns" << "integer"
1202 << IdxExpr->getSourceRange();
1203 return;
1204 }
1205 }
1206 break;
1207 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001208 } // switch
1209
1210 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001211 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001212 i = D->specific_attr_begin<OwnershipAttr>(),
1213 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001214 i != e; ++i) {
1215 if ((*i)->getOwnKind() != K) {
1216 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1217 I!=E; ++I) {
1218 if (x == *I) {
1219 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1220 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001221 }
1222 }
1223 }
1224 }
1225 OwnershipArgs.push_back(x);
1226 }
1227
1228 unsigned* start = OwnershipArgs.data();
1229 unsigned size = OwnershipArgs.size();
1230 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001231
1232 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1233 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1234 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001235 }
Sean Huntcf807c42010-08-18 23:23:40 +00001236
Chandler Carruth87c44602011-07-01 23:49:12 +00001237 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001238 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001239}
1240
John McCall332bb2a2011-02-08 22:35:49 +00001241/// Whether this declaration has internal linkage for the purposes of
1242/// things that want to complain about things not have internal linkage.
1243static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1244 switch (D->getLinkage()) {
1245 case NoLinkage:
1246 case InternalLinkage:
1247 return true;
1248
1249 // Template instantiations that go from external to unique-external
1250 // shouldn't get diagnosed.
1251 case UniqueExternalLinkage:
1252 return true;
1253
1254 case ExternalLinkage:
1255 return false;
1256 }
1257 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001258}
1259
Chandler Carruth1b03c872011-07-02 00:01:44 +00001260static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001261 // Check the attribute arguments.
1262 if (Attr.getNumArgs() > 1) {
1263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1264 return;
1265 }
1266
Chandler Carruth87c44602011-07-01 23:49:12 +00001267 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001269 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001270 return;
1271 }
1272
Chandler Carruth87c44602011-07-01 23:49:12 +00001273 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001274
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001275 // gcc rejects
1276 // class c {
1277 // static int a __attribute__((weakref ("v2")));
1278 // static int b() __attribute__((weakref ("f3")));
1279 // };
1280 // and ignores the attributes of
1281 // void f(void) {
1282 // static int a __attribute__((weakref ("v2")));
1283 // }
1284 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001285 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001286 if (!Ctx->isFileContext()) {
1287 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001288 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001289 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001290 }
1291
1292 // The GCC manual says
1293 //
1294 // At present, a declaration to which `weakref' is attached can only
1295 // be `static'.
1296 //
1297 // It also says
1298 //
1299 // Without a TARGET,
1300 // given as an argument to `weakref' or to `alias', `weakref' is
1301 // equivalent to `weak'.
1302 //
1303 // gcc 4.4.1 will accept
1304 // int a7 __attribute__((weakref));
1305 // as
1306 // int a7 __attribute__((weak));
1307 // This looks like a bug in gcc. We reject that for now. We should revisit
1308 // it if this behaviour is actually used.
1309
John McCall332bb2a2011-02-08 22:35:49 +00001310 if (!hasEffectivelyInternalLinkage(nd)) {
1311 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001312 return;
1313 }
1314
1315 // GCC rejects
1316 // static ((alias ("y"), weakref)).
1317 // Should we? How to check that weakref is before or after alias?
1318
1319 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001320 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001321 Arg = Arg->IgnoreParenCasts();
1322 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1323
Douglas Gregor5cee1192011-07-27 05:40:30 +00001324 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1326 << "weakref" << 1;
1327 return;
1328 }
1329 // GCC will accept anything as the argument of weakref. Should we
1330 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001331 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001332 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001333 }
1334
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001335 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001336}
1337
Chandler Carruth1b03c872011-07-02 00:01:44 +00001338static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001339 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001340 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001342 return;
1343 }
Mike Stumpbf916502009-07-24 19:02:52 +00001344
Peter Collingbourne7a730022010-11-23 20:45:58 +00001345 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001346 Arg = Arg->IgnoreParenCasts();
1347 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001348
Douglas Gregor5cee1192011-07-27 05:40:30 +00001349 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001350 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001351 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001352 return;
1353 }
Mike Stumpbf916502009-07-24 19:02:52 +00001354
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001355 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001356 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1357 return;
1358 }
1359
Chris Lattner6b6b5372008-06-26 18:38:35 +00001360 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001361
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001362 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001363 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001364}
1365
Benjamin Krameree409a92012-05-12 21:10:52 +00001366static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1367 // Check the attribute arguments.
1368 if (!checkAttributeNumArgs(S, Attr, 0))
1369 return;
1370
1371 if (!isa<FunctionDecl>(D)) {
1372 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1373 << Attr.getName() << ExpectedFunction;
1374 return;
1375 }
1376
1377 if (D->hasAttr<HotAttr>()) {
1378 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1379 << Attr.getName() << "hot";
1380 return;
1381 }
1382
1383 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1384}
1385
1386static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1387 // Check the attribute arguments.
1388 if (!checkAttributeNumArgs(S, Attr, 0))
1389 return;
1390
1391 if (!isa<FunctionDecl>(D)) {
1392 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1393 << Attr.getName() << ExpectedFunction;
1394 return;
1395 }
1396
1397 if (D->hasAttr<ColdAttr>()) {
1398 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1399 << Attr.getName() << "cold";
1400 return;
1401 }
1402
1403 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1404}
1405
Chandler Carruth1b03c872011-07-02 00:01:44 +00001406static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001407 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001408 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001409 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001410
Chandler Carruth87c44602011-07-01 23:49:12 +00001411 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001413 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001414 return;
1415 }
1416
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001417 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001418}
1419
Chandler Carruth1b03c872011-07-02 00:01:44 +00001420static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1421 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001422 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001423 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1425 return;
1426 }
1427
Chandler Carruth87c44602011-07-01 23:49:12 +00001428 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001430 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001431 return;
1432 }
Mike Stumpbf916502009-07-24 19:02:52 +00001433
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001434 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001435}
1436
Chandler Carruth1b03c872011-07-02 00:01:44 +00001437static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001438 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001439 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001440 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1441 return;
1442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Chandler Carruth87c44602011-07-01 23:49:12 +00001444 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001445 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001446 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001447 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001448 return;
1449 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001450 }
1451
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001452 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001453}
1454
Chandler Carruth1b03c872011-07-02 00:01:44 +00001455static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001456 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001457 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001458 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001459
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001460 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001461}
1462
Chandler Carruth1b03c872011-07-02 00:01:44 +00001463static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001464 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001465 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001466 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001467 else
1468 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001469 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001470}
1471
Chandler Carruth1b03c872011-07-02 00:01:44 +00001472static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001473 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001474 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001475 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001476 else
1477 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001478 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001479}
1480
Chandler Carruth1b03c872011-07-02 00:01:44 +00001481static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001482 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001483
1484 if (S.CheckNoReturnAttr(attr)) return;
1485
Chandler Carruth87c44602011-07-01 23:49:12 +00001486 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001487 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001488 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001489 return;
1490 }
1491
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001492 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001493}
1494
1495bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001496 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001497 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1498 attr.setInvalid();
1499 return true;
1500 }
1501
1502 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001503}
1504
Chandler Carruth1b03c872011-07-02 00:01:44 +00001505static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1506 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001507
1508 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1509 // because 'analyzer_noreturn' does not impact the type.
1510
Chandler Carruth1731e202011-07-11 23:30:35 +00001511 if(!checkAttributeNumArgs(S, Attr, 0))
1512 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001513
Chandler Carruth87c44602011-07-01 23:49:12 +00001514 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1515 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001516 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1517 && !VD->getType()->isFunctionPointerType())) {
1518 S.Diag(Attr.getLoc(),
1519 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1520 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001521 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001522 return;
1523 }
1524 }
1525
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001526 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001527}
1528
John Thompson35cc9622010-08-09 21:53:52 +00001529// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001530static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001531/*
1532 Returning a Vector Class in Registers
1533
Eric Christopherf48f3672010-12-01 22:13:54 +00001534 According to the PPU ABI specifications, a class with a single member of
1535 vector type is returned in memory when used as the return value of a function.
1536 This results in inefficient code when implementing vector classes. To return
1537 the value in a single vector register, add the vecreturn attribute to the
1538 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001539
1540 Example:
1541
1542 struct Vector
1543 {
1544 __vector float xyzw;
1545 } __attribute__((vecreturn));
1546
1547 Vector Add(Vector lhs, Vector rhs)
1548 {
1549 Vector result;
1550 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1551 return result; // This will be returned in a register
1552 }
1553*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001554 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001556 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001557 return;
1558 }
1559
Chandler Carruth87c44602011-07-01 23:49:12 +00001560 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001561 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1562 return;
1563 }
1564
Chandler Carruth87c44602011-07-01 23:49:12 +00001565 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001566 int count = 0;
1567
1568 if (!isa<CXXRecordDecl>(record)) {
1569 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1570 return;
1571 }
1572
1573 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1574 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1575 return;
1576 }
1577
Eric Christopherf48f3672010-12-01 22:13:54 +00001578 for (RecordDecl::field_iterator iter = record->field_begin();
1579 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001580 if ((count == 1) || !iter->getType()->isVectorType()) {
1581 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1582 return;
1583 }
1584 count++;
1585 }
1586
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001587 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001588}
1589
Chandler Carruth1b03c872011-07-02 00:01:44 +00001590static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001591 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001592 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001593 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001594 return;
1595 }
1596 // FIXME: Actually store the attribute on the declaration
1597}
1598
Chandler Carruth1b03c872011-07-02 00:01:44 +00001599static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001600 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001601 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001602 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001603 return;
1604 }
Mike Stumpbf916502009-07-24 19:02:52 +00001605
Chandler Carruth87c44602011-07-01 23:49:12 +00001606 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001607 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001609 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001610 return;
1611 }
Mike Stumpbf916502009-07-24 19:02:52 +00001612
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001613 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001614}
1615
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001616static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1617 const AttributeList &Attr) {
1618 // check the attribute arguments.
1619 if (Attr.hasParameterOrArguments()) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1621 return;
1622 }
1623
1624 if (!isa<FunctionDecl>(D)) {
1625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1626 << Attr.getName() << ExpectedFunction;
1627 return;
1628 }
1629
1630 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1631}
1632
Chandler Carruth1b03c872011-07-02 00:01:44 +00001633static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001634 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001635 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1637 return;
1638 }
Mike Stumpbf916502009-07-24 19:02:52 +00001639
Chandler Carruth87c44602011-07-01 23:49:12 +00001640 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001641 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001642 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1643 return;
1644 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001645 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001646 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001647 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001648 return;
1649 }
Mike Stumpbf916502009-07-24 19:02:52 +00001650
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001651 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001652}
1653
Chandler Carruth1b03c872011-07-02 00:01:44 +00001654static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001655 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001656 if (Attr.getNumArgs() > 1) {
1657 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001658 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001659 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001660
1661 int priority = 65535; // FIXME: Do not hardcode such constants.
1662 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001663 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001664 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001665 if (E->isTypeDependent() || E->isValueDependent() ||
1666 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001668 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001669 return;
1670 }
1671 priority = Idx.getZExtValue();
1672 }
Mike Stumpbf916502009-07-24 19:02:52 +00001673
Chandler Carruth87c44602011-07-01 23:49:12 +00001674 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001676 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001677 return;
1678 }
1679
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001680 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001681 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001682}
1683
Chandler Carruth1b03c872011-07-02 00:01:44 +00001684static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001685 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001686 if (Attr.getNumArgs() > 1) {
1687 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001688 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001689 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001690
1691 int priority = 65535; // FIXME: Do not hardcode such constants.
1692 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001693 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001694 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001695 if (E->isTypeDependent() || E->isValueDependent() ||
1696 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001697 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001698 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001699 return;
1700 }
1701 priority = Idx.getZExtValue();
1702 }
Mike Stumpbf916502009-07-24 19:02:52 +00001703
Chandler Carruth87c44602011-07-01 23:49:12 +00001704 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001705 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001706 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001707 return;
1708 }
1709
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001710 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001711 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001712}
1713
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001714template <typename AttrTy>
1715static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1716 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001717 unsigned NumArgs = Attr.getNumArgs();
1718 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001719 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001720 return;
1721 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001722
1723 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001724 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001725 if (NumArgs == 1) {
1726 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001727 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001728 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001729 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001730 return;
1731 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001732 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001733 }
Mike Stumpbf916502009-07-24 19:02:52 +00001734
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001735 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001736}
1737
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001738static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1739 const AttributeList &Attr) {
1740 unsigned NumArgs = Attr.getNumArgs();
1741 if (NumArgs > 0) {
1742 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1743 return;
1744 }
1745
1746 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001747 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001748}
1749
Patrick Beardb2f68202012-04-06 18:12:22 +00001750static void handleObjCRootClassAttr(Sema &S, Decl *D,
1751 const AttributeList &Attr) {
1752 if (!isa<ObjCInterfaceDecl>(D)) {
1753 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1754 return;
1755 }
1756
1757 unsigned NumArgs = Attr.getNumArgs();
1758 if (NumArgs > 0) {
1759 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1760 return;
1761 }
1762
1763 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1764}
1765
Ted Kremenek71207fc2012-01-05 22:47:47 +00001766static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001767 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001768 if (!isa<ObjCInterfaceDecl>(D)) {
1769 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1770 return;
1771 }
1772
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001773 unsigned NumArgs = Attr.getNumArgs();
1774 if (NumArgs > 0) {
1775 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1776 return;
1777 }
1778
Ted Kremenek71207fc2012-01-05 22:47:47 +00001779 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001780 Attr.getRange(), S.Context));
1781}
1782
Jordy Rosefad5de92012-05-08 03:27:22 +00001783static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1784 IdentifierInfo *Platform,
1785 VersionTuple Introduced,
1786 VersionTuple Deprecated,
1787 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001788 StringRef PlatformName
1789 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1790 if (PlatformName.empty())
1791 PlatformName = Platform->getName();
1792
1793 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1794 // of these steps are needed).
1795 if (!Introduced.empty() && !Deprecated.empty() &&
1796 !(Introduced <= Deprecated)) {
1797 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1798 << 1 << PlatformName << Deprecated.getAsString()
1799 << 0 << Introduced.getAsString();
1800 return true;
1801 }
1802
1803 if (!Introduced.empty() && !Obsoleted.empty() &&
1804 !(Introduced <= Obsoleted)) {
1805 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1806 << 2 << PlatformName << Obsoleted.getAsString()
1807 << 0 << Introduced.getAsString();
1808 return true;
1809 }
1810
1811 if (!Deprecated.empty() && !Obsoleted.empty() &&
1812 !(Deprecated <= Obsoleted)) {
1813 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1814 << 2 << PlatformName << Obsoleted.getAsString()
1815 << 1 << Deprecated.getAsString();
1816 return true;
1817 }
1818
1819 return false;
1820}
1821
Rafael Espindola599f1b72012-05-13 03:25:18 +00001822AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
1823 IdentifierInfo *Platform,
1824 VersionTuple Introduced,
1825 VersionTuple Deprecated,
1826 VersionTuple Obsoleted,
1827 bool IsUnavailable,
1828 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00001829 VersionTuple MergedIntroduced = Introduced;
1830 VersionTuple MergedDeprecated = Deprecated;
1831 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00001832 bool FoundAny = false;
1833
Rafael Espindola98ae8342012-05-10 02:50:16 +00001834 if (D->hasAttrs()) {
1835 AttrVec &Attrs = D->getAttrs();
1836 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1837 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1838 if (!OldAA) {
1839 ++i;
1840 continue;
1841 }
Rafael Espindola3b294362012-05-06 19:56:25 +00001842
Rafael Espindola98ae8342012-05-10 02:50:16 +00001843 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1844 if (OldPlatform != Platform) {
1845 ++i;
1846 continue;
1847 }
1848
1849 FoundAny = true;
1850 VersionTuple OldIntroduced = OldAA->getIntroduced();
1851 VersionTuple OldDeprecated = OldAA->getDeprecated();
1852 VersionTuple OldObsoleted = OldAA->getObsoleted();
1853 bool OldIsUnavailable = OldAA->getUnavailable();
1854 StringRef OldMessage = OldAA->getMessage();
1855
1856 if ((!OldIntroduced.empty() && !Introduced.empty() &&
1857 OldIntroduced != Introduced) ||
1858 (!OldDeprecated.empty() && !Deprecated.empty() &&
1859 OldDeprecated != Deprecated) ||
1860 (!OldObsoleted.empty() && !Obsoleted.empty() &&
1861 OldObsoleted != Obsoleted) ||
1862 (OldIsUnavailable != IsUnavailable) ||
1863 (OldMessage != Message)) {
1864 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1865 Diag(Range.getBegin(), diag::note_previous_attribute);
1866 Attrs.erase(Attrs.begin() + i);
1867 --e;
1868 continue;
1869 }
1870
1871 VersionTuple MergedIntroduced2 = MergedIntroduced;
1872 VersionTuple MergedDeprecated2 = MergedDeprecated;
1873 VersionTuple MergedObsoleted2 = MergedObsoleted;
1874
1875 if (MergedIntroduced2.empty())
1876 MergedIntroduced2 = OldIntroduced;
1877 if (MergedDeprecated2.empty())
1878 MergedDeprecated2 = OldDeprecated;
1879 if (MergedObsoleted2.empty())
1880 MergedObsoleted2 = OldObsoleted;
1881
1882 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1883 MergedIntroduced2, MergedDeprecated2,
1884 MergedObsoleted2)) {
1885 Attrs.erase(Attrs.begin() + i);
1886 --e;
1887 continue;
1888 }
1889
1890 MergedIntroduced = MergedIntroduced2;
1891 MergedDeprecated = MergedDeprecated2;
1892 MergedObsoleted = MergedObsoleted2;
1893 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00001894 }
Rafael Espindola3b294362012-05-06 19:56:25 +00001895 }
1896
1897 if (FoundAny &&
1898 MergedIntroduced == Introduced &&
1899 MergedDeprecated == Deprecated &&
1900 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00001901 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00001902
Rafael Espindola98ae8342012-05-10 02:50:16 +00001903 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00001904 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00001905 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1906 Introduced, Deprecated,
1907 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00001908 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00001909 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00001910}
1911
Chandler Carruth1b03c872011-07-02 00:01:44 +00001912static void handleAvailabilityAttr(Sema &S, Decl *D,
1913 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001914 IdentifierInfo *Platform = Attr.getParameterName();
1915 SourceLocation PlatformLoc = Attr.getParameterLoc();
1916
Rafael Espindola3b294362012-05-06 19:56:25 +00001917 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001918 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1919 << Platform;
1920
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001921 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1922 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1923 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001924 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001925 StringRef Str;
1926 const StringLiteral *SE =
1927 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1928 if (SE)
1929 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00001930
Rafael Espindola599f1b72012-05-13 03:25:18 +00001931 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
1932 Platform,
1933 Introduced.Version,
1934 Deprecated.Version,
1935 Obsoleted.Version,
1936 IsUnavailable, Str);
1937 if (NewAttr)
1938 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00001939}
1940
Rafael Espindola599f1b72012-05-13 03:25:18 +00001941VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
1942 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00001943 if (isa<TypedefNameDecl>(D)) {
1944 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00001945 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00001946 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00001947 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
1948 if (ExistingAttr) {
1949 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
1950 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00001951 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00001952 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
1953 Diag(Range.getBegin(), diag::note_previous_attribute);
1954 D->dropAttr<VisibilityAttr>();
1955 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00001956 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001957}
1958
Chandler Carruth1b03c872011-07-02 00:01:44 +00001959static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001960 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001961 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001962 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001963
Peter Collingbourne7a730022010-11-23 20:45:58 +00001964 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001965 Arg = Arg->IgnoreParenCasts();
1966 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001967
Douglas Gregor5cee1192011-07-27 05:40:30 +00001968 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001969 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001970 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001971 return;
1972 }
Mike Stumpbf916502009-07-24 19:02:52 +00001973
Chris Lattner5f9e2722011-07-23 10:55:15 +00001974 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001975 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001976
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001977 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001978 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001979 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001980 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001981 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001982 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00001983 else if (TypeStr == "protected") {
1984 // Complain about attempts to use protected visibility on targets
1985 // (like Darwin) that don't support it.
1986 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1987 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1988 type = VisibilityAttr::Default;
1989 } else {
1990 type = VisibilityAttr::Protected;
1991 }
1992 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00001993 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001994 return;
1995 }
Mike Stumpbf916502009-07-24 19:02:52 +00001996
Rafael Espindola599f1b72012-05-13 03:25:18 +00001997 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
1998 if (NewAttr)
1999 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002000}
2001
Chandler Carruth1b03c872011-07-02 00:01:44 +00002002static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2003 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002004 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2005 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002006 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002007 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002008 return;
2009 }
2010
Chandler Carruth87c44602011-07-01 23:49:12 +00002011 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2012 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2013 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002014 << "objc_method_family" << 1;
2015 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002016 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002017 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002018 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002019 return;
2020 }
2021
Chris Lattner5f9e2722011-07-23 10:55:15 +00002022 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002023 ObjCMethodFamilyAttr::FamilyKind family;
2024 if (param == "none")
2025 family = ObjCMethodFamilyAttr::OMF_None;
2026 else if (param == "alloc")
2027 family = ObjCMethodFamilyAttr::OMF_alloc;
2028 else if (param == "copy")
2029 family = ObjCMethodFamilyAttr::OMF_copy;
2030 else if (param == "init")
2031 family = ObjCMethodFamilyAttr::OMF_init;
2032 else if (param == "mutableCopy")
2033 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2034 else if (param == "new")
2035 family = ObjCMethodFamilyAttr::OMF_new;
2036 else {
2037 // Just warn and ignore it. This is future-proof against new
2038 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002039 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002040 return;
2041 }
2042
John McCallf85e1932011-06-15 23:02:42 +00002043 if (family == ObjCMethodFamilyAttr::OMF_init &&
2044 !method->getResultType()->isObjCObjectPointerType()) {
2045 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2046 << method->getResultType();
2047 // Ignore the attribute.
2048 return;
2049 }
2050
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002051 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002052 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002053}
2054
Chandler Carruth1b03c872011-07-02 00:01:44 +00002055static void handleObjCExceptionAttr(Sema &S, Decl *D,
2056 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002057 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002058 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002059
Chris Lattner0db29ec2009-02-14 08:09:34 +00002060 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2061 if (OCI == 0) {
2062 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2063 return;
2064 }
Mike Stumpbf916502009-07-24 19:02:52 +00002065
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002066 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002067}
2068
Chandler Carruth1b03c872011-07-02 00:01:44 +00002069static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002070 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002072 return;
2073 }
Richard Smith162e1c12011-04-15 14:24:37 +00002074 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002075 QualType T = TD->getUnderlyingType();
2076 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002077 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002078 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2079 return;
2080 }
2081 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002082 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2083 QualType T = PD->getType();
2084 if (!T->isPointerType() ||
2085 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
2086 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2087 return;
2088 }
2089 }
2090 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002091 // It is okay to include this attribute on properties, e.g.:
2092 //
2093 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2094 //
2095 // In this case it follows tradition and suppresses an error in the above
2096 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002097 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002098 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002099 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002100}
2101
Mike Stumpbf916502009-07-24 19:02:52 +00002102static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002103handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002104 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002106 return;
2107 }
2108
2109 if (!isa<FunctionDecl>(D)) {
2110 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2111 return;
2112 }
2113
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002114 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002115}
2116
Chandler Carruth1b03c872011-07-02 00:01:44 +00002117static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002118 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002119 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002120 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002121 return;
2122 }
Mike Stumpbf916502009-07-24 19:02:52 +00002123
Steve Naroff9eae5762008-09-18 16:44:58 +00002124 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002126 return;
2127 }
Mike Stumpbf916502009-07-24 19:02:52 +00002128
Sean Huntcf807c42010-08-18 23:23:40 +00002129 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002130 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002131 type = BlocksAttr::ByRef;
2132 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002133 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002134 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002135 return;
2136 }
Mike Stumpbf916502009-07-24 19:02:52 +00002137
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002138 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002139}
2140
Chandler Carruth1b03c872011-07-02 00:01:44 +00002141static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002142 // check the attribute arguments.
2143 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002144 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002145 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002146 }
2147
John McCall3323fad2011-09-09 07:56:05 +00002148 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002149 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002150 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002151 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002152 if (E->isTypeDependent() || E->isValueDependent() ||
2153 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002154 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002155 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002156 return;
2157 }
Mike Stumpbf916502009-07-24 19:02:52 +00002158
John McCall3323fad2011-09-09 07:56:05 +00002159 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002160 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2161 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002162 return;
2163 }
John McCall3323fad2011-09-09 07:56:05 +00002164
2165 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002166 }
2167
John McCall3323fad2011-09-09 07:56:05 +00002168 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002169 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002170 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002171 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002172 if (E->isTypeDependent() || E->isValueDependent() ||
2173 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002174 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002175 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002176 return;
2177 }
2178 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002179
John McCall3323fad2011-09-09 07:56:05 +00002180 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002181 // FIXME: This error message could be improved, it would be nice
2182 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002183 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2184 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002185 return;
2186 }
2187 }
2188
Chandler Carruth87c44602011-07-01 23:49:12 +00002189 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002190 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002191 if (isa<FunctionNoProtoType>(FT)) {
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2193 return;
2194 }
Mike Stumpbf916502009-07-24 19:02:52 +00002195
Chris Lattner897cd902009-03-17 23:03:47 +00002196 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002197 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002198 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002199 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002200 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002201 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002202 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002203 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002204 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002205 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2206 if (!BD->isVariadic()) {
2207 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2208 return;
2209 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002210 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002211 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002212 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002213 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002214 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002215 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002216 int m = Ty->isFunctionPointerType() ? 0 : 1;
2217 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002218 return;
2219 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002220 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002221 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002222 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002223 return;
2224 }
Anders Carlsson77091822008-10-05 18:05:59 +00002225 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002227 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002228 return;
2229 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002230 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002231 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002232}
2233
Chandler Carruth1b03c872011-07-02 00:01:44 +00002234static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002235 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002236 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002237 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002238
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002239 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002240 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002241 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002242 return;
2243 }
Mike Stumpbf916502009-07-24 19:02:52 +00002244
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002245 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2246 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2247 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002248 return;
2249 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002250 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2251 if (MD->getResultType()->isVoidType()) {
2252 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2253 << Attr.getName() << 1;
2254 return;
2255 }
2256
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002257 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002258}
2259
Chandler Carruth1b03c872011-07-02 00:01:44 +00002260static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002261 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002262 if (Attr.hasParameterOrArguments()) {
2263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002264 return;
2265 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002266
Chandler Carruth87c44602011-07-01 23:49:12 +00002267 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002268 if (isa<CXXRecordDecl>(D)) {
2269 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2270 return;
2271 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002272 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2273 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002274 return;
2275 }
2276
Chandler Carruth87c44602011-07-01 23:49:12 +00002277 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002278
2279 // 'weak' only applies to declarations with external linkage.
2280 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002281 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002282 return;
2283 }
Mike Stumpbf916502009-07-24 19:02:52 +00002284
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002285 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002286}
2287
Chandler Carruth1b03c872011-07-02 00:01:44 +00002288static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002289 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002290 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002291 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002292
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002293
2294 // weak_import only applies to variable & function declarations.
2295 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002296 if (!D->canBeWeakImported(isDef)) {
2297 if (isDef)
2298 S.Diag(Attr.getLoc(),
2299 diag::warn_attribute_weak_import_invalid_on_definition)
2300 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002301 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002302 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002303 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002304 // Nothing to warn about here.
2305 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002306 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002307 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002308
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002309 return;
2310 }
2311
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002312 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002313}
2314
Chandler Carruth1b03c872011-07-02 00:01:44 +00002315static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2316 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002317 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002318 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002319 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002320
2321 unsigned WGSize[3];
2322 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002323 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002324 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002325 if (E->isTypeDependent() || E->isValueDependent() ||
2326 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2328 << "reqd_work_group_size" << E->getSourceRange();
2329 return;
2330 }
2331 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2332 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002333 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002334 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002335 WGSize[2]));
2336}
2337
Rafael Espindola599f1b72012-05-13 03:25:18 +00002338SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2339 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002340 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2341 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002342 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002343 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2344 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002345 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002346 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002347 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002348}
2349
Chandler Carruth1b03c872011-07-02 00:01:44 +00002350static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002351 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002352 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002353 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002354
2355 // Make sure that there is a string literal as the sections's single
2356 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002357 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002358 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002359 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002360 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002361 return;
2362 }
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Chris Lattner797c3c42009-08-10 19:03:04 +00002364 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002365 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002366 if (!Error.empty()) {
2367 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2368 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002369 return;
2370 }
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002372 // This attribute cannot be applied to local variables.
2373 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2374 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2375 return;
2376 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002377 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2378 SE->getString());
2379 if (NewAttr)
2380 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002381}
2382
Chris Lattner6b6b5372008-06-26 18:38:35 +00002383
Chandler Carruth1b03c872011-07-02 00:01:44 +00002384static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002385 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002386 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002387 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002388 return;
2389 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002390
Chandler Carruth87c44602011-07-01 23:49:12 +00002391 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002392 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002393 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002394 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002395 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002396 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002397}
2398
Chandler Carruth1b03c872011-07-02 00:01:44 +00002399static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002400 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002401 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002402 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002403 return;
2404 }
Mike Stumpbf916502009-07-24 19:02:52 +00002405
Chandler Carruth87c44602011-07-01 23:49:12 +00002406 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002407 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002408 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002409 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002410 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002411 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002412}
2413
Chandler Carruth1b03c872011-07-02 00:01:44 +00002414static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002415 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002416 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002417 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002418
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002419 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002420}
2421
Chandler Carruth1b03c872011-07-02 00:01:44 +00002422static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002423 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2425 return;
2426 }
Mike Stumpbf916502009-07-24 19:02:52 +00002427
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002428 if (Attr.getNumArgs() != 0) {
2429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2430 return;
2431 }
Mike Stumpbf916502009-07-24 19:02:52 +00002432
Chandler Carruth87c44602011-07-01 23:49:12 +00002433 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002434
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002435 if (!VD || !VD->hasLocalStorage()) {
2436 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2437 return;
2438 }
Mike Stumpbf916502009-07-24 19:02:52 +00002439
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002440 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002441 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002442 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002443 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2444 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002445 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002446 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002447 Attr.getParameterName();
2448 return;
2449 }
Mike Stumpbf916502009-07-24 19:02:52 +00002450
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002451 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2452 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002453 S.Diag(Attr.getParameterLoc(),
2454 diag::err_attribute_cleanup_arg_not_function)
2455 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002456 return;
2457 }
2458
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002459 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002460 S.Diag(Attr.getParameterLoc(),
2461 diag::err_attribute_cleanup_func_must_take_one_arg)
2462 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002463 return;
2464 }
Mike Stumpbf916502009-07-24 19:02:52 +00002465
Anders Carlsson89941c12009-02-07 23:16:50 +00002466 // We're currently more strict than GCC about what function types we accept.
2467 // If this ever proves to be a problem it should be easy to fix.
2468 QualType Ty = S.Context.getPointerType(VD->getType());
2469 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002470 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2471 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002472 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002473 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2474 Attr.getParameterName() << ParamTy << Ty;
2475 return;
2476 }
Mike Stumpbf916502009-07-24 19:02:52 +00002477
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002478 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002479 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002480}
2481
Mike Stumpbf916502009-07-24 19:02:52 +00002482/// Handle __attribute__((format_arg((idx)))) attribute based on
2483/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002484static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002485 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002486 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002487
Chandler Carruth87c44602011-07-01 23:49:12 +00002488 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002489 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002490 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002491 return;
2492 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002493
2494 // In C++ the implicit 'this' function parameter also counts, and they are
2495 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002496 bool HasImplicitThisParam = isInstanceMethod(D);
2497 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002498 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002499
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002500 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002501 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002502 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002503 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2504 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2506 << "format" << 2 << IdxExpr->getSourceRange();
2507 return;
2508 }
Mike Stumpbf916502009-07-24 19:02:52 +00002509
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002510 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2512 << "format" << 2 << IdxExpr->getSourceRange();
2513 return;
2514 }
Mike Stumpbf916502009-07-24 19:02:52 +00002515
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002516 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002517
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002518 if (HasImplicitThisParam) {
2519 if (ArgIdx == 0) {
2520 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2521 << "format_arg" << IdxExpr->getSourceRange();
2522 return;
2523 }
2524 ArgIdx--;
2525 }
2526
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002527 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002528 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002529
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002530 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2531 if (not_nsstring_type &&
2532 !isCFStringType(Ty, S.Context) &&
2533 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002534 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002535 // FIXME: Should highlight the actual expression that has the wrong type.
2536 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002537 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002538 << IdxExpr->getSourceRange();
2539 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002540 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002541 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002542 if (!isNSStringType(Ty, S.Context) &&
2543 !isCFStringType(Ty, S.Context) &&
2544 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002545 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002546 // FIXME: Should highlight the actual expression that has the wrong type.
2547 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002548 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002549 << IdxExpr->getSourceRange();
2550 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002551 }
2552
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002553 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002554 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002555}
2556
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002557enum FormatAttrKind {
2558 CFStringFormat,
2559 NSStringFormat,
2560 StrftimeFormat,
2561 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002562 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002563 InvalidFormat
2564};
2565
2566/// getFormatAttrKind - Map from format attribute names to supported format
2567/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002568static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002569 return llvm::StringSwitch<FormatAttrKind>(Format)
2570 // Check for formats that get handled specially.
2571 .Case("NSString", NSStringFormat)
2572 .Case("CFString", CFStringFormat)
2573 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002574
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002575 // Otherwise, check for supported formats.
2576 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2577 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2578 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002579
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002580 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2581 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002582}
2583
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002584/// Handle __attribute__((init_priority(priority))) attributes based on
2585/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002586static void handleInitPriorityAttr(Sema &S, Decl *D,
2587 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002588 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002589 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2590 return;
2591 }
2592
Chandler Carruth87c44602011-07-01 23:49:12 +00002593 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002594 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2595 Attr.setInvalid();
2596 return;
2597 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002598 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002599 if (S.Context.getAsArrayType(T))
2600 T = S.Context.getBaseElementType(T);
2601 if (!T->getAs<RecordType>()) {
2602 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2603 Attr.setInvalid();
2604 return;
2605 }
2606
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002607 if (Attr.getNumArgs() != 1) {
2608 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2609 Attr.setInvalid();
2610 return;
2611 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002612 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002613
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002614 llvm::APSInt priority(32);
2615 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2616 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2617 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2618 << "init_priority" << priorityExpr->getSourceRange();
2619 Attr.setInvalid();
2620 return;
2621 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002622 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002623 if (prioritynum < 101 || prioritynum > 65535) {
2624 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2625 << priorityExpr->getSourceRange();
2626 Attr.setInvalid();
2627 return;
2628 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002629 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002630 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002631}
2632
Rafael Espindola599f1b72012-05-13 03:25:18 +00002633FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2634 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002635 // Check whether we already have an equivalent format attribute.
2636 for (specific_attr_iterator<FormatAttr>
2637 i = D->specific_attr_begin<FormatAttr>(),
2638 e = D->specific_attr_end<FormatAttr>();
2639 i != e ; ++i) {
2640 FormatAttr *f = *i;
2641 if (f->getType() == Format &&
2642 f->getFormatIdx() == FormatIdx &&
2643 f->getFirstArg() == FirstArg) {
2644 // If we don't have a valid location for this attribute, adopt the
2645 // location.
2646 if (f->getLocation().isInvalid())
2647 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002648 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002649 }
2650 }
2651
Rafael Espindola599f1b72012-05-13 03:25:18 +00002652 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2653 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002654}
2655
Mike Stumpbf916502009-07-24 19:02:52 +00002656/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2657/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002658static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002659
Chris Lattner545dd342008-06-28 23:36:30 +00002660 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002662 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002663 return;
2664 }
2665
Chris Lattner545dd342008-06-28 23:36:30 +00002666 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002668 return;
2669 }
2670
Chandler Carruth87c44602011-07-01 23:49:12 +00002671 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002673 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002674 return;
2675 }
2676
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002677 // In C++ the implicit 'this' function parameter also counts, and they are
2678 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002679 bool HasImplicitThisParam = isInstanceMethod(D);
2680 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002681 unsigned FirstIdx = 1;
2682
Chris Lattner5f9e2722011-07-23 10:55:15 +00002683 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002684
2685 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002686 if (Format.startswith("__") && Format.endswith("__"))
2687 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002688
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002689 // Check for supported formats.
2690 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002691
2692 if (Kind == IgnoredFormat)
2693 return;
2694
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002695 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002696 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002697 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002698 return;
2699 }
2700
2701 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002702 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002703 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002704 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2705 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002706 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002707 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002708 return;
2709 }
2710
2711 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002713 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002714 return;
2715 }
2716
2717 // FIXME: Do we need to bounds check?
2718 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002719
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002720 if (HasImplicitThisParam) {
2721 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002722 S.Diag(Attr.getLoc(),
2723 diag::err_format_attribute_implicit_this_format_string)
2724 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002725 return;
2726 }
2727 ArgIdx--;
2728 }
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Chris Lattner6b6b5372008-06-26 18:38:35 +00002730 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002731 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002732
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002733 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002734 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002735 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2736 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002737 return;
2738 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002739 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002740 // FIXME: do we need to check if the type is NSString*? What are the
2741 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002742 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002743 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002744 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2745 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002746 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002747 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002748 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002749 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002750 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002751 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2752 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002753 return;
2754 }
2755
2756 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002757 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002758 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002759 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2760 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002761 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002762 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002763 return;
2764 }
2765
2766 // check if the function is variadic if the 3rd argument non-zero
2767 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002768 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002769 ++NumArgs; // +1 for ...
2770 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002771 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002772 return;
2773 }
2774 }
2775
Chris Lattner3c73c412008-11-19 08:23:25 +00002776 // strftime requires FirstArg to be 0 because it doesn't read from any
2777 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002778 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002779 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002780 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2781 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002782 return;
2783 }
2784 // if 0 it disables parameter checking (to use with e.g. va_list)
2785 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002786 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002787 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002788 return;
2789 }
2790
Rafael Espindola599f1b72012-05-13 03:25:18 +00002791 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
2792 Idx.getZExtValue(),
2793 FirstArg.getZExtValue());
2794 if (NewAttr)
2795 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002796}
2797
Chandler Carruth1b03c872011-07-02 00:01:44 +00002798static void handleTransparentUnionAttr(Sema &S, Decl *D,
2799 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002800 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002801 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002802 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002803
Chris Lattner6b6b5372008-06-26 18:38:35 +00002804
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002805 // Try to find the underlying union declaration.
2806 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002807 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002808 if (TD && TD->getUnderlyingType()->isUnionType())
2809 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2810 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002811 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002812
2813 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002815 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002816 return;
2817 }
2818
John McCall5e1cdac2011-10-07 06:10:15 +00002819 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002820 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002821 diag::warn_transparent_union_attribute_not_definition);
2822 return;
2823 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002824
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002825 RecordDecl::field_iterator Field = RD->field_begin(),
2826 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002827 if (Field == FieldEnd) {
2828 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2829 return;
2830 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002831
David Blaikie581deb32012-06-06 20:45:41 +00002832 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002833 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002834 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002835 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002836 diag::warn_transparent_union_attribute_floating)
2837 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002838 return;
2839 }
2840
2841 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2842 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2843 for (; Field != FieldEnd; ++Field) {
2844 QualType FieldType = Field->getType();
2845 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2846 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2847 // Warn if we drop the attribute.
2848 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002849 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002850 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002851 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002852 diag::warn_transparent_union_attribute_field_size_align)
2853 << isSize << Field->getDeclName() << FieldBits;
2854 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002855 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002856 diag::note_transparent_union_first_field_size_align)
2857 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002858 return;
2859 }
2860 }
2861
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002862 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002863}
2864
Chandler Carruth1b03c872011-07-02 00:01:44 +00002865static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002866 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002867 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002868 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002869
Peter Collingbourne7a730022010-11-23 20:45:58 +00002870 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002871 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002872
Chris Lattner6b6b5372008-06-26 18:38:35 +00002873 // Make sure that there is a string literal as the annotation's single
2874 // argument.
2875 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002876 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002877 return;
2878 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002879
2880 // Don't duplicate annotations that are already set.
2881 for (specific_attr_iterator<AnnotateAttr>
2882 i = D->specific_attr_begin<AnnotateAttr>(),
2883 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2884 if ((*i)->getAnnotation() == SE->getString())
2885 return;
2886 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002887 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002888 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002889}
2890
Chandler Carruth1b03c872011-07-02 00:01:44 +00002891static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002892 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002893 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002895 return;
2896 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002897
2898 //FIXME: The C++0x version of this attribute has more limited applicabilty
2899 // than GNU's, and should error out when it is used to specify a
2900 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002901
Chris Lattner545dd342008-06-28 23:36:30 +00002902 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002903 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002904 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002905 }
Mike Stumpbf916502009-07-24 19:02:52 +00002906
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002907 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002908}
2909
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002910void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002911 // FIXME: Handle pack-expansions here.
2912 if (DiagnoseUnexpandedParameterPack(E))
2913 return;
2914
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002915 if (E->isTypeDependent() || E->isValueDependent()) {
2916 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002917 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002918 return;
2919 }
2920
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002921 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002922 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002923 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00002924 ExprResult ICE
2925 = VerifyIntegerConstantExpression(E, &Alignment,
2926 diag::err_aligned_attribute_argument_not_int,
2927 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00002928 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002929 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002930 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002931 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2932 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002933 return;
2934 }
2935
Richard Smith282e7e62012-02-04 09:53:13 +00002936 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Sean Huntcf807c42010-08-18 23:23:40 +00002937}
2938
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002939void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002940 // FIXME: Cache the number on the Attr object if non-dependent?
2941 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002942 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002943 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002944}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002945
Chandler Carruthd309c812011-07-01 23:49:16 +00002946/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002947/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002948///
Mike Stumpbf916502009-07-24 19:02:52 +00002949/// Despite what would be logical, the mode attribute is a decl attribute, not a
2950/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2951/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002952static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002953 // This attribute isn't documented, but glibc uses it. It changes
2954 // the width of an int or unsigned int to the specified size.
2955
2956 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002957 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002958 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002959
Chris Lattnerfbf13472008-06-27 22:18:37 +00002960
2961 IdentifierInfo *Name = Attr.getParameterName();
2962 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002963 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002964 return;
2965 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002966
Chris Lattner5f9e2722011-07-23 10:55:15 +00002967 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002968
2969 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002970 if (Str.startswith("__") && Str.endswith("__"))
2971 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002972
2973 unsigned DestWidth = 0;
2974 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002975 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002976 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002977 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002978 switch (Str[0]) {
2979 case 'Q': DestWidth = 8; break;
2980 case 'H': DestWidth = 16; break;
2981 case 'S': DestWidth = 32; break;
2982 case 'D': DestWidth = 64; break;
2983 case 'X': DestWidth = 96; break;
2984 case 'T': DestWidth = 128; break;
2985 }
2986 if (Str[1] == 'F') {
2987 IntegerMode = false;
2988 } else if (Str[1] == 'C') {
2989 IntegerMode = false;
2990 ComplexMode = true;
2991 } else if (Str[1] != 'I') {
2992 DestWidth = 0;
2993 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002994 break;
2995 case 4:
2996 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2997 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002998 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002999 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003000 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003001 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003002 break;
3003 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003004 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003005 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003006 break;
3007 }
3008
3009 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003010 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003011 OldTy = TD->getUnderlyingType();
3012 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3013 OldTy = VD->getType();
3014 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003015 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003016 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003017 return;
3018 }
Eli Friedman73397492009-03-03 06:41:03 +00003019
John McCall183700f2009-09-21 23:43:11 +00003020 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003021 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3022 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003023 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003024 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3025 } else if (ComplexMode) {
3026 if (!OldTy->isComplexType())
3027 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3028 } else {
3029 if (!OldTy->isFloatingType())
3030 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3031 }
3032
Mike Stump390b4cc2009-05-16 07:39:55 +00003033 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3034 // and friends, at least with glibc.
3035 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3036 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003037 // FIXME: Make sure floating-point mappings are accurate
3038 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003039 QualType NewTy;
3040 switch (DestWidth) {
3041 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003042 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003043 return;
3044 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003045 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003046 return;
3047 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003048 if (!IntegerMode) {
3049 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3050 return;
3051 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003052 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003053 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003054 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003055 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003056 break;
3057 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003058 if (!IntegerMode) {
3059 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3060 return;
3061 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003062 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003063 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003064 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003065 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003066 break;
3067 case 32:
3068 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003069 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003070 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003071 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003072 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003073 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003074 break;
3075 case 64:
3076 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003077 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003078 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003079 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003080 NewTy = S.Context.LongTy;
3081 else
3082 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003083 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003084 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003085 NewTy = S.Context.UnsignedLongTy;
3086 else
3087 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003088 break;
Eli Friedman73397492009-03-03 06:41:03 +00003089 case 96:
3090 NewTy = S.Context.LongDoubleTy;
3091 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003092 case 128:
3093 if (!IntegerMode) {
3094 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3095 return;
3096 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003097 if (OldTy->isSignedIntegerType())
3098 NewTy = S.Context.Int128Ty;
3099 else
3100 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003101 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003102 }
3103
Eli Friedman73397492009-03-03 06:41:03 +00003104 if (ComplexMode) {
3105 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003106 }
3107
3108 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003109 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003110 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003111 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003112 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003113 cast<ValueDecl>(D)->setType(NewTy);
3114}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003115
Chandler Carruth1b03c872011-07-02 00:01:44 +00003116static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003117 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003118 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003119 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003120
Chandler Carruth87c44602011-07-01 23:49:12 +00003121 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00003122 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003123 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00003124 return;
3125 }
Mike Stumpbf916502009-07-24 19:02:52 +00003126
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003127 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003128}
3129
Chandler Carruth1b03c872011-07-02 00:01:44 +00003130static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003131 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003132 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003133 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003134
Mike Stumpbf916502009-07-24 19:02:52 +00003135
Chandler Carruth87c44602011-07-01 23:49:12 +00003136 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003137 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003138 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003139 return;
3140 }
Mike Stumpbf916502009-07-24 19:02:52 +00003141
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003142 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003143}
3144
Chandler Carruth1b03c872011-07-02 00:01:44 +00003145static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3146 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003147 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003148 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003149 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003150
Chris Lattner7255a2d2010-06-22 00:03:40 +00003151
Chandler Carruth87c44602011-07-01 23:49:12 +00003152 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003153 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003154 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003155 return;
3156 }
3157
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003158 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003159 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003160}
3161
Chandler Carruth1b03c872011-07-02 00:01:44 +00003162static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003163 if (S.LangOpts.CUDA) {
3164 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003165 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003166 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3167 return;
3168 }
3169
Chandler Carruth87c44602011-07-01 23:49:12 +00003170 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003171 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003172 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003173 return;
3174 }
3175
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003176 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003177 } else {
3178 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3179 }
3180}
3181
Chandler Carruth1b03c872011-07-02 00:01:44 +00003182static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003183 if (S.LangOpts.CUDA) {
3184 // check the attribute arguments.
3185 if (Attr.getNumArgs() != 0) {
3186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3187 return;
3188 }
3189
Chandler Carruth87c44602011-07-01 23:49:12 +00003190 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003192 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003193 return;
3194 }
3195
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003196 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003197 } else {
3198 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3199 }
3200}
3201
Chandler Carruth1b03c872011-07-02 00:01:44 +00003202static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003203 if (S.LangOpts.CUDA) {
3204 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003205 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003206 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003207
Chandler Carruth87c44602011-07-01 23:49:12 +00003208 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003210 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003211 return;
3212 }
3213
Chandler Carruth87c44602011-07-01 23:49:12 +00003214 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003215 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003216 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003217 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3218 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3219 << FD->getType()
3220 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3221 "void");
3222 } else {
3223 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3224 << FD->getType();
3225 }
3226 return;
3227 }
3228
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003229 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003230 } else {
3231 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3232 }
3233}
3234
Chandler Carruth1b03c872011-07-02 00:01:44 +00003235static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003236 if (S.LangOpts.CUDA) {
3237 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003238 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003239 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003240
Peter Collingbourneced76712010-12-01 03:15:31 +00003241
Chandler Carruth87c44602011-07-01 23:49:12 +00003242 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003244 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003245 return;
3246 }
3247
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003248 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003249 } else {
3250 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3251 }
3252}
3253
Chandler Carruth1b03c872011-07-02 00:01:44 +00003254static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003255 if (S.LangOpts.CUDA) {
3256 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003257 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003258 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003259
Peter Collingbourneced76712010-12-01 03:15:31 +00003260
Chandler Carruth87c44602011-07-01 23:49:12 +00003261 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003262 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003263 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003264 return;
3265 }
3266
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003267 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003268 } else {
3269 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3270 }
3271}
3272
Chandler Carruth1b03c872011-07-02 00:01:44 +00003273static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003274 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003275 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003276 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003277
Chandler Carruth87c44602011-07-01 23:49:12 +00003278 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003279 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003280 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003281 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003282 return;
3283 }
Mike Stumpbf916502009-07-24 19:02:52 +00003284
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003285 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003286 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003287 return;
3288 }
Mike Stumpbf916502009-07-24 19:02:52 +00003289
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003290 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003291}
3292
Chandler Carruth1b03c872011-07-02 00:01:44 +00003293static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003294 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003295
Chandler Carruth87c44602011-07-01 23:49:12 +00003296 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003297 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3298 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003299 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003300 return;
3301
Chandler Carruth87c44602011-07-01 23:49:12 +00003302 if (!isa<ObjCMethodDecl>(D)) {
3303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3304 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003305 return;
3306 }
3307
Chandler Carruth87c44602011-07-01 23:49:12 +00003308 switch (Attr.getKind()) {
Sean Huntbfcb0372012-06-19 03:39:03 +00003309 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003310 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003311 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003312 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003313 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003314 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003315 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003316 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003317 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003318 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003319 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003320 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003321 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003322 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003323 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003324 case AttributeList::AT_Pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003325 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003326 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003327 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003328 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003329 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003330 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003331 return;
3332 }
3333
Chris Lattner5f9e2722011-07-23 10:55:15 +00003334 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003335 PcsAttr::PCSType PCS;
3336 if (StrRef == "aapcs")
3337 PCS = PcsAttr::AAPCS;
3338 else if (StrRef == "aapcs-vfp")
3339 PCS = PcsAttr::AAPCS_VFP;
3340 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003341 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3342 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003343 return;
3344 }
3345
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003346 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003347 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003348 default:
3349 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003350 }
3351}
3352
Chandler Carruth1b03c872011-07-02 00:01:44 +00003353static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003354 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003355 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003356}
3357
John McCall711c52b2011-01-05 12:14:39 +00003358bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3359 if (attr.isInvalid())
3360 return true;
3361
Ted Kremenek831efae2011-04-15 05:49:29 +00003362 if ((attr.getNumArgs() != 0 &&
Sean Huntbfcb0372012-06-19 03:39:03 +00003363 !(attr.getKind() == AttributeList::AT_Pcs && attr.getNumArgs() == 1)) ||
Ted Kremenek831efae2011-04-15 05:49:29 +00003364 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003365 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3366 attr.setInvalid();
3367 return true;
3368 }
3369
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003370 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3371 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003372 switch (attr.getKind()) {
Sean Huntbfcb0372012-06-19 03:39:03 +00003373 case AttributeList::AT_CDecl: CC = CC_C; break;
3374 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3375 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3376 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3377 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3378 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003379 Expr *Arg = attr.getArg(0);
3380 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003381 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003382 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3383 << "pcs" << 1;
3384 attr.setInvalid();
3385 return true;
3386 }
3387
Chris Lattner5f9e2722011-07-23 10:55:15 +00003388 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003389 if (StrRef == "aapcs") {
3390 CC = CC_AAPCS;
3391 break;
3392 } else if (StrRef == "aapcs-vfp") {
3393 CC = CC_AAPCS_VFP;
3394 break;
3395 }
3396 // FALLS THROUGH
3397 }
David Blaikie7530c032012-01-17 06:56:22 +00003398 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003399 }
3400
3401 return false;
3402}
3403
Chandler Carruth1b03c872011-07-02 00:01:44 +00003404static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003405 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003406
3407 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003408 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003409 return;
3410
Chandler Carruth87c44602011-07-01 23:49:12 +00003411 if (!isa<ObjCMethodDecl>(D)) {
3412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3413 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003414 return;
3415 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003416
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003417 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003418}
3419
3420/// Checks a regparm attribute, returning true if it is ill-formed and
3421/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003422bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3423 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003424 return true;
3425
Chandler Carruth87c44602011-07-01 23:49:12 +00003426 if (Attr.getNumArgs() != 1) {
3427 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3428 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003429 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003430 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003431
Chandler Carruth87c44602011-07-01 23:49:12 +00003432 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003433 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003434 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003435 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003436 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003437 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003438 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003439 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003440 }
3441
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003442 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003443 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003444 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003445 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003446 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003447 }
3448
John McCall711c52b2011-01-05 12:14:39 +00003449 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003450 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003451 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003452 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003453 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003454 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003455 }
3456
John McCall711c52b2011-01-05 12:14:39 +00003457 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003458}
3459
Chandler Carruth1b03c872011-07-02 00:01:44 +00003460static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003461 if (S.LangOpts.CUDA) {
3462 // check the attribute arguments.
3463 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003464 // FIXME: 0 is not okay.
3465 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003466 return;
3467 }
3468
Chandler Carruth87c44602011-07-01 23:49:12 +00003469 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003471 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003472 return;
3473 }
3474
3475 Expr *MaxThreadsExpr = Attr.getArg(0);
3476 llvm::APSInt MaxThreads(32);
3477 if (MaxThreadsExpr->isTypeDependent() ||
3478 MaxThreadsExpr->isValueDependent() ||
3479 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3481 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3482 return;
3483 }
3484
3485 llvm::APSInt MinBlocks(32);
3486 if (Attr.getNumArgs() > 1) {
3487 Expr *MinBlocksExpr = Attr.getArg(1);
3488 if (MinBlocksExpr->isTypeDependent() ||
3489 MinBlocksExpr->isValueDependent() ||
3490 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3492 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3493 return;
3494 }
3495 }
3496
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003497 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003498 MaxThreads.getZExtValue(),
3499 MinBlocks.getZExtValue()));
3500 } else {
3501 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3502 }
3503}
3504
Chris Lattner0744e5f2008-06-29 00:23:49 +00003505//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003506// Checker-specific attribute handlers.
3507//===----------------------------------------------------------------------===//
3508
John McCallc7ad3812011-01-25 03:31:58 +00003509static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003510 return type->isDependentType() ||
3511 type->isObjCObjectPointerType() ||
3512 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003513}
3514static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003515 return type->isDependentType() ||
3516 type->isPointerType() ||
3517 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003518}
3519
Chandler Carruth1b03c872011-07-02 00:01:44 +00003520static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003521 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003522 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003523 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003524 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003525 return;
3526 }
3527
3528 bool typeOK, cf;
Sean Huntbfcb0372012-06-19 03:39:03 +00003529 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003530 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3531 cf = false;
3532 } else {
3533 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3534 cf = true;
3535 }
3536
3537 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003538 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003539 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003540 return;
3541 }
3542
3543 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003544 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003545 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003546 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003547}
3548
Chandler Carruth1b03c872011-07-02 00:01:44 +00003549static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3550 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003551 if (!isa<ObjCMethodDecl>(D)) {
3552 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003553 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003554 return;
3555 }
3556
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003557 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003558}
3559
Chandler Carruth1b03c872011-07-02 00:01:44 +00003560static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3561 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003562
John McCallc7ad3812011-01-25 03:31:58 +00003563 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003564
Chandler Carruth87c44602011-07-01 23:49:12 +00003565 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003566 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003567 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003568 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003569 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Huntbfcb0372012-06-19 03:39:03 +00003570 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003571 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003572 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003573 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003574 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003575 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003576 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003577 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003578 return;
3579 }
Mike Stumpbf916502009-07-24 19:02:52 +00003580
John McCallc7ad3812011-01-25 03:31:58 +00003581 bool typeOK;
3582 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003583 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003584 default: llvm_unreachable("invalid ownership attribute");
Sean Huntbfcb0372012-06-19 03:39:03 +00003585 case AttributeList::AT_NSReturnsAutoreleased:
3586 case AttributeList::AT_NSReturnsRetained:
3587 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003588 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3589 cf = false;
3590 break;
3591
Sean Huntbfcb0372012-06-19 03:39:03 +00003592 case AttributeList::AT_CFReturnsRetained:
3593 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003594 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3595 cf = true;
3596 break;
3597 }
3598
3599 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003600 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003601 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003602 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003603 }
Mike Stumpbf916502009-07-24 19:02:52 +00003604
Chandler Carruth87c44602011-07-01 23:49:12 +00003605 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003606 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003607 llvm_unreachable("invalid ownership attribute");
Sean Huntbfcb0372012-06-19 03:39:03 +00003608 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003609 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003610 S.Context));
3611 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003612 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003613 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003614 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003615 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003616 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003617 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003618 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003619 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003620 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003621 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003622 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003623 return;
Sean Huntbfcb0372012-06-19 03:39:03 +00003624 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003625 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003626 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003627 return;
3628 };
3629}
3630
John McCalldc7c5ad2011-07-22 08:53:00 +00003631static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3632 const AttributeList &attr) {
3633 SourceLocation loc = attr.getLoc();
3634
3635 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3636
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003637 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003638 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003639 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003640 return;
3641 }
3642
3643 // Check that the method returns a normal pointer.
3644 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003645
3646 if (!resultType->isReferenceType() &&
3647 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003648 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3649 << SourceRange(loc)
3650 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3651
3652 // Drop the attribute.
3653 return;
3654 }
3655
3656 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003657 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003658}
3659
John McCall8dfac0b2011-09-30 05:12:12 +00003660/// Handle cf_audited_transfer and cf_unknown_transfer.
3661static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3662 if (!isa<FunctionDecl>(D)) {
3663 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003664 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003665 return;
3666 }
3667
Sean Huntbfcb0372012-06-19 03:39:03 +00003668 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00003669
3670 // Check whether there's a conflicting attribute already present.
3671 Attr *Existing;
3672 if (IsAudited) {
3673 Existing = D->getAttr<CFUnknownTransferAttr>();
3674 } else {
3675 Existing = D->getAttr<CFAuditedTransferAttr>();
3676 }
3677 if (Existing) {
3678 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3679 << A.getName()
3680 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3681 << A.getRange() << Existing->getRange();
3682 return;
3683 }
3684
3685 // All clear; add the attribute.
3686 if (IsAudited) {
3687 D->addAttr(
3688 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3689 } else {
3690 D->addAttr(
3691 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3692 }
3693}
3694
John McCallfe98da02011-09-29 07:17:38 +00003695static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3696 const AttributeList &Attr) {
3697 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3698 if (!RD || RD->isUnion()) {
3699 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003700 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003701 }
3702
3703 IdentifierInfo *ParmName = Attr.getParameterName();
3704
3705 // In Objective-C, verify that the type names an Objective-C type.
3706 // We don't want to check this outside of ObjC because people sometimes
3707 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003708 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003709 // Check for an existing type with this name.
3710 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3711 Sema::LookupOrdinaryName);
3712 if (S.LookupName(R, Sc)) {
3713 NamedDecl *Target = R.getFoundDecl();
3714 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3715 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3716 S.Diag(Target->getLocStart(), diag::note_declared_at);
3717 }
3718 }
3719 }
3720
3721 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3722 ParmName));
3723}
3724
Chandler Carruth1b03c872011-07-02 00:01:44 +00003725static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3726 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003727 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003728
Chandler Carruth87c44602011-07-01 23:49:12 +00003729 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003730 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003731}
3732
Chandler Carruth1b03c872011-07-02 00:01:44 +00003733static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3734 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003735 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003736 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003737 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003738 return;
3739 }
3740
Chandler Carruth87c44602011-07-01 23:49:12 +00003741 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003742 QualType type = vd->getType();
3743
3744 if (!type->isDependentType() &&
3745 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003746 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003747 << type;
3748 return;
3749 }
3750
3751 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3752
3753 // If we have no lifetime yet, check the lifetime we're presumably
3754 // going to infer.
3755 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3756 lifetime = type->getObjCARCImplicitLifetime();
3757
3758 switch (lifetime) {
3759 case Qualifiers::OCL_None:
3760 assert(type->isDependentType() &&
3761 "didn't infer lifetime for non-dependent type?");
3762 break;
3763
3764 case Qualifiers::OCL_Weak: // meaningful
3765 case Qualifiers::OCL_Strong: // meaningful
3766 break;
3767
3768 case Qualifiers::OCL_ExplicitNone:
3769 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003770 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003771 << (lifetime == Qualifiers::OCL_Autoreleasing);
3772 break;
3773 }
3774
Chandler Carruth87c44602011-07-01 23:49:12 +00003775 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003776 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003777}
3778
Charles Davisf0122fe2010-02-16 18:27:26 +00003779static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman94287722012-02-23 22:46:33 +00003780 switch (Attr.getKind()) {
3781 default:
3782 return false;
Sean Huntbfcb0372012-06-19 03:39:03 +00003783 case AttributeList::AT_DLLImport:
3784 case AttributeList::AT_DLLExport:
3785 case AttributeList::AT_Uuid:
3786 case AttributeList::AT_Deprecated:
3787 case AttributeList::AT_NoReturn:
3788 case AttributeList::AT_NoThrow:
3789 case AttributeList::AT_Naked:
3790 case AttributeList::AT_NoInline:
Aaron Ballman94287722012-02-23 22:46:33 +00003791 return true;
3792 }
Francois Pichet11542142010-12-19 06:50:37 +00003793}
3794
3795//===----------------------------------------------------------------------===//
3796// Microsoft specific attribute handlers.
3797//===----------------------------------------------------------------------===//
3798
Chandler Carruth1b03c872011-07-02 00:01:44 +00003799static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003800 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003801 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003802 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003803 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003804
Francois Pichet11542142010-12-19 06:50:37 +00003805 Expr *Arg = Attr.getArg(0);
3806 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003807 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003808 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3809 << "uuid" << 1;
3810 return;
3811 }
3812
Chris Lattner5f9e2722011-07-23 10:55:15 +00003813 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003814
3815 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3816 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003817
Francois Pichetd3d3be92010-12-20 01:41:49 +00003818 // Validate GUID length.
3819 if (IsCurly && StrRef.size() != 38) {
3820 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3821 return;
3822 }
3823 if (!IsCurly && StrRef.size() != 36) {
3824 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3825 return;
3826 }
3827
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003828 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003829 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003830 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003831 if (IsCurly) // Skip the optional '{'
3832 ++I;
3833
3834 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003835 if (i == 8 || i == 13 || i == 18 || i == 23) {
3836 if (*I != '-') {
3837 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3838 return;
3839 }
3840 } else if (!isxdigit(*I)) {
3841 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3842 return;
3843 }
3844 I++;
3845 }
Francois Pichet11542142010-12-19 06:50:37 +00003846
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003847 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003848 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003849 } else
Francois Pichet11542142010-12-19 06:50:37 +00003850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003851}
3852
John McCallc052dbb2012-05-22 21:28:12 +00003853static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3854 if (S.LangOpts.MicrosoftExt) {
3855 AttributeList::Kind Kind = Attr.getKind();
Sean Huntbfcb0372012-06-19 03:39:03 +00003856 if (Kind == AttributeList::AT_SingleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00003857 D->addAttr(
3858 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
Sean Huntbfcb0372012-06-19 03:39:03 +00003859 else if (Kind == AttributeList::AT_MultipleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00003860 D->addAttr(
3861 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
Sean Huntbfcb0372012-06-19 03:39:03 +00003862 else if (Kind == AttributeList::AT_VirtualInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00003863 D->addAttr(
3864 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
3865 } else
3866 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3867}
3868
3869static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3870 if (S.LangOpts.MicrosoftExt) {
3871 AttributeList::Kind Kind = Attr.getKind();
Sean Huntbfcb0372012-06-19 03:39:03 +00003872 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00003873 D->addAttr(
3874 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Huntbfcb0372012-06-19 03:39:03 +00003875 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00003876 D->addAttr(
3877 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Huntbfcb0372012-06-19 03:39:03 +00003878 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00003879 D->addAttr(
3880 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
3881 } else
3882 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3883}
3884
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00003885static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3886 if (S.LangOpts.MicrosoftExt)
3887 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
3888 else
3889 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3890}
3891
Ted Kremenekb71368d2009-05-09 02:44:38 +00003892//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003893// Top Level Sema Entry Points
3894//===----------------------------------------------------------------------===//
3895
Chandler Carruth1b03c872011-07-02 00:01:44 +00003896static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3897 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003898 switch (Attr.getKind()) {
Sean Huntbfcb0372012-06-19 03:39:03 +00003899 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
3900 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
3901 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003902 default:
3903 break;
3904 }
3905}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003906
Chandler Carruth1b03c872011-07-02 00:01:44 +00003907static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3908 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003909 switch (Attr.getKind()) {
Sean Huntbfcb0372012-06-19 03:39:03 +00003910 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
3911 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3912 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003913 handleIBOutletCollection(S, D, Attr); break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003914 case AttributeList::AT_AddressSpace:
3915 case AttributeList::AT_OpenCLImageAccess:
3916 case AttributeList::AT_ObjCGC:
3917 case AttributeList::AT_VectorSize:
3918 case AttributeList::AT_NeonVectorType:
3919 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00003920 // Ignore these, these are type attributes, handled by
3921 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003922 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003923 case AttributeList::AT_CUDADevice:
3924 case AttributeList::AT_CUDAHost:
3925 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00003926 // Ignore, this is a non-inheritable attribute, handled
3927 // by ProcessNonInheritableDeclAttr.
3928 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003929 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3930 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3931 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3932 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003933 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003934 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003935 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003936 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3937 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3938 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003939 handleDependencyAttr (S, D, Attr); break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003940 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
3941 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
3942 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
3943 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00003944 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
3945 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003946 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3947 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003948 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003949 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003950 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3951 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3952 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
3953 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3954 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003955 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003956 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003957 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
3958 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
3959 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
3960 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
3961 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003962 case AttributeList::AT_ownership_returns:
3963 case AttributeList::AT_ownership_takes:
3964 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003965 handleOwnershipAttr (S, D, Attr); break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003966 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3967 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
3968 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
3969 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
3970 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
3971 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
3972 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003973
Sean Huntbfcb0372012-06-19 03:39:03 +00003974 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003975 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Huntbfcb0372012-06-19 03:39:03 +00003976 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003977 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003978
Sean Huntbfcb0372012-06-19 03:39:03 +00003979 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00003980 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3981
Sean Huntbfcb0372012-06-19 03:39:03 +00003982 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00003983 handleNSBridgedAttr(S, scope, D, Attr); break;
3984
Sean Huntbfcb0372012-06-19 03:39:03 +00003985 case AttributeList::AT_CFAuditedTransfer:
3986 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00003987 handleCFTransferAttr(S, D, Attr); break;
3988
Ted Kremenekb71368d2009-05-09 02:44:38 +00003989 // Checker-specific.
Sean Huntbfcb0372012-06-19 03:39:03 +00003990 case AttributeList::AT_CFConsumed:
3991 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3992 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003993 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003994
Sean Huntbfcb0372012-06-19 03:39:03 +00003995 case AttributeList::AT_NSReturnsAutoreleased:
3996 case AttributeList::AT_NSReturnsNotRetained:
3997 case AttributeList::AT_CFReturnsNotRetained:
3998 case AttributeList::AT_NSReturnsRetained:
3999 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004000 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004001
Sean Huntbfcb0372012-06-19 03:39:03 +00004002 case AttributeList::AT_ReqdWorkGroupSize:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004003 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004004
Sean Huntbfcb0372012-06-19 03:39:03 +00004005 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004006 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004007
Sean Huntbfcb0372012-06-19 03:39:03 +00004008 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4009 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4010 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004011 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4012 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004013 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004014 handleArcWeakrefUnavailableAttr (S, D, Attr);
4015 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004016 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004017 handleObjCRootClassAttr(S, D, Attr);
4018 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004019 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004020 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004021 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004022 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4023 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004024 handleReturnsTwiceAttr(S, D, Attr);
4025 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004026 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4027 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4028 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004029 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004030 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4031 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4032 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4033 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004034 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004035 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004036 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004037 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004038 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004039 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004040 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004041 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004042 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4043 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4044 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4045 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4046 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4047 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4048 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4049 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4050 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004051 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004052 // Just ignore
4053 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004054 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004055 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004056 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004057 case AttributeList::AT_StdCall:
4058 case AttributeList::AT_CDecl:
4059 case AttributeList::AT_FastCall:
4060 case AttributeList::AT_ThisCall:
4061 case AttributeList::AT_Pascal:
4062 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004063 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004064 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004065 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004066 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004067 break;
John McCallc052dbb2012-05-22 21:28:12 +00004068
4069 // Microsoft attributes:
Sean Huntbfcb0372012-06-19 03:39:03 +00004070 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004071 handleMsStructAttr(S, D, Attr);
4072 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004073 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004074 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004075 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004076 case AttributeList::AT_SingleInheritance:
4077 case AttributeList::AT_MultipleInheritance:
4078 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004079 handleInheritanceAttr(S, D, Attr);
4080 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004081 case AttributeList::AT_Win64:
4082 case AttributeList::AT_Ptr32:
4083 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004084 handlePortabilityAttr(S, D, Attr);
4085 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004086 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004087 handleForceInlineAttr(S, D, Attr);
4088 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004089
4090 // Thread safety attributes:
Sean Huntbfcb0372012-06-19 03:39:03 +00004091 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004092 handleGuardedVarAttr(S, D, Attr);
4093 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004094 case AttributeList::AT_PtGuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004095 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
4096 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004097 case AttributeList::AT_ScopedLockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004098 handleLockableAttr(S, D, Attr, /*scoped = */true);
4099 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004100 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004101 handleNoAddressSafetyAttr(S, D, Attr);
4102 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004103 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004104 handleNoThreadSafetyAttr(S, D, Attr);
4105 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004106 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004107 handleLockableAttr(S, D, Attr);
4108 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004109 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004110 handleGuardedByAttr(S, D, Attr);
4111 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004112 case AttributeList::AT_PtGuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004113 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
4114 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004115 case AttributeList::AT_ExclusiveLockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004116 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
4117 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004118 case AttributeList::AT_ExclusiveLocksRequired:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004119 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
4120 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004121 case AttributeList::AT_ExclusiveTrylockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004122 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
4123 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004124 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004125 handleLockReturnedAttr(S, D, Attr);
4126 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004127 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004128 handleLocksExcludedAttr(S, D, Attr);
4129 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004130 case AttributeList::AT_SharedLockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004131 handleLockFunAttr(S, D, Attr);
4132 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004133 case AttributeList::AT_SharedLocksRequired:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004134 handleLocksRequiredAttr(S, D, Attr);
4135 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004136 case AttributeList::AT_SharedTrylockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004137 handleTrylockFunAttr(S, D, Attr);
4138 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004139 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004140 handleUnlockFunAttr(S, D, Attr);
4141 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004142 case AttributeList::AT_AcquiredBefore:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004143 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
4144 break;
Sean Huntbfcb0372012-06-19 03:39:03 +00004145 case AttributeList::AT_AcquiredAfter:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004146 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
4147 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004148
Chris Lattner803d0802008-06-29 00:43:07 +00004149 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004150 // Ask target about the attribute.
4151 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4152 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00004153 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
4154 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004155 break;
4156 }
4157}
4158
Peter Collingbourne60700392011-01-21 02:08:45 +00004159/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4160/// the attribute applies to decls. If the attribute is a type attribute, just
4161/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4162/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004163static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4164 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004165 bool NonInheritable, bool Inheritable) {
4166 if (Attr.isInvalid())
4167 return;
4168
4169 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
4170 // FIXME: Try to deal with other __declspec attributes!
4171 return;
4172
4173 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004174 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004175
4176 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004177 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004178}
4179
Chris Lattner803d0802008-06-29 00:43:07 +00004180/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4181/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004182void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004183 const AttributeList *AttrList,
4184 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004185 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004186 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004187 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004188
4189 // GCC accepts
4190 // static int a9 __attribute__((weakref));
4191 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004192 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004193 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004194 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004195 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004196 }
4197}
4198
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004199// Annotation attributes are the only attributes allowed after an access
4200// specifier.
4201bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4202 const AttributeList *AttrList) {
4203 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Huntbfcb0372012-06-19 03:39:03 +00004204 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004205 handleAnnotateAttr(*this, ASDecl, *l);
4206 } else {
4207 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4208 return true;
4209 }
4210 }
4211
4212 return false;
4213}
4214
John McCalle82247a2011-10-01 05:17:03 +00004215/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4216/// contains any decl attributes that we should warn about.
4217static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4218 for ( ; A; A = A->getNext()) {
4219 // Only warn if the attribute is an unignored, non-type attribute.
4220 if (A->isUsedAsTypeAttr()) continue;
4221 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4222
4223 if (A->getKind() == AttributeList::UnknownAttribute) {
4224 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4225 << A->getName() << A->getRange();
4226 } else {
4227 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4228 << A->getName() << A->getRange();
4229 }
4230 }
4231}
4232
4233/// checkUnusedDeclAttributes - Given a declarator which is not being
4234/// used to build a declaration, complain about any decl attributes
4235/// which might be lying around on it.
4236void Sema::checkUnusedDeclAttributes(Declarator &D) {
4237 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4238 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4239 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4240 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4241}
4242
Ryan Flynne25ff832009-07-30 03:15:39 +00004243/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004244/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004245NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4246 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004247 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004248 NamedDecl *NewD = 0;
4249 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004250 FunctionDecl *NewFD;
4251 // FIXME: Missing call to CheckFunctionDeclaration().
4252 // FIXME: Mangling?
4253 // FIXME: Is the qualifier info correct?
4254 // FIXME: Is the DeclContext correct?
4255 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4256 Loc, Loc, DeclarationName(II),
4257 FD->getType(), FD->getTypeSourceInfo(),
4258 SC_None, SC_None,
4259 false/*isInlineSpecified*/,
4260 FD->hasPrototype(),
4261 false/*isConstexprSpecified*/);
4262 NewD = NewFD;
4263
4264 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004265 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004266
4267 // Fake up parameter variables; they are declared as if this were
4268 // a typedef.
4269 QualType FDTy = FD->getType();
4270 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4271 SmallVector<ParmVarDecl*, 16> Params;
4272 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4273 AE = FT->arg_type_end(); AI != AE; ++AI) {
4274 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4275 Param->setScopeInfo(0, Params.size());
4276 Params.push_back(Param);
4277 }
David Blaikie4278c652011-09-21 18:16:56 +00004278 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004279 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004280 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4281 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004282 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004283 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004284 VD->getStorageClass(),
4285 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004286 if (VD->getQualifier()) {
4287 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004288 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004289 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004290 }
4291 return NewD;
4292}
4293
James Dennett1dfbd922012-06-14 21:40:34 +00004294/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004295/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004296void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004297 if (W.getUsed()) return; // only do this once
4298 W.setUsed(true);
4299 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4300 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004301 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004302 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4303 NDId->getName()));
4304 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004305 WeakTopLevelDecl.push_back(NewD);
4306 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4307 // to insert Decl at TU scope, sorry.
4308 DeclContext *SavedContext = CurContext;
4309 CurContext = Context.getTranslationUnitDecl();
4310 PushOnScopeChains(NewD, S);
4311 CurContext = SavedContext;
4312 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004313 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004314 }
4315}
4316
Chris Lattner0744e5f2008-06-29 00:23:49 +00004317/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4318/// it, apply them to D. This is a bit tricky because PD can have attributes
4319/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004320void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4321 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004322 // It's valid to "forward-declare" #pragma weak, in which case we
4323 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004324 if (Inheritable) {
4325 LoadExternalWeakUndeclaredIdentifiers();
4326 if (!WeakUndeclaredIdentifiers.empty()) {
4327 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4328 if (IdentifierInfo *Id = ND->getIdentifier()) {
4329 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4330 = WeakUndeclaredIdentifiers.find(Id);
4331 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4332 WeakInfo W = I->second;
4333 DeclApplyPragmaWeak(S, ND, W);
4334 WeakUndeclaredIdentifiers[Id] = W;
4335 }
John McCalld4aff0e2010-10-27 00:59:00 +00004336 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004337 }
4338 }
4339 }
4340
Chris Lattner0744e5f2008-06-29 00:23:49 +00004341 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004342 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004343 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004344
Chris Lattner0744e5f2008-06-29 00:23:49 +00004345 // Walk the declarator structure, applying decl attributes that were in a type
4346 // position to the decl itself. This handles cases like:
4347 // int *__attr__(x)** D;
4348 // when X is a decl attribute.
4349 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4350 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004351 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004352
Chris Lattner0744e5f2008-06-29 00:23:49 +00004353 // Finally, apply any attributes on the decl itself.
4354 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004355 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004356}
John McCall54abf7d2009-11-04 02:18:39 +00004357
John McCallf85e1932011-06-15 23:02:42 +00004358/// Is the given declaration allowed to use a forbidden type?
4359static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4360 // Private ivars are always okay. Unfortunately, people don't
4361 // always properly make their ivars private, even in system headers.
4362 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004363 // Function declarations in sys headers will be marked unavailable.
4364 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4365 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004366 return false;
4367
4368 // Require it to be declared in a system header.
4369 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4370}
4371
4372/// Handle a delayed forbidden-type diagnostic.
4373static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4374 Decl *decl) {
4375 if (decl && isForbiddenTypeAllowed(S, decl)) {
4376 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4377 "this system declaration uses an unsupported type"));
4378 return;
4379 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004380 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004381 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004382 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004383 // kind of forbidden type messages on unavailable functions.
4384 if (FD->hasAttr<UnavailableAttr>() &&
4385 diag.getForbiddenTypeDiagnostic() ==
4386 diag::err_arc_array_param_no_ownership) {
4387 diag.Triggered = true;
4388 return;
4389 }
4390 }
John McCallf85e1932011-06-15 23:02:42 +00004391
4392 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4393 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4394 diag.Triggered = true;
4395}
4396
John McCall92576642012-05-07 06:16:41 +00004397void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4398 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004399 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004400 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004401
John McCall92576642012-05-07 06:16:41 +00004402 // When delaying diagnostics to run in the context of a parsed
4403 // declaration, we only want to actually emit anything if parsing
4404 // succeeds.
4405 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004406
John McCall92576642012-05-07 06:16:41 +00004407 // We emit all the active diagnostics in this pool or any of its
4408 // parents. In general, we'll get one pool for the decl spec
4409 // and a child pool for each declarator; in a decl group like:
4410 // deprecated_typedef foo, *bar, baz();
4411 // only the declarator pops will be passed decls. This is correct;
4412 // we really do need to consider delayed diagnostics from the decl spec
4413 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004414 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004415 do {
John McCall13489672012-05-07 06:16:58 +00004416 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004417 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4418 // This const_cast is a bit lame. Really, Triggered should be mutable.
4419 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004420 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004421 continue;
4422
John McCalleee1d542011-02-14 07:13:47 +00004423 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004424 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004425 // Don't bother giving deprecation diagnostics if the decl is invalid.
4426 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004427 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004428 break;
4429
4430 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004431 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004432 break;
John McCallf85e1932011-06-15 23:02:42 +00004433
4434 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004435 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004436 break;
John McCall2f514482010-01-27 03:50:35 +00004437 }
4438 }
John McCall92576642012-05-07 06:16:41 +00004439 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004440}
4441
John McCall13489672012-05-07 06:16:58 +00004442/// Given a set of delayed diagnostics, re-emit them as if they had
4443/// been delayed in the current context instead of in the given pool.
4444/// Essentially, this just moves them to the current pool.
4445void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4446 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4447 assert(curPool && "re-emitting in undelayed context not supported");
4448 curPool->steal(pool);
4449}
4450
John McCall54abf7d2009-11-04 02:18:39 +00004451static bool isDeclDeprecated(Decl *D) {
4452 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004453 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004454 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004455 // A category implicitly has the availability of the interface.
4456 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4457 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004458 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4459 return false;
4460}
4461
John McCall9c3087b2010-08-26 02:13:20 +00004462void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004463 Decl *Ctx) {
4464 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004465 return;
4466
John McCall2f514482010-01-27 03:50:35 +00004467 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004468 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004469 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004470 << DD.getDeprecationDecl()->getDeclName()
4471 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004472 else if (DD.getUnknownObjCClass()) {
4473 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4474 << DD.getDeprecationDecl()->getDeclName();
4475 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4476 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004477 else
4478 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004479 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004480}
4481
Chris Lattner5f9e2722011-07-23 10:55:15 +00004482void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004483 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004484 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004485 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004486 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004487 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4488 UnknownObjCClass,
4489 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004490 return;
4491 }
4492
4493 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004494 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004495 return;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004496 if (!Message.empty()) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004497 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4498 << Message;
Fariborz Jahaniand6724362012-04-23 20:30:52 +00004499 Diag(D->getLocation(),
4500 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4501 : diag::note_previous_decl) << D->getDeclName();
4502 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004503 else {
Fariborz Jahanian350e9562012-05-27 16:59:48 +00004504 if (!UnknownObjCClass) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004505 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian350e9562012-05-27 16:59:48 +00004506 Diag(D->getLocation(),
4507 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4508 : diag::note_previous_decl) << D->getDeclName();
4509 }
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004510 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004511 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004512 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4513 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004514 }
John McCall54abf7d2009-11-04 02:18:39 +00004515}