1 //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements decl-related attribute processing.
12 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "clang/Parse/DeclSpec.h"
20 #include <llvm/ADT/StringExtras.h>
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) {
29 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
40 else if (blocksToo && Ty->isBlockPointerType())
41 Ty = Ty->getAsBlockPointerType()->getPointeeType();
43 return Ty->getAsFunctionType();
46 // FIXME: We should provide an abstraction around a method or function
47 // to provide the following bits of information.
49 /// isFunctionOrMethod - Return true if the given decl has function
50 /// type (function or function-typed variable) or an Objective-C
52 static bool isFunctionOrMethod(Decl *d) {
53 return getFunctionType(d, false) || isa<ObjCMethodDecl>(d);
56 /// isFunctionOrMethodOrBlock - Return true if the given decl has function
57 /// type (function or function-typed variable) or an Objective-C
58 /// method or a block.
59 static bool isFunctionOrMethodOrBlock(Decl *d) {
60 if (isFunctionOrMethod(d))
62 // check for block is more involved.
63 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64 QualType Ty = V->getType();
65 return Ty->isBlockPointerType();
67 return isa<BlockDecl>(d);
70 /// hasFunctionProto - Return true if the given decl has a argument
71 /// information. This decl should have already passed
72 /// isFunctionOrMethod or isFunctionOrMethodOrBlock.
73 static bool hasFunctionProto(Decl *d) {
74 if (const FunctionType *FnTy = getFunctionType(d))
75 return isa<FunctionProtoType>(FnTy);
77 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
82 /// getFunctionOrMethodNumArgs - Return number of function or method
83 /// arguments. It is an error to call this on a K&R function (use
84 /// hasFunctionProto first).
85 static unsigned getFunctionOrMethodNumArgs(Decl *d) {
86 if (const FunctionType *FnTy = getFunctionType(d))
87 return cast<FunctionProtoType>(FnTy)->getNumArgs();
88 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
89 return BD->getNumParams();
90 return cast<ObjCMethodDecl>(d)->param_size();
93 static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
94 if (const FunctionType *FnTy = getFunctionType(d))
95 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
96 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getParamDecl(Idx)->getType();
99 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
102 static QualType getFunctionOrMethodResultType(Decl *d) {
103 if (const FunctionType *FnTy = getFunctionType(d))
104 return cast<FunctionProtoType>(FnTy)->getResultType();
105 return cast<ObjCMethodDecl>(d)->getResultType();
108 static bool isFunctionOrMethodVariadic(Decl *d) {
109 if (const FunctionType *FnTy = getFunctionType(d)) {
110 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
111 return proto->isVariadic();
112 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
113 return BD->IsVariadic();
115 return cast<ObjCMethodDecl>(d)->isVariadic();
119 static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
120 const PointerType *PT = T->getAsPointerType();
124 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
128 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
130 // FIXME: Should we walk the chain of classes?
131 return ClsName == &Ctx.Idents.get("NSString") ||
132 ClsName == &Ctx.Idents.get("NSMutableString");
135 static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
136 const PointerType *PT = T->getAsPointerType();
140 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
144 const RecordDecl *RD = RT->getDecl();
145 if (RD->getTagKind() != TagDecl::TK_struct)
148 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
151 //===----------------------------------------------------------------------===//
152 // Attribute Implementations
153 //===----------------------------------------------------------------------===//
155 // FIXME: All this manual attribute parsing code is gross. At the
156 // least add some helper functions to check most argument patterns (#
157 // and types of args).
159 static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
161 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
163 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
167 QualType curType = tDecl->getUnderlyingType();
168 // check the attribute arguments.
169 if (Attr.getNumArgs() != 1) {
170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
173 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
174 llvm::APSInt vecSize(32);
175 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
176 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
177 << "ext_vector_type" << sizeExpr->getSourceRange();
180 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
181 // in conjunction with complex types (pointers, arrays, functions, etc.).
182 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
183 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
186 // unlike gcc's vector_size attribute, the size is specified as the
187 // number of elements, not the number of bytes.
188 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
190 if (vectorSize == 0) {
191 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
192 << sizeExpr->getSourceRange();
195 // Instantiate/Install the vector type, the number of elements is > 0.
196 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
197 // Remember this typedef decl, we will need it later for diagnostics.
198 S.ExtVectorDecls.push_back(tDecl);
202 /// HandleVectorSizeAttribute - this attribute is only applicable to
203 /// integral and float scalars, although arrays, pointers, and function
204 /// return values are allowed in conjunction with this construct. Aggregates
205 /// with this attribute are invalid, even if they are of the same size as a
206 /// corresponding scalar.
207 /// The raw attribute should contain precisely 1 argument, the vector size
208 /// for the variable, measured in bytes. If curType and rawAttr are well
209 /// formed, this routine will return a new vector type.
210 static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
212 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
213 CurType = VD->getType();
214 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
215 CurType = TD->getUnderlyingType();
217 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
218 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
222 // Check the attribute arugments.
223 if (Attr.getNumArgs() != 1) {
224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
227 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
228 llvm::APSInt vecSize(32);
229 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
231 << "vector_size" << sizeExpr->getSourceRange();
234 // navigate to the base type - we need to provide for vector pointers,
235 // vector arrays, and functions returning vectors.
236 if (CurType->isPointerType() || CurType->isArrayType() ||
237 CurType->isFunctionType()) {
238 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
240 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
242 if (PointerType *PT = dyn_cast<PointerType>(canonType))
243 canonType = PT->getPointeeType().getTypePtr();
244 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
245 canonType = AT->getElementType().getTypePtr();
246 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
247 canonType = FT->getResultType().getTypePtr();
248 } while (canonType->isPointerType() || canonType->isArrayType() ||
249 canonType->isFunctionType());
252 // the base type must be integer or float, and can't already be a vector.
253 if (CurType->isVectorType() ||
254 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
255 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
258 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
259 // vecSize is specified in bytes - convert to bits.
260 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
262 // the vector size needs to be an integral multiple of the type size.
263 if (vectorSize % typeSize) {
264 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
265 << sizeExpr->getSourceRange();
268 if (vectorSize == 0) {
269 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
270 << sizeExpr->getSourceRange();
274 // Success! Instantiate the vector type, the number of elements is > 0, and
275 // not required to be a power of 2, unlike GCC.
276 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
278 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
279 VD->setType(CurType);
281 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
284 static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
285 // check the attribute arguments.
286 if (Attr.getNumArgs() > 0) {
287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
291 if (TagDecl *TD = dyn_cast<TagDecl>(d))
292 TD->addAttr(::new (S.Context) PackedAttr(1));
293 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
294 // If the alignment is less than or equal to 8 bits, the packed attribute
296 if (!FD->getType()->isIncompleteType() &&
297 S.Context.getTypeAlign(FD->getType()) <= 8)
298 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
299 << Attr.getName() << FD->getType();
301 FD->addAttr(::new (S.Context) PackedAttr(1));
303 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
306 static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
307 // check the attribute arguments.
308 if (Attr.getNumArgs() > 0) {
309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
313 // The IBOutlet attribute only applies to instance variables of Objective-C
315 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
316 d->addAttr(::new (S.Context) IBOutletAttr());
318 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
321 static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
322 // GCC ignores the nonnull attribute on K&R style function
323 // prototypes, so we ignore it as well
324 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
325 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
326 << Attr.getName() << 0 /*function*/;
330 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
332 // The nonnull attribute only applies to pointers.
333 llvm::SmallVector<unsigned, 10> NonNullArgs;
335 for (AttributeList::arg_iterator I=Attr.arg_begin(),
336 E=Attr.arg_end(); I!=E; ++I) {
339 // The argument must be an integer constant expression.
340 Expr *Ex = static_cast<Expr *>(*I);
341 llvm::APSInt ArgNum(32);
342 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
343 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
344 << "nonnull" << Ex->getSourceRange();
348 unsigned x = (unsigned) ArgNum.getZExtValue();
350 if (x < 1 || x > NumArgs) {
351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
352 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
358 // Is the function argument a pointer type?
359 QualType T = getFunctionOrMethodArgType(d, x);
360 if (!T->isPointerType() && !T->isBlockPointerType()) {
361 // FIXME: Should also highlight argument in decl.
362 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
363 << "nonnull" << Ex->getSourceRange();
367 NonNullArgs.push_back(x);
370 // If no arguments were specified to __attribute__((nonnull)) then all
371 // pointer arguments have a nonnull attribute.
372 if (NonNullArgs.empty()) {
373 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
374 QualType T = getFunctionOrMethodArgType(d, I);
375 if (T->isPointerType() || T->isBlockPointerType())
376 NonNullArgs.push_back(I);
379 if (NonNullArgs.empty()) {
380 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
385 unsigned* start = &NonNullArgs[0];
386 unsigned size = NonNullArgs.size();
387 std::sort(start, start + size);
388 d->addAttr(::new (S.Context) NonNullAttr(start, size));
391 static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
392 // check the attribute arguments.
393 if (Attr.getNumArgs() != 1) {
394 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
398 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
399 Arg = Arg->IgnoreParenCasts();
400 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
402 if (Str == 0 || Str->isWide()) {
403 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
408 const char *Alias = Str->getStrData();
409 unsigned AliasLen = Str->getByteLength();
411 // FIXME: check if target symbol exists in current file
413 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
416 static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
418 // check the attribute arguments.
419 if (Attr.getNumArgs() != 0) {
420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
424 if (!isa<FunctionDecl>(d)) {
425 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
426 << Attr.getName() << 0 /*function*/;
430 d->addAttr(::new (S.Context) AlwaysInlineAttr());
433 static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
435 // check the attribute arguments.
436 if (Attr.getNumArgs() != 0) {
437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
441 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
442 ValueDecl *VD = dyn_cast<ValueDecl>(d);
443 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
445 << Attr.getName() << 0 /*function*/;
453 static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
454 if (HandleCommonNoReturnAttr(d, Attr, S))
455 d->addAttr(::new (S.Context) NoReturnAttr());
458 static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
460 if (HandleCommonNoReturnAttr(d, Attr, S))
461 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
464 static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
465 // check the attribute arguments.
466 if (Attr.getNumArgs() != 0) {
467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
471 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
473 << Attr.getName() << 2 /*variable and function*/;
477 d->addAttr(::new (S.Context) UnusedAttr());
480 static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
481 // check the attribute arguments.
482 if (Attr.getNumArgs() != 0) {
483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
487 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
488 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
489 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
492 } else if (!isFunctionOrMethod(d)) {
493 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
494 << Attr.getName() << 2 /*variable and function*/;
498 d->addAttr(::new (S.Context) UsedAttr());
501 static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
502 // check the attribute arguments.
503 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
509 int priority = 65535; // FIXME: Do not hardcode such constants.
510 if (Attr.getNumArgs() > 0) {
511 Expr *E = static_cast<Expr *>(Attr.getArg(0));
512 llvm::APSInt Idx(32);
513 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
515 << "constructor" << 1 << E->getSourceRange();
518 priority = Idx.getZExtValue();
521 if (!isa<FunctionDecl>(d)) {
522 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
523 << Attr.getName() << 0 /*function*/;
527 d->addAttr(::new (S.Context) ConstructorAttr(priority));
530 static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
531 // check the attribute arguments.
532 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
538 int priority = 65535; // FIXME: Do not hardcode such constants.
539 if (Attr.getNumArgs() > 0) {
540 Expr *E = static_cast<Expr *>(Attr.getArg(0));
541 llvm::APSInt Idx(32);
542 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
543 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
544 << "destructor" << 1 << E->getSourceRange();
547 priority = Idx.getZExtValue();
550 if (!isa<FunctionDecl>(d)) {
551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
552 << Attr.getName() << 0 /*function*/;
556 d->addAttr(::new (S.Context) DestructorAttr(priority));
559 static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
560 // check the attribute arguments.
561 if (Attr.getNumArgs() != 0) {
562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
566 d->addAttr(::new (S.Context) DeprecatedAttr());
569 static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
570 // check the attribute arguments.
571 if (Attr.getNumArgs() != 0) {
572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
576 d->addAttr(::new (S.Context) UnavailableAttr());
579 static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
580 // check the attribute arguments.
581 if (Attr.getNumArgs() != 1) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
586 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
587 Arg = Arg->IgnoreParenCasts();
588 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
590 if (Str == 0 || Str->isWide()) {
591 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
592 << "visibility" << 1;
596 const char *TypeStr = Str->getStrData();
597 unsigned TypeLen = Str->getByteLength();
598 VisibilityAttr::VisibilityTypes type;
600 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
601 type = VisibilityAttr::DefaultVisibility;
602 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
603 type = VisibilityAttr::HiddenVisibility;
604 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
605 type = VisibilityAttr::HiddenVisibility; // FIXME
606 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
607 type = VisibilityAttr::ProtectedVisibility;
609 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
613 d->addAttr(::new (S.Context) VisibilityAttr(type));
616 static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
618 if (Attr.getNumArgs() != 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
623 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
625 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
629 D->addAttr(::new (S.Context) ObjCExceptionAttr());
632 static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
633 if (Attr.getNumArgs() != 0) {
634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
637 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
638 QualType T = TD->getUnderlyingType();
639 if (!T->isPointerType() ||
640 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
641 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
645 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
649 HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
650 if (Attr.getNumArgs() != 0) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
655 if (!isa<FunctionDecl>(D)) {
656 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
660 D->addAttr(::new (S.Context) OverloadableAttr());
663 static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
664 if (!Attr.getParameterName()) {
665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
670 if (Attr.getNumArgs() != 0) {
671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
675 BlocksAttr::BlocksAttrTypes type;
676 if (Attr.getParameterName()->isStr("byref"))
677 type = BlocksAttr::ByRef;
679 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
680 << "blocks" << Attr.getParameterName();
684 d->addAttr(::new (S.Context) BlocksAttr(type));
687 static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
688 // check the attribute arguments.
689 if (Attr.getNumArgs() > 2) {
690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
696 if (Attr.getNumArgs() > 0) {
697 Expr *E = static_cast<Expr *>(Attr.getArg(0));
698 llvm::APSInt Idx(32);
699 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
700 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
701 << "sentinel" << 1 << E->getSourceRange();
704 sentinel = Idx.getZExtValue();
707 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
708 << E->getSourceRange();
714 if (Attr.getNumArgs() > 1) {
715 Expr *E = static_cast<Expr *>(Attr.getArg(1));
716 llvm::APSInt Idx(32);
717 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
718 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
719 << "sentinel" << 2 << E->getSourceRange();
722 nullPos = Idx.getZExtValue();
724 if (nullPos > 1 || nullPos < 0) {
725 // FIXME: This error message could be improved, it would be nice
726 // to say what the bounds actually are.
727 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
728 << E->getSourceRange();
733 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
734 const FunctionType *FT = FD->getType()->getAsFunctionType();
735 assert(FT && "FunctionDecl has non-function type?");
737 if (isa<FunctionNoProtoType>(FT)) {
738 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
742 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
743 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
746 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
747 if (!MD->isVariadic()) {
748 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
751 } else if (isa<BlockDecl>(d)) {
752 // Note! BlockDecl is typeless. Variadic diagnostics
753 // will be issued by the caller.
755 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
756 QualType Ty = V->getType();
757 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
758 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
759 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
760 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
761 int m = Ty->isFunctionPointerType() ? 0 : 1;
762 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
768 << Attr.getName() << 6 /*function, method or block */;
772 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
773 << Attr.getName() << 6 /*function, method or block */;
776 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
779 static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
780 // check the attribute arguments.
781 if (Attr.getNumArgs() != 0) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
786 // TODO: could also be applied to methods?
787 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
790 << Attr.getName() << 0 /*function*/;
794 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
797 static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
798 // check the attribute arguments.
799 if (Attr.getNumArgs() != 0) {
800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
804 // TODO: could also be applied to methods?
805 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
807 << Attr.getName() << 2 /*variable and function*/;
811 D->addAttr(::new (S.Context) WeakAttr());
814 static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
815 // check the attribute arguments.
816 if (Attr.getNumArgs() != 0) {
817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
821 // weak_import only applies to variable & function declarations.
823 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
824 isDef = (!VD->hasExternalStorage() || VD->getInit());
825 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
826 isDef = FD->getBody(S.Context);
827 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
828 // We ignore weak import on properties and methods
831 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
832 << Attr.getName() << 2 /*variable and function*/;
836 // Merge should handle any subsequent violations.
838 S.Diag(Attr.getLoc(),
839 diag::warn_attribute_weak_import_invalid_on_definition)
840 << "weak_import" << 2 /*variable and function*/;
844 D->addAttr(::new (S.Context) WeakImportAttr());
847 static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
848 // check the attribute arguments.
849 if (Attr.getNumArgs() != 0) {
850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
854 // Attribute can be applied only to functions or variables.
855 if (isa<VarDecl>(D)) {
856 D->addAttr(::new (S.Context) DLLImportAttr());
860 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
863 << Attr.getName() << 2 /*variable and function*/;
867 // Currently, the dllimport attribute is ignored for inlined functions.
868 // Warning is emitted.
869 if (FD->isInline()) {
870 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
874 // The attribute is also overridden by a subsequent declaration as dllexport.
875 // Warning is emitted.
876 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
877 nextAttr = nextAttr->getNext()) {
878 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
884 if (D->getAttr<DLLExportAttr>()) {
885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
889 D->addAttr(::new (S.Context) DLLImportAttr());
892 static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
893 // check the attribute arguments.
894 if (Attr.getNumArgs() != 0) {
895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
899 // Attribute can be applied only to functions or variables.
900 if (isa<VarDecl>(D)) {
901 D->addAttr(::new (S.Context) DLLExportAttr());
905 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
908 << Attr.getName() << 2 /*variable and function*/;
912 // Currently, the dllexport attribute is ignored for inlined functions,
913 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
914 if (FD->isInline()) {
915 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
920 D->addAttr(::new (S.Context) DLLExportAttr());
923 static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
924 // Attribute has no arguments.
925 if (Attr.getNumArgs() != 1) {
926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
930 // Make sure that there is a string literal as the sections's single
933 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
936 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
939 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
940 SE->getByteLength())));
943 static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
944 // Attribute has no arguments.
945 if (Attr.getNumArgs() != 0) {
946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
950 // Attribute can be applied only to functions.
951 if (!isa<FunctionDecl>(d)) {
952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
953 << Attr.getName() << 0 /*function*/;
957 // stdcall and fastcall attributes are mutually incompatible.
958 if (d->getAttr<FastCallAttr>()) {
959 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
960 << "stdcall" << "fastcall";
964 d->addAttr(::new (S.Context) StdCallAttr());
967 static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
968 // Attribute has no arguments.
969 if (Attr.getNumArgs() != 0) {
970 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
974 if (!isa<FunctionDecl>(d)) {
975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
976 << Attr.getName() << 0 /*function*/;
980 // stdcall and fastcall attributes are mutually incompatible.
981 if (d->getAttr<StdCallAttr>()) {
982 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
983 << "fastcall" << "stdcall";
987 d->addAttr(::new (S.Context) FastCallAttr());
990 static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
991 // check the attribute arguments.
992 if (Attr.getNumArgs() != 0) {
993 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
997 d->addAttr(::new (S.Context) NoThrowAttr());
1000 static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1001 // check the attribute arguments.
1002 if (Attr.getNumArgs() != 0) {
1003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1007 d->addAttr(::new (S.Context) ConstAttr());
1010 static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1011 // check the attribute arguments.
1012 if (Attr.getNumArgs() != 0) {
1013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1017 d->addAttr(::new (S.Context) PureAttr());
1020 static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1021 // Match gcc which ignores cleanup attrs when compiling C++.
1022 if (S.getLangOptions().CPlusPlus)
1025 if (!Attr.getParameterName()) {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1030 if (Attr.getNumArgs() != 0) {
1031 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1035 VarDecl *VD = dyn_cast<VarDecl>(d);
1037 if (!VD || !VD->hasLocalStorage()) {
1038 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1042 // Look up the function
1043 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1044 Sema::LookupOrdinaryName);
1046 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
1047 Attr.getParameterName();
1051 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1053 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
1054 Attr.getParameterName();
1058 if (FD->getNumParams() != 1) {
1059 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
1060 Attr.getParameterName();
1064 // We're currently more strict than GCC about what function types we accept.
1065 // If this ever proves to be a problem it should be easy to fix.
1066 QualType Ty = S.Context.getPointerType(VD->getType());
1067 QualType ParamTy = FD->getParamDecl(0)->getType();
1068 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
1069 S.Diag(Attr.getLoc(),
1070 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1071 Attr.getParameterName() << ParamTy << Ty;
1075 d->addAttr(::new (S.Context) CleanupAttr(FD));
1078 /// Handle __attribute__((format_arg((idx)))) attribute
1079 /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1080 static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1081 if (Attr.getNumArgs() != 1) {
1082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1085 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1086 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1087 << Attr.getName() << 0 /*function*/;
1090 // FIXME: in C++ the implicit 'this' function parameter also counts.
1091 // this is needed in order to be compatible with GCC
1092 // the index must start with 1.
1093 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1094 unsigned FirstIdx = 1;
1095 // checks for the 2nd argument
1096 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1097 llvm::APSInt Idx(32);
1098 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1100 << "format" << 2 << IdxExpr->getSourceRange();
1104 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1106 << "format" << 2 << IdxExpr->getSourceRange();
1110 unsigned ArgIdx = Idx.getZExtValue() - 1;
1112 // make sure the format string is really a string
1113 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1115 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1116 if (not_nsstring_type &&
1117 !isCFStringType(Ty, S.Context) &&
1118 (!Ty->isPointerType() ||
1119 !Ty->getAsPointerType()->getPointeeType()->isCharType())) {
1120 // FIXME: Should highlight the actual expression that has the wrong type.
1121 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1122 << (not_nsstring_type ? "a string type" : "an NSString")
1123 << IdxExpr->getSourceRange();
1126 Ty = getFunctionOrMethodResultType(d);
1127 if (!isNSStringType(Ty, S.Context) &&
1128 !isCFStringType(Ty, S.Context) &&
1129 (!Ty->isPointerType() ||
1130 !Ty->getAsPointerType()->getPointeeType()->isCharType())) {
1131 // FIXME: Should highlight the actual expression that has the wrong type.
1132 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
1133 << (not_nsstring_type ? "string type" : "NSString")
1134 << IdxExpr->getSourceRange();
1138 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
1141 /// Handle __attribute__((format(type,idx,firstarg))) attributes
1142 /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1143 static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1145 if (!Attr.getParameterName()) {
1146 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1151 if (Attr.getNumArgs() != 2) {
1152 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
1156 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
1157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1158 << Attr.getName() << 0 /*function*/;
1162 // FIXME: in C++ the implicit 'this' function parameter also counts.
1163 // this is needed in order to be compatible with GCC
1164 // the index must start in 1 and the limit is numargs+1
1165 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1166 unsigned FirstIdx = 1;
1168 const char *Format = Attr.getParameterName()->getName();
1169 unsigned FormatLen = Attr.getParameterName()->getLength();
1171 // Normalize the argument, __foo__ becomes foo.
1172 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1173 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1178 bool Supported = false;
1179 bool is_NSString = false;
1180 bool is_strftime = false;
1181 bool is_CFString = false;
1183 switch (FormatLen) {
1185 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1186 case 6: Supported = !memcmp(Format, "printf", 6); break;
1187 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
1189 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1190 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1191 (is_CFString = !memcmp(Format, "CFString", 8));
1196 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1197 << "format" << Attr.getParameterName()->getName();
1201 // checks for the 2nd argument
1202 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1203 llvm::APSInt Idx(32);
1204 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1206 << "format" << 2 << IdxExpr->getSourceRange();
1210 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1212 << "format" << 2 << IdxExpr->getSourceRange();
1216 // FIXME: Do we need to bounds check?
1217 unsigned ArgIdx = Idx.getZExtValue() - 1;
1219 // make sure the format string is really a string
1220 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1223 if (!isCFStringType(Ty, S.Context)) {
1224 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1225 << "a CFString" << IdxExpr->getSourceRange();
1228 } else if (is_NSString) {
1229 // FIXME: do we need to check if the type is NSString*? What are the
1231 if (!isNSStringType(Ty, S.Context)) {
1232 // FIXME: Should highlight the actual expression that has the wrong type.
1233 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1234 << "an NSString" << IdxExpr->getSourceRange();
1237 } else if (!Ty->isPointerType() ||
1238 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1239 // FIXME: Should highlight the actual expression that has the wrong type.
1240 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1241 << "a string type" << IdxExpr->getSourceRange();
1245 // check the 3rd argument
1246 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
1247 llvm::APSInt FirstArg(32);
1248 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
1249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1250 << "format" << 3 << FirstArgExpr->getSourceRange();
1254 // check if the function is variadic if the 3rd argument non-zero
1255 if (FirstArg != 0) {
1256 if (isFunctionOrMethodVariadic(d)) {
1257 ++NumArgs; // +1 for ...
1259 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
1264 // strftime requires FirstArg to be 0 because it doesn't read from any
1265 // variable the input is just the current time + the format string.
1267 if (FirstArg != 0) {
1268 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1269 << FirstArgExpr->getSourceRange();
1272 // if 0 it disables parameter checking (to use with e.g. va_list)
1273 } else if (FirstArg != 0 && FirstArg != NumArgs) {
1274 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1275 << "format" << 3 << FirstArgExpr->getSourceRange();
1279 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
1280 Idx.getZExtValue(), FirstArg.getZExtValue()));
1283 static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1285 // check the attribute arguments.
1286 if (Attr.getNumArgs() != 0) {
1287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1291 // Try to find the underlying union declaration.
1293 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1294 if (TD && TD->getUnderlyingType()->isUnionType())
1295 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1297 RD = dyn_cast<RecordDecl>(d);
1299 if (!RD || !RD->isUnion()) {
1300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1301 << Attr.getName() << 1 /*union*/;
1305 if (!RD->isDefinition()) {
1306 S.Diag(Attr.getLoc(),
1307 diag::warn_transparent_union_attribute_not_definition);
1311 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1312 FieldEnd = RD->field_end(S.Context);
1313 if (Field == FieldEnd) {
1314 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1318 FieldDecl *FirstField = *Field;
1319 QualType FirstType = FirstField->getType();
1320 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1321 S.Diag(FirstField->getLocation(),
1322 diag::warn_transparent_union_attribute_floating);
1326 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1327 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1328 for (; Field != FieldEnd; ++Field) {
1329 QualType FieldType = Field->getType();
1330 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1331 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1332 // Warn if we drop the attribute.
1333 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1334 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1335 : S.Context.getTypeAlign(FieldType);
1336 S.Diag(Field->getLocation(),
1337 diag::warn_transparent_union_attribute_field_size_align)
1338 << isSize << Field->getDeclName() << FieldBits;
1339 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1340 S.Diag(FirstField->getLocation(),
1341 diag::note_transparent_union_first_field_size_align)
1342 << isSize << FirstBits;
1347 RD->addAttr(::new (S.Context) TransparentUnionAttr());
1350 static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1351 // check the attribute arguments.
1352 if (Attr.getNumArgs() != 1) {
1353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1356 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
1357 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1359 // Make sure that there is a string literal as the annotation's single
1362 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
1365 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1366 SE->getByteLength())));
1369 static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1370 // check the attribute arguments.
1371 if (Attr.getNumArgs() > 1) {
1372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1377 if (Attr.getNumArgs() == 0) {
1378 // FIXME: This should be the target specific maximum alignment.
1379 // (For now we just use 128 bits which is the maximum on X86).
1381 d->addAttr(::new (S.Context) AlignedAttr(Align));
1385 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1386 llvm::APSInt Alignment(32);
1387 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
1388 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1389 << "aligned" << alignmentExpr->getSourceRange();
1392 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1393 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1394 << alignmentExpr->getSourceRange();
1398 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
1401 /// HandleModeAttr - This attribute modifies the width of a decl with
1404 /// Despite what would be logical, the mode attribute is a decl attribute,
1405 /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1406 /// 'G' be HImode, not an intermediate pointer.
1408 static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1409 // This attribute isn't documented, but glibc uses it. It changes
1410 // the width of an int or unsigned int to the specified size.
1412 // Check that there aren't any arguments
1413 if (Attr.getNumArgs() != 0) {
1414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1418 IdentifierInfo *Name = Attr.getParameterName();
1420 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
1423 const char *Str = Name->getName();
1424 unsigned Len = Name->getLength();
1426 // Normalize the attribute name, __foo__ becomes foo.
1427 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1428 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1433 unsigned DestWidth = 0;
1434 bool IntegerMode = true;
1435 bool ComplexMode = false;
1439 case 'Q': DestWidth = 8; break;
1440 case 'H': DestWidth = 16; break;
1441 case 'S': DestWidth = 32; break;
1442 case 'D': DestWidth = 64; break;
1443 case 'X': DestWidth = 96; break;
1444 case 'T': DestWidth = 128; break;
1446 if (Str[1] == 'F') {
1447 IntegerMode = false;
1448 } else if (Str[1] == 'C') {
1449 IntegerMode = false;
1451 } else if (Str[1] != 'I') {
1456 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1457 // pointer on PIC16 and other embedded platforms.
1458 if (!memcmp(Str, "word", 4))
1459 DestWidth = S.Context.Target.getPointerWidth(0);
1460 if (!memcmp(Str, "byte", 4))
1461 DestWidth = S.Context.Target.getCharWidth();
1464 if (!memcmp(Str, "pointer", 7))
1465 DestWidth = S.Context.Target.getPointerWidth(0);
1470 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1471 OldTy = TD->getUnderlyingType();
1472 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1473 OldTy = VD->getType();
1475 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1476 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
1480 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1481 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1482 else if (IntegerMode) {
1483 if (!OldTy->isIntegralType())
1484 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1485 } else if (ComplexMode) {
1486 if (!OldTy->isComplexType())
1487 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1489 if (!OldTy->isFloatingType())
1490 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1493 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1494 // and friends, at least with glibc.
1495 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1496 // width on unusual platforms.
1497 // FIXME: Make sure floating-point mappings are accurate
1498 // FIXME: Support XF and TF types
1500 switch (DestWidth) {
1502 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
1505 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1509 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1512 if (OldTy->isSignedIntegerType())
1513 NewTy = S.Context.SignedCharTy;
1515 NewTy = S.Context.UnsignedCharTy;
1519 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1522 if (OldTy->isSignedIntegerType())
1523 NewTy = S.Context.ShortTy;
1525 NewTy = S.Context.UnsignedShortTy;
1529 NewTy = S.Context.FloatTy;
1530 else if (OldTy->isSignedIntegerType())
1531 NewTy = S.Context.IntTy;
1533 NewTy = S.Context.UnsignedIntTy;
1537 NewTy = S.Context.DoubleTy;
1538 else if (OldTy->isSignedIntegerType())
1539 NewTy = S.Context.LongLongTy;
1541 NewTy = S.Context.UnsignedLongLongTy;
1544 NewTy = S.Context.LongDoubleTy;
1548 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1551 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
1556 NewTy = S.Context.getComplexType(NewTy);
1559 // Install the new type.
1560 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1561 TD->setUnderlyingType(NewTy);
1563 cast<ValueDecl>(D)->setType(NewTy);
1566 static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1567 // check the attribute arguments.
1568 if (Attr.getNumArgs() > 0) {
1569 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1573 if (!isFunctionOrMethod(d)) {
1574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1575 << Attr.getName() << 0 /*function*/;
1579 d->addAttr(::new (S.Context) NodebugAttr());
1582 static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1583 // check the attribute arguments.
1584 if (Attr.getNumArgs() != 0) {
1585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1589 if (!isa<FunctionDecl>(d)) {
1590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1591 << Attr.getName() << 0 /*function*/;
1595 d->addAttr(::new (S.Context) NoinlineAttr());
1598 static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1599 // check the attribute arguments.
1600 if (Attr.getNumArgs() != 0) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1605 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1608 << Attr.getName() << 0 /*function*/;
1612 if (!Fn->isInline()) {
1613 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
1617 d->addAttr(::new (S.Context) GNUInlineAttr());
1620 static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1621 // check the attribute arguments.
1622 if (Attr.getNumArgs() != 1) {
1623 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1627 if (!isFunctionOrMethod(d)) {
1628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1629 << Attr.getName() << 0 /*function*/;
1633 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1634 llvm::APSInt NumParams(32);
1635 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1637 << "regparm" << NumParamsExpr->getSourceRange();
1641 if (S.Context.Target.getRegParmMax() == 0) {
1642 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
1643 << NumParamsExpr->getSourceRange();
1647 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
1648 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1649 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
1653 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
1656 //===----------------------------------------------------------------------===//
1657 // Checker-specific attribute handlers.
1658 //===----------------------------------------------------------------------===//
1660 static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1665 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1666 RetTy = MD->getResultType();
1667 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1668 RetTy = FD->getResultType();
1670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1671 << Attr.getName() << 3 /* function or method */;
1675 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1676 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1681 switch (Attr.getKind()) {
1683 assert(0 && "invalid ownership attribute");
1685 case AttributeList::AT_cf_returns_retained:
1686 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1688 case AttributeList::AT_ns_returns_retained:
1689 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1694 //===----------------------------------------------------------------------===//
1695 // Top Level Sema Entry Points
1696 //===----------------------------------------------------------------------===//
1698 /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
1699 /// the attribute applies to decls. If the attribute is a type attribute, just
1700 /// silently ignore it.
1701 static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1702 if (Attr.isDeclspecAttribute())
1703 // FIXME: Try to deal with __declspec attributes!
1705 switch (Attr.getKind()) {
1706 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
1707 case AttributeList::AT_address_space:
1708 case AttributeList::AT_objc_gc:
1709 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
1711 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1712 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
1713 case AttributeList::AT_always_inline:
1714 HandleAlwaysInlineAttr (D, Attr, S); break;
1715 case AttributeList::AT_analyzer_noreturn:
1716 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
1717 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1718 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1719 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1720 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1721 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1722 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
1723 case AttributeList::AT_ext_vector_type:
1724 HandleExtVectorTypeAttr(D, Attr, S);
1726 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1727 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1728 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1729 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
1730 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1731 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1732 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1733 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1735 // Checker-specific.
1736 case AttributeList::AT_ns_returns_retained:
1737 case AttributeList::AT_cf_returns_retained:
1738 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1740 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1741 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1742 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1743 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
1744 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1745 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
1746 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
1747 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1748 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1750 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1751 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
1752 case AttributeList::AT_transparent_union:
1753 HandleTransparentUnionAttr(D, Attr, S);
1755 case AttributeList::AT_objc_exception:
1756 HandleObjCExceptionAttr(D, Attr, S);
1758 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
1759 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1760 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1761 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1762 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1763 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1764 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1765 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
1766 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
1767 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
1768 case AttributeList::IgnoredAttribute:
1769 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
1773 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1778 /// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1779 /// attribute list to the specified decl, ignoring any type attributes.
1780 void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1782 ProcessDeclAttribute(D, *AttrList, *this);
1783 AttrList = AttrList->getNext();
1787 /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1788 /// it, apply them to D. This is a bit tricky because PD can have attributes
1789 /// specified in many different places, and we need to find and apply them all.
1790 void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1791 // Apply decl attributes from the DeclSpec if present.
1792 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1793 ProcessDeclAttributeList(D, Attrs);
1795 // Walk the declarator structure, applying decl attributes that were in a type
1796 // position to the decl itself. This handles cases like:
1797 // int *__attr__(x)** D;
1798 // when X is a decl attribute.
1799 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1800 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1801 ProcessDeclAttributeList(D, Attrs);
1803 // Finally, apply any attributes on the decl itself.
1804 if (const AttributeList *Attrs = PD.getAttributes())
1805 ProcessDeclAttributeList(D, Attrs);