SemaDeclAttr.cpp revision 13d4dd133a042a70f23acb6b1a764138452ce54a
1//===--- 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 14#include "Sema.h" 15#include "TargetAttributesSema.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/Expr.h" 19#include "clang/Basic/TargetInfo.h" 20#include "clang/Parse/DeclSpec.h" 21#include "llvm/ADT/StringExtras.h" 22using namespace clang; 23 24//===----------------------------------------------------------------------===// 25// Helper functions 26//===----------------------------------------------------------------------===// 27 28static const FunctionType *getFunctionType(const Decl *d, 29 bool blocksToo = true) { 30 QualType Ty; 31 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d)) 32 Ty = decl->getType(); 33 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d)) 34 Ty = decl->getType(); 35 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) 36 Ty = decl->getUnderlyingType(); 37 else 38 return 0; 39 40 if (Ty->isFunctionPointerType()) 41 Ty = Ty->getAs<PointerType>()->getPointeeType(); 42 else if (blocksToo && Ty->isBlockPointerType()) 43 Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); 44 45 return Ty->getAs<FunctionType>(); 46} 47 48// FIXME: We should provide an abstraction around a method or function 49// to provide the following bits of information. 50 51/// isFunction - Return true if the given decl has function 52/// type (function or function-typed variable). 53static bool isFunction(const Decl *d) { 54 return getFunctionType(d, false) != NULL; 55} 56 57/// isFunctionOrMethod - Return true if the given decl has function 58/// type (function or function-typed variable) or an Objective-C 59/// method. 60static bool isFunctionOrMethod(const Decl *d) { 61 return isFunction(d)|| isa<ObjCMethodDecl>(d); 62} 63 64/// isFunctionOrMethodOrBlock - Return true if the given decl has function 65/// type (function or function-typed variable) or an Objective-C 66/// method or a block. 67static bool isFunctionOrMethodOrBlock(const Decl *d) { 68 if (isFunctionOrMethod(d)) 69 return true; 70 // check for block is more involved. 71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) { 72 QualType Ty = V->getType(); 73 return Ty->isBlockPointerType(); 74 } 75 return isa<BlockDecl>(d); 76} 77 78/// hasFunctionProto - Return true if the given decl has a argument 79/// information. This decl should have already passed 80/// isFunctionOrMethod or isFunctionOrMethodOrBlock. 81static bool hasFunctionProto(const Decl *d) { 82 if (const FunctionType *FnTy = getFunctionType(d)) 83 return isa<FunctionProtoType>(FnTy); 84 else { 85 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d)); 86 return true; 87 } 88} 89 90/// getFunctionOrMethodNumArgs - Return number of function or method 91/// arguments. It is an error to call this on a K&R function (use 92/// hasFunctionProto first). 93static unsigned getFunctionOrMethodNumArgs(const Decl *d) { 94 if (const FunctionType *FnTy = getFunctionType(d)) 95 return cast<FunctionProtoType>(FnTy)->getNumArgs(); 96 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) 97 return BD->getNumParams(); 98 return cast<ObjCMethodDecl>(d)->param_size(); 99} 100 101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) { 102 if (const FunctionType *FnTy = getFunctionType(d)) 103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx); 104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) 105 return BD->getParamDecl(Idx)->getType(); 106 107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType(); 108} 109 110static QualType getFunctionOrMethodResultType(const Decl *d) { 111 if (const FunctionType *FnTy = getFunctionType(d)) 112 return cast<FunctionProtoType>(FnTy)->getResultType(); 113 return cast<ObjCMethodDecl>(d)->getResultType(); 114} 115 116static bool isFunctionOrMethodVariadic(const Decl *d) { 117 if (const FunctionType *FnTy = getFunctionType(d)) { 118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); 119 return proto->isVariadic(); 120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) 121 return BD->isVariadic(); 122 else { 123 return cast<ObjCMethodDecl>(d)->isVariadic(); 124 } 125} 126 127static inline bool isNSStringType(QualType T, ASTContext &Ctx) { 128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); 129 if (!PT) 130 return false; 131 132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>(); 133 if (!ClsT) 134 return false; 135 136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); 137 138 // FIXME: Should we walk the chain of classes? 139 return ClsName == &Ctx.Idents.get("NSString") || 140 ClsName == &Ctx.Idents.get("NSMutableString"); 141} 142 143static inline bool isCFStringType(QualType T, ASTContext &Ctx) { 144 const PointerType *PT = T->getAs<PointerType>(); 145 if (!PT) 146 return false; 147 148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); 149 if (!RT) 150 return false; 151 152 const RecordDecl *RD = RT->getDecl(); 153 if (RD->getTagKind() != TagDecl::TK_struct) 154 return false; 155 156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); 157} 158 159//===----------------------------------------------------------------------===// 160// Attribute Implementations 161//===----------------------------------------------------------------------===// 162 163// FIXME: All this manual attribute parsing code is gross. At the 164// least add some helper functions to check most argument patterns (# 165// and types of args). 166 167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, 168 const AttributeList &Attr, Sema &S) { 169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); 170 if (tDecl == 0) { 171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); 172 return; 173 } 174 175 QualType curType = tDecl->getUnderlyingType(); 176 177 Expr *sizeExpr; 178 179 // Special case where the argument is a template id. 180 if (Attr.getParameterName()) { 181 CXXScopeSpec SS; 182 UnqualifiedId id; 183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); 184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>(); 185 } else { 186 // check the attribute arguments. 187 if (Attr.getNumArgs() != 1) { 188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 189 return; 190 } 191 sizeExpr = static_cast<Expr *>(Attr.getArg(0)); 192 } 193 194 // Instantiate/Install the vector type, and let Sema build the type for us. 195 // This will run the reguired checks. 196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc()); 197 if (!T.isNull()) { 198 // FIXME: preserve the old source info. 199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); 200 201 // Remember this typedef decl, we will need it later for diagnostics. 202 S.ExtVectorDecls.push_back(tDecl); 203 } 204} 205 206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 207 // check the attribute arguments. 208 if (Attr.getNumArgs() > 0) { 209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 210 return; 211 } 212 213 if (TagDecl *TD = dyn_cast<TagDecl>(d)) 214 TD->addAttr(::new (S.Context) PackedAttr); 215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { 216 // If the alignment is less than or equal to 8 bits, the packed attribute 217 // has no effect. 218 if (!FD->getType()->isIncompleteType() && 219 S.Context.getTypeAlign(FD->getType()) <= 8) 220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) 221 << Attr.getName() << FD->getType(); 222 else 223 FD->addAttr(::new (S.Context) PackedAttr); 224 } else 225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 226} 227 228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { 229 // check the attribute arguments. 230 if (Attr.getNumArgs() > 0) { 231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 232 return; 233 } 234 235 // The IBAction attributes only apply to instance methods. 236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) 237 if (MD->isInstanceMethod()) { 238 d->addAttr(::new (S.Context) IBActionAttr()); 239 return; 240 } 241 242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName(); 243} 244 245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) { 246 // check the attribute arguments. 247 if (Attr.getNumArgs() > 0) { 248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 249 return; 250 } 251 252 // The IBOutlet attributes only apply to instance variables of 253 // Objective-C classes. 254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) { 255 d->addAttr(::new (S.Context) IBOutletAttr()); 256 return; 257 } 258 259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName(); 260} 261 262static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { 263 // GCC ignores the nonnull attribute on K&R style function prototypes, so we 264 // ignore it as well 265 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { 266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 267 << Attr.getName() << 0 /*function*/; 268 return; 269 } 270 271 unsigned NumArgs = getFunctionOrMethodNumArgs(d); 272 273 // The nonnull attribute only applies to pointers. 274 llvm::SmallVector<unsigned, 10> NonNullArgs; 275 276 for (AttributeList::arg_iterator I=Attr.arg_begin(), 277 E=Attr.arg_end(); I!=E; ++I) { 278 279 280 // The argument must be an integer constant expression. 281 Expr *Ex = static_cast<Expr *>(*I); 282 llvm::APSInt ArgNum(32); 283 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) { 284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 285 << "nonnull" << Ex->getSourceRange(); 286 return; 287 } 288 289 unsigned x = (unsigned) ArgNum.getZExtValue(); 290 291 if (x < 1 || x > NumArgs) { 292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 293 << "nonnull" << I.getArgNum() << Ex->getSourceRange(); 294 return; 295 } 296 297 --x; 298 299 // Is the function argument a pointer type? 300 QualType T = getFunctionOrMethodArgType(d, x); 301 if (!T->isAnyPointerType() && !T->isBlockPointerType()) { 302 // FIXME: Should also highlight argument in decl. 303 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only) 304 << "nonnull" << Ex->getSourceRange(); 305 continue; 306 } 307 308 NonNullArgs.push_back(x); 309 } 310 311 // If no arguments were specified to __attribute__((nonnull)) then all pointer 312 // arguments have a nonnull attribute. 313 if (NonNullArgs.empty()) { 314 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { 315 QualType T = getFunctionOrMethodArgType(d, I); 316 if (T->isAnyPointerType() || T->isBlockPointerType()) 317 NonNullArgs.push_back(I); 318 } 319 320 if (NonNullArgs.empty()) { 321 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); 322 return; 323 } 324 } 325 326 unsigned* start = &NonNullArgs[0]; 327 unsigned size = NonNullArgs.size(); 328 std::sort(start, start + size); 329 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size)); 330} 331 332static bool isStaticVarOrStaticFunciton(Decl *D) { 333 if (VarDecl *VD = dyn_cast<VarDecl>(D)) 334 return VD->getStorageClass() == VarDecl::Static; 335 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 336 return FD->getStorageClass() == FunctionDecl::Static; 337 return false; 338} 339 340static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) { 341 // Check the attribute arguments. 342 if (Attr.getNumArgs() > 1) { 343 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 344 return; 345 } 346 347 // gcc rejects 348 // class c { 349 // static int a __attribute__((weakref ("v2"))); 350 // static int b() __attribute__((weakref ("f3"))); 351 // }; 352 // and ignores the attributes of 353 // void f(void) { 354 // static int a __attribute__((weakref ("v2"))); 355 // } 356 // we reject them 357 if (const DeclContext *Ctx = d->getDeclContext()) { 358 Ctx = Ctx->getLookupContext(); 359 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) { 360 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << 361 dyn_cast<NamedDecl>(d)->getNameAsString(); 362 return; 363 } 364 } 365 366 // The GCC manual says 367 // 368 // At present, a declaration to which `weakref' is attached can only 369 // be `static'. 370 // 371 // It also says 372 // 373 // Without a TARGET, 374 // given as an argument to `weakref' or to `alias', `weakref' is 375 // equivalent to `weak'. 376 // 377 // gcc 4.4.1 will accept 378 // int a7 __attribute__((weakref)); 379 // as 380 // int a7 __attribute__((weak)); 381 // This looks like a bug in gcc. We reject that for now. We should revisit 382 // it if this behaviour is actually used. 383 384 if (!isStaticVarOrStaticFunciton(d)) { 385 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) << 386 dyn_cast<NamedDecl>(d)->getNameAsString(); 387 return; 388 } 389 390 // GCC rejects 391 // static ((alias ("y"), weakref)). 392 // Should we? How to check that weakref is before or after alias? 393 394 if (Attr.getNumArgs() == 1) { 395 Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); 396 Arg = Arg->IgnoreParenCasts(); 397 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 398 399 if (Str == 0 || Str->isWide()) { 400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 401 << "weakref" << 1; 402 return; 403 } 404 // GCC will accept anything as the argument of weakref. Should we 405 // check for an existing decl? 406 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString())); 407 } 408 409 d->addAttr(::new (S.Context) WeakRefAttr()); 410} 411 412static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { 413 // check the attribute arguments. 414 if (Attr.getNumArgs() != 1) { 415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 416 return; 417 } 418 419 Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); 420 Arg = Arg->IgnoreParenCasts(); 421 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 422 423 if (Str == 0 || Str->isWide()) { 424 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 425 << "alias" << 1; 426 return; 427 } 428 429 // FIXME: check if target symbol exists in current file 430 431 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString())); 432} 433 434static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, 435 Sema &S) { 436 // check the attribute arguments. 437 if (Attr.getNumArgs() != 0) { 438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 439 return; 440 } 441 442 if (!isa<FunctionDecl>(d)) { 443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 444 << Attr.getName() << 0 /*function*/; 445 return; 446 } 447 448 d->addAttr(::new (S.Context) AlwaysInlineAttr()); 449} 450 451static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { 452 // check the attribute arguments. 453 if (Attr.getNumArgs() != 0) { 454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 455 return; 456 } 457 458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { 459 QualType RetTy = FD->getResultType(); 460 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { 461 d->addAttr(::new (S.Context) MallocAttr()); 462 return; 463 } 464 } 465 466 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); 467} 468 469static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr, 470 Sema &S, bool EmitDiagnostics) { 471 // check the attribute arguments. 472 if (Attr.getNumArgs() != 0) { 473 if (EmitDiagnostics) 474 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 475 return false; 476 } 477 478 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { 479 ValueDecl *VD = dyn_cast<ValueDecl>(d); 480 if (VD == 0 || (!VD->getType()->isBlockPointerType() 481 && !VD->getType()->isFunctionPointerType())) { 482 if (EmitDiagnostics) 483 S.Diag(Attr.getLoc(), 484 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 485 : diag::warn_attribute_wrong_decl_type) 486 << Attr.getName() << 0 /*function*/; 487 return false; 488 } 489 } 490 491 return true; 492} 493 494static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { 495 /* 496 Do check for well-formedness, but do not emit diagnostics: 497 it was already emitted by Sema::ProcessFnAttr(). 498 */ 499 if (HandleCommonNoReturnAttr(d, Attr, S, /*EmitDiagnostic=*/false)) 500 d->addAttr(::new (S.Context) NoReturnAttr()); 501} 502 503static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, 504 Sema &S) { 505 if (HandleCommonNoReturnAttr(d, Attr, S, true)) 506 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr()); 507} 508 509static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) { 510 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) { 511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 512 << Attr.getName() << 8 /*function, method, or parameter*/; 513 return; 514 } 515 // FIXME: Actually store the attribute on the declaration 516} 517 518static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 519 // check the attribute arguments. 520 if (Attr.getNumArgs() != 0) { 521 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 522 return; 523 } 524 525 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) && 526 !isa<TypeDecl>(d)) { 527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 528 << Attr.getName() << 2 /*variable and function*/; 529 return; 530 } 531 532 d->addAttr(::new (S.Context) UnusedAttr()); 533} 534 535static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 536 // check the attribute arguments. 537 if (Attr.getNumArgs() != 0) { 538 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 539 return; 540 } 541 542 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { 543 if (VD->hasLocalStorage() || VD->hasExternalStorage()) { 544 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; 545 return; 546 } 547 } else if (!isFunctionOrMethod(d)) { 548 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 549 << Attr.getName() << 2 /*variable and function*/; 550 return; 551 } 552 553 d->addAttr(::new (S.Context) UsedAttr()); 554} 555 556static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { 557 // check the attribute arguments. 558 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { 559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 560 << "0 or 1"; 561 return; 562 } 563 564 int priority = 65535; // FIXME: Do not hardcode such constants. 565 if (Attr.getNumArgs() > 0) { 566 Expr *E = static_cast<Expr *>(Attr.getArg(0)); 567 llvm::APSInt Idx(32); 568 if (!E->isIntegerConstantExpr(Idx, S.Context)) { 569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 570 << "constructor" << 1 << E->getSourceRange(); 571 return; 572 } 573 priority = Idx.getZExtValue(); 574 } 575 576 if (!isa<FunctionDecl>(d)) { 577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 578 << Attr.getName() << 0 /*function*/; 579 return; 580 } 581 582 d->addAttr(::new (S.Context) ConstructorAttr(priority)); 583} 584 585static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { 586 // check the attribute arguments. 587 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { 588 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 589 << "0 or 1"; 590 return; 591 } 592 593 int priority = 65535; // FIXME: Do not hardcode such constants. 594 if (Attr.getNumArgs() > 0) { 595 Expr *E = static_cast<Expr *>(Attr.getArg(0)); 596 llvm::APSInt Idx(32); 597 if (!E->isIntegerConstantExpr(Idx, S.Context)) { 598 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 599 << "destructor" << 1 << E->getSourceRange(); 600 return; 601 } 602 priority = Idx.getZExtValue(); 603 } 604 605 if (!isa<FunctionDecl>(d)) { 606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 607 << Attr.getName() << 0 /*function*/; 608 return; 609 } 610 611 d->addAttr(::new (S.Context) DestructorAttr(priority)); 612} 613 614static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 615 // check the attribute arguments. 616 if (Attr.getNumArgs() != 0) { 617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 618 return; 619 } 620 621 d->addAttr(::new (S.Context) DeprecatedAttr()); 622} 623 624static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { 625 // check the attribute arguments. 626 if (Attr.getNumArgs() != 0) { 627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 628 return; 629 } 630 631 d->addAttr(::new (S.Context) UnavailableAttr()); 632} 633 634static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { 635 // check the attribute arguments. 636 if (Attr.getNumArgs() != 1) { 637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 638 return; 639 } 640 641 Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); 642 Arg = Arg->IgnoreParenCasts(); 643 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 644 645 if (Str == 0 || Str->isWide()) { 646 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 647 << "visibility" << 1; 648 return; 649 } 650 651 llvm::StringRef TypeStr = Str->getString(); 652 VisibilityAttr::VisibilityTypes type; 653 654 if (TypeStr == "default") 655 type = VisibilityAttr::DefaultVisibility; 656 else if (TypeStr == "hidden") 657 type = VisibilityAttr::HiddenVisibility; 658 else if (TypeStr == "internal") 659 type = VisibilityAttr::HiddenVisibility; // FIXME 660 else if (TypeStr == "protected") 661 type = VisibilityAttr::ProtectedVisibility; 662 else { 663 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; 664 return; 665 } 666 667 d->addAttr(::new (S.Context) VisibilityAttr(type)); 668} 669 670static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, 671 Sema &S) { 672 if (Attr.getNumArgs() != 0) { 673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 674 return; 675 } 676 677 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); 678 if (OCI == 0) { 679 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); 680 return; 681 } 682 683 D->addAttr(::new (S.Context) ObjCExceptionAttr()); 684} 685 686static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { 687 if (Attr.getNumArgs() != 0) { 688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 689 return; 690 } 691 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { 692 QualType T = TD->getUnderlyingType(); 693 if (!T->isPointerType() || 694 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { 695 S.Diag(TD->getLocation(), diag::err_nsobject_attribute); 696 return; 697 } 698 } 699 D->addAttr(::new (S.Context) ObjCNSObjectAttr()); 700} 701 702static void 703HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { 704 if (Attr.getNumArgs() != 0) { 705 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 706 return; 707 } 708 709 if (!isa<FunctionDecl>(D)) { 710 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); 711 return; 712 } 713 714 D->addAttr(::new (S.Context) OverloadableAttr()); 715} 716 717static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { 718 if (!Attr.getParameterName()) { 719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 720 << "blocks" << 1; 721 return; 722 } 723 724 if (Attr.getNumArgs() != 0) { 725 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 726 return; 727 } 728 729 BlocksAttr::BlocksAttrTypes type; 730 if (Attr.getParameterName()->isStr("byref")) 731 type = BlocksAttr::ByRef; 732 else { 733 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 734 << "blocks" << Attr.getParameterName(); 735 return; 736 } 737 738 d->addAttr(::new (S.Context) BlocksAttr(type)); 739} 740 741static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { 742 // check the attribute arguments. 743 if (Attr.getNumArgs() > 2) { 744 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 745 << "0, 1 or 2"; 746 return; 747 } 748 749 int sentinel = 0; 750 if (Attr.getNumArgs() > 0) { 751 Expr *E = static_cast<Expr *>(Attr.getArg(0)); 752 llvm::APSInt Idx(32); 753 if (!E->isIntegerConstantExpr(Idx, S.Context)) { 754 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 755 << "sentinel" << 1 << E->getSourceRange(); 756 return; 757 } 758 sentinel = Idx.getZExtValue(); 759 760 if (sentinel < 0) { 761 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) 762 << E->getSourceRange(); 763 return; 764 } 765 } 766 767 int nullPos = 0; 768 if (Attr.getNumArgs() > 1) { 769 Expr *E = static_cast<Expr *>(Attr.getArg(1)); 770 llvm::APSInt Idx(32); 771 if (!E->isIntegerConstantExpr(Idx, S.Context)) { 772 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 773 << "sentinel" << 2 << E->getSourceRange(); 774 return; 775 } 776 nullPos = Idx.getZExtValue(); 777 778 if (nullPos > 1 || nullPos < 0) { 779 // FIXME: This error message could be improved, it would be nice 780 // to say what the bounds actually are. 781 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) 782 << E->getSourceRange(); 783 return; 784 } 785 } 786 787 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { 788 const FunctionType *FT = FD->getType()->getAs<FunctionType>(); 789 assert(FT && "FunctionDecl has non-function type?"); 790 791 if (isa<FunctionNoProtoType>(FT)) { 792 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); 793 return; 794 } 795 796 if (!cast<FunctionProtoType>(FT)->isVariadic()) { 797 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; 798 return; 799 } 800 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { 801 if (!MD->isVariadic()) { 802 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; 803 return; 804 } 805 } else if (isa<BlockDecl>(d)) { 806 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the 807 // caller. 808 ; 809 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { 810 QualType Ty = V->getType(); 811 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { 812 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) 813 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); 814 if (!cast<FunctionProtoType>(FT)->isVariadic()) { 815 int m = Ty->isFunctionPointerType() ? 0 : 1; 816 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; 817 return; 818 } 819 } else { 820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 821 << Attr.getName() << 6 /*function, method or block */; 822 return; 823 } 824 } else { 825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 826 << Attr.getName() << 6 /*function, method or block */; 827 return; 828 } 829 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos)); 830} 831 832static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { 833 // check the attribute arguments. 834 if (Attr.getNumArgs() != 0) { 835 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 836 return; 837 } 838 839 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { 840 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 841 << Attr.getName() << 0 /*function*/; 842 return; 843 } 844 845 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { 846 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) 847 << Attr.getName() << 0; 848 return; 849 } 850 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 851 if (MD->getResultType()->isVoidType()) { 852 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) 853 << Attr.getName() << 1; 854 return; 855 } 856 857 D->addAttr(::new (S.Context) WarnUnusedResultAttr()); 858} 859 860static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) { 861 // check the attribute arguments. 862 if (Attr.getNumArgs() != 0) { 863 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 864 return; 865 } 866 867 /* weak only applies to non-static declarations */ 868 if (isStaticVarOrStaticFunciton(D)) { 869 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) << 870 dyn_cast<NamedDecl>(D)->getNameAsString(); 871 return; 872 } 873 874 // TODO: could also be applied to methods? 875 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { 876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 877 << Attr.getName() << 2 /*variable and function*/; 878 return; 879 } 880 881 D->addAttr(::new (S.Context) WeakAttr()); 882} 883 884static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { 885 // check the attribute arguments. 886 if (Attr.getNumArgs() != 0) { 887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 888 return; 889 } 890 891 // weak_import only applies to variable & function declarations. 892 bool isDef = false; 893 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 894 isDef = (!VD->hasExternalStorage() || VD->getInit()); 895 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 896 isDef = FD->getBody(); 897 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) { 898 // We ignore weak import on properties and methods 899 return; 900 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) { 901 // Don't issue the warning for darwin as target; yet, ignore the attribute. 902 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin || 903 !isa<ObjCInterfaceDecl>(D)) 904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 905 << Attr.getName() << 2 /*variable and function*/; 906 return; 907 } 908 909 // Merge should handle any subsequent violations. 910 if (isDef) { 911 S.Diag(Attr.getLoc(), 912 diag::warn_attribute_weak_import_invalid_on_definition) 913 << "weak_import" << 2 /*variable and function*/; 914 return; 915 } 916 917 D->addAttr(::new (S.Context) WeakImportAttr()); 918} 919 920static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, 921 Sema &S) { 922 // Attribute has 3 arguments. 923 if (Attr.getNumArgs() != 3) { 924 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 925 return; 926 } 927 928 unsigned WGSize[3]; 929 for (unsigned i = 0; i < 3; ++i) { 930 Expr *E = static_cast<Expr *>(Attr.getArg(i)); 931 llvm::APSInt ArgNum(32); 932 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) { 933 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 934 << "reqd_work_group_size" << E->getSourceRange(); 935 return; 936 } 937 WGSize[i] = (unsigned) ArgNum.getZExtValue(); 938 } 939 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1], 940 WGSize[2])); 941} 942 943static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { 944 // Attribute has no arguments. 945 if (Attr.getNumArgs() != 1) { 946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 947 return; 948 } 949 950 // Make sure that there is a string literal as the sections's single 951 // argument. 952 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0)); 953 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); 954 if (!SE) { 955 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; 956 return; 957 } 958 959 // If the target wants to validate the section specifier, make it happen. 960 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); 961 if (!Error.empty()) { 962 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) 963 << Error; 964 return; 965 } 966 967 // This attribute cannot be applied to local variables. 968 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { 969 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); 970 return; 971 } 972 973 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString())); 974} 975 976 977static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { 978 // check the attribute arguments. 979 if (Attr.getNumArgs() != 0) { 980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 981 return; 982 } 983 984 d->addAttr(::new (S.Context) NoThrowAttr()); 985} 986 987static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { 988 // check the attribute arguments. 989 if (Attr.getNumArgs() != 0) { 990 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 991 return; 992 } 993 994 d->addAttr(::new (S.Context) ConstAttr()); 995} 996 997static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { 998 // check the attribute arguments. 999 if (Attr.getNumArgs() != 0) { 1000 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1001 return; 1002 } 1003 1004 d->addAttr(::new (S.Context) PureAttr()); 1005} 1006 1007static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1008 if (!Attr.getParameterName()) { 1009 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1010 return; 1011 } 1012 1013 if (Attr.getNumArgs() != 0) { 1014 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1015 return; 1016 } 1017 1018 VarDecl *VD = dyn_cast<VarDecl>(d); 1019 1020 if (!VD || !VD->hasLocalStorage()) { 1021 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; 1022 return; 1023 } 1024 1025 // Look up the function 1026 // FIXME: Lookup probably isn't looking in the right place 1027 // FIXME: The lookup source location should be in the attribute, not the 1028 // start of the attribute. 1029 NamedDecl *CleanupDecl 1030 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(), 1031 Sema::LookupOrdinaryName); 1032 if (!CleanupDecl) { 1033 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) << 1034 Attr.getParameterName(); 1035 return; 1036 } 1037 1038 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); 1039 if (!FD) { 1040 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) << 1041 Attr.getParameterName(); 1042 return; 1043 } 1044 1045 if (FD->getNumParams() != 1) { 1046 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) << 1047 Attr.getParameterName(); 1048 return; 1049 } 1050 1051 // We're currently more strict than GCC about what function types we accept. 1052 // If this ever proves to be a problem it should be easy to fix. 1053 QualType Ty = S.Context.getPointerType(VD->getType()); 1054 QualType ParamTy = FD->getParamDecl(0)->getType(); 1055 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) { 1056 S.Diag(Attr.getLoc(), 1057 diag::err_attribute_cleanup_func_arg_incompatible_type) << 1058 Attr.getParameterName() << ParamTy << Ty; 1059 return; 1060 } 1061 1062 d->addAttr(::new (S.Context) CleanupAttr(FD)); 1063} 1064 1065/// Handle __attribute__((format_arg((idx)))) attribute based on 1066/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 1067static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1068 if (Attr.getNumArgs() != 1) { 1069 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1070 return; 1071 } 1072 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { 1073 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1074 << Attr.getName() << 0 /*function*/; 1075 return; 1076 } 1077 // FIXME: in C++ the implicit 'this' function parameter also counts. this is 1078 // needed in order to be compatible with GCC the index must start with 1. 1079 unsigned NumArgs = getFunctionOrMethodNumArgs(d); 1080 unsigned FirstIdx = 1; 1081 // checks for the 2nd argument 1082 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); 1083 llvm::APSInt Idx(32); 1084 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { 1085 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1086 << "format" << 2 << IdxExpr->getSourceRange(); 1087 return; 1088 } 1089 1090 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { 1091 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 1092 << "format" << 2 << IdxExpr->getSourceRange(); 1093 return; 1094 } 1095 1096 unsigned ArgIdx = Idx.getZExtValue() - 1; 1097 1098 // make sure the format string is really a string 1099 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); 1100 1101 bool not_nsstring_type = !isNSStringType(Ty, S.Context); 1102 if (not_nsstring_type && 1103 !isCFStringType(Ty, S.Context) && 1104 (!Ty->isPointerType() || 1105 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { 1106 // FIXME: Should highlight the actual expression that has the wrong type. 1107 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1108 << (not_nsstring_type ? "a string type" : "an NSString") 1109 << IdxExpr->getSourceRange(); 1110 return; 1111 } 1112 Ty = getFunctionOrMethodResultType(d); 1113 if (!isNSStringType(Ty, S.Context) && 1114 !isCFStringType(Ty, S.Context) && 1115 (!Ty->isPointerType() || 1116 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { 1117 // FIXME: Should highlight the actual expression that has the wrong type. 1118 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) 1119 << (not_nsstring_type ? "string type" : "NSString") 1120 << IdxExpr->getSourceRange(); 1121 return; 1122 } 1123 1124 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue())); 1125} 1126 1127enum FormatAttrKind { 1128 CFStringFormat, 1129 NSStringFormat, 1130 StrftimeFormat, 1131 SupportedFormat, 1132 IgnoredFormat, 1133 InvalidFormat 1134}; 1135 1136/// getFormatAttrKind - Map from format attribute names to supported format 1137/// types. 1138static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { 1139 // Check for formats that get handled specially. 1140 if (Format == "NSString") 1141 return NSStringFormat; 1142 if (Format == "CFString") 1143 return CFStringFormat; 1144 if (Format == "strftime") 1145 return StrftimeFormat; 1146 1147 // Otherwise, check for supported formats. 1148 if (Format == "scanf" || Format == "printf" || Format == "printf0" || 1149 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || 1150 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || 1151 Format == "zcmn_err") 1152 return SupportedFormat; 1153 1154 if (Format == "gcc_diag" || Format == "gcc_cdiag" || 1155 Format == "gcc_cxxdiag" || Format == "gcc_tdiag") 1156 return IgnoredFormat; 1157 1158 return InvalidFormat; 1159} 1160 1161/// Handle __attribute__((format(type,idx,firstarg))) attributes based on 1162/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 1163static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1164 1165 if (!Attr.getParameterName()) { 1166 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1167 << "format" << 1; 1168 return; 1169 } 1170 1171 if (Attr.getNumArgs() != 2) { 1172 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; 1173 return; 1174 } 1175 1176 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { 1177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1178 << Attr.getName() << 0 /*function*/; 1179 return; 1180 } 1181 1182 unsigned NumArgs = getFunctionOrMethodNumArgs(d); 1183 unsigned FirstIdx = 1; 1184 1185 llvm::StringRef Format = Attr.getParameterName()->getName(); 1186 1187 // Normalize the argument, __foo__ becomes foo. 1188 if (Format.startswith("__") && Format.endswith("__")) 1189 Format = Format.substr(2, Format.size() - 4); 1190 1191 // Check for supported formats. 1192 FormatAttrKind Kind = getFormatAttrKind(Format); 1193 1194 if (Kind == IgnoredFormat) 1195 return; 1196 1197 if (Kind == InvalidFormat) { 1198 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1199 << "format" << Attr.getParameterName()->getName(); 1200 return; 1201 } 1202 1203 // checks for the 2nd argument 1204 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); 1205 llvm::APSInt Idx(32); 1206 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { 1207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1208 << "format" << 2 << IdxExpr->getSourceRange(); 1209 return; 1210 } 1211 1212 // FIXME: We should handle the implicit 'this' parameter in a more generic 1213 // way that can be used for other arguments. 1214 bool HasImplicitThisParam = false; 1215 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) { 1216 if (MD->isInstance()) { 1217 HasImplicitThisParam = true; 1218 NumArgs++; 1219 } 1220 } 1221 1222 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { 1223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 1224 << "format" << 2 << IdxExpr->getSourceRange(); 1225 return; 1226 } 1227 1228 // FIXME: Do we need to bounds check? 1229 unsigned ArgIdx = Idx.getZExtValue() - 1; 1230 1231 if (HasImplicitThisParam) { 1232 if (ArgIdx == 0) { 1233 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1234 << "a string type" << IdxExpr->getSourceRange(); 1235 return; 1236 } 1237 ArgIdx--; 1238 } 1239 1240 // make sure the format string is really a string 1241 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); 1242 1243 if (Kind == CFStringFormat) { 1244 if (!isCFStringType(Ty, S.Context)) { 1245 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1246 << "a CFString" << IdxExpr->getSourceRange(); 1247 return; 1248 } 1249 } else if (Kind == NSStringFormat) { 1250 // FIXME: do we need to check if the type is NSString*? What are the 1251 // semantics? 1252 if (!isNSStringType(Ty, S.Context)) { 1253 // FIXME: Should highlight the actual expression that has the wrong type. 1254 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1255 << "an NSString" << IdxExpr->getSourceRange(); 1256 return; 1257 } 1258 } else if (!Ty->isPointerType() || 1259 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { 1260 // FIXME: Should highlight the actual expression that has the wrong type. 1261 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1262 << "a string type" << IdxExpr->getSourceRange(); 1263 return; 1264 } 1265 1266 // check the 3rd argument 1267 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); 1268 llvm::APSInt FirstArg(32); 1269 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { 1270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1271 << "format" << 3 << FirstArgExpr->getSourceRange(); 1272 return; 1273 } 1274 1275 // check if the function is variadic if the 3rd argument non-zero 1276 if (FirstArg != 0) { 1277 if (isFunctionOrMethodVariadic(d)) { 1278 ++NumArgs; // +1 for ... 1279 } else { 1280 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); 1281 return; 1282 } 1283 } 1284 1285 // strftime requires FirstArg to be 0 because it doesn't read from any 1286 // variable the input is just the current time + the format string. 1287 if (Kind == StrftimeFormat) { 1288 if (FirstArg != 0) { 1289 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) 1290 << FirstArgExpr->getSourceRange(); 1291 return; 1292 } 1293 // if 0 it disables parameter checking (to use with e.g. va_list) 1294 } else if (FirstArg != 0 && FirstArg != NumArgs) { 1295 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 1296 << "format" << 3 << FirstArgExpr->getSourceRange(); 1297 return; 1298 } 1299 1300 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(), 1301 FirstArg.getZExtValue())); 1302} 1303 1304static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, 1305 Sema &S) { 1306 // check the attribute arguments. 1307 if (Attr.getNumArgs() != 0) { 1308 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1309 return; 1310 } 1311 1312 // Try to find the underlying union declaration. 1313 RecordDecl *RD = 0; 1314 TypedefDecl *TD = dyn_cast<TypedefDecl>(d); 1315 if (TD && TD->getUnderlyingType()->isUnionType()) 1316 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); 1317 else 1318 RD = dyn_cast<RecordDecl>(d); 1319 1320 if (!RD || !RD->isUnion()) { 1321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1322 << Attr.getName() << 1 /*union*/; 1323 return; 1324 } 1325 1326 if (!RD->isDefinition()) { 1327 S.Diag(Attr.getLoc(), 1328 diag::warn_transparent_union_attribute_not_definition); 1329 return; 1330 } 1331 1332 RecordDecl::field_iterator Field = RD->field_begin(), 1333 FieldEnd = RD->field_end(); 1334 if (Field == FieldEnd) { 1335 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); 1336 return; 1337 } 1338 1339 FieldDecl *FirstField = *Field; 1340 QualType FirstType = FirstField->getType(); 1341 if (FirstType->isFloatingType() || FirstType->isVectorType()) { 1342 S.Diag(FirstField->getLocation(), 1343 diag::warn_transparent_union_attribute_floating); 1344 return; 1345 } 1346 1347 uint64_t FirstSize = S.Context.getTypeSize(FirstType); 1348 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); 1349 for (; Field != FieldEnd; ++Field) { 1350 QualType FieldType = Field->getType(); 1351 if (S.Context.getTypeSize(FieldType) != FirstSize || 1352 S.Context.getTypeAlign(FieldType) != FirstAlign) { 1353 // Warn if we drop the attribute. 1354 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; 1355 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) 1356 : S.Context.getTypeAlign(FieldType); 1357 S.Diag(Field->getLocation(), 1358 diag::warn_transparent_union_attribute_field_size_align) 1359 << isSize << Field->getDeclName() << FieldBits; 1360 unsigned FirstBits = isSize? FirstSize : FirstAlign; 1361 S.Diag(FirstField->getLocation(), 1362 diag::note_transparent_union_first_field_size_align) 1363 << isSize << FirstBits; 1364 return; 1365 } 1366 } 1367 1368 RD->addAttr(::new (S.Context) TransparentUnionAttr()); 1369} 1370 1371static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1372 // check the attribute arguments. 1373 if (Attr.getNumArgs() != 1) { 1374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1375 return; 1376 } 1377 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0)); 1378 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); 1379 1380 // Make sure that there is a string literal as the annotation's single 1381 // argument. 1382 if (!SE) { 1383 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; 1384 return; 1385 } 1386 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString())); 1387} 1388 1389static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1390 // check the attribute arguments. 1391 if (Attr.getNumArgs() > 1) { 1392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1393 return; 1394 } 1395 1396 //FIXME: The C++0x version of this attribute has more limited applicabilty 1397 // than GNU's, and should error out when it is used to specify a 1398 // weaker alignment, rather than being silently ignored. 1399 1400 unsigned Align = 0; 1401 if (Attr.getNumArgs() == 0) { 1402 // FIXME: This should be the target specific maximum alignment. 1403 // (For now we just use 128 bits which is the maximum on X86). 1404 Align = 128; 1405 d->addAttr(::new (S.Context) AlignedAttr(Align)); 1406 return; 1407 } 1408 1409 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); 1410 llvm::APSInt Alignment(32); 1411 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { 1412 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 1413 << "aligned" << alignmentExpr->getSourceRange(); 1414 return; 1415 } 1416 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { 1417 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two) 1418 << alignmentExpr->getSourceRange(); 1419 return; 1420 } 1421 1422 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8)); 1423} 1424 1425/// HandleModeAttr - This attribute modifies the width of a decl with primitive 1426/// type. 1427/// 1428/// Despite what would be logical, the mode attribute is a decl attribute, not a 1429/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be 1430/// HImode, not an intermediate pointer. 1431static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { 1432 // This attribute isn't documented, but glibc uses it. It changes 1433 // the width of an int or unsigned int to the specified size. 1434 1435 // Check that there aren't any arguments 1436 if (Attr.getNumArgs() != 0) { 1437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1438 return; 1439 } 1440 1441 IdentifierInfo *Name = Attr.getParameterName(); 1442 if (!Name) { 1443 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); 1444 return; 1445 } 1446 1447 llvm::StringRef Str = Attr.getParameterName()->getName(); 1448 1449 // Normalize the attribute name, __foo__ becomes foo. 1450 if (Str.startswith("__") && Str.endswith("__")) 1451 Str = Str.substr(2, Str.size() - 4); 1452 1453 unsigned DestWidth = 0; 1454 bool IntegerMode = true; 1455 bool ComplexMode = false; 1456 switch (Str.size()) { 1457 case 2: 1458 switch (Str[0]) { 1459 case 'Q': DestWidth = 8; break; 1460 case 'H': DestWidth = 16; break; 1461 case 'S': DestWidth = 32; break; 1462 case 'D': DestWidth = 64; break; 1463 case 'X': DestWidth = 96; break; 1464 case 'T': DestWidth = 128; break; 1465 } 1466 if (Str[1] == 'F') { 1467 IntegerMode = false; 1468 } else if (Str[1] == 'C') { 1469 IntegerMode = false; 1470 ComplexMode = true; 1471 } else if (Str[1] != 'I') { 1472 DestWidth = 0; 1473 } 1474 break; 1475 case 4: 1476 // FIXME: glibc uses 'word' to define register_t; this is narrower than a 1477 // pointer on PIC16 and other embedded platforms. 1478 if (Str == "word") 1479 DestWidth = S.Context.Target.getPointerWidth(0); 1480 else if (Str == "byte") 1481 DestWidth = S.Context.Target.getCharWidth(); 1482 break; 1483 case 7: 1484 if (Str == "pointer") 1485 DestWidth = S.Context.Target.getPointerWidth(0); 1486 break; 1487 } 1488 1489 QualType OldTy; 1490 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) 1491 OldTy = TD->getUnderlyingType(); 1492 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) 1493 OldTy = VD->getType(); 1494 else { 1495 S.Diag(D->getLocation(), diag::err_attr_wrong_decl) 1496 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); 1497 return; 1498 } 1499 1500 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) 1501 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); 1502 else if (IntegerMode) { 1503 if (!OldTy->isIntegralType()) 1504 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 1505 } else if (ComplexMode) { 1506 if (!OldTy->isComplexType()) 1507 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 1508 } else { 1509 if (!OldTy->isFloatingType()) 1510 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 1511 } 1512 1513 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t 1514 // and friends, at least with glibc. 1515 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong 1516 // width on unusual platforms. 1517 // FIXME: Make sure floating-point mappings are accurate 1518 // FIXME: Support XF and TF types 1519 QualType NewTy; 1520 switch (DestWidth) { 1521 case 0: 1522 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; 1523 return; 1524 default: 1525 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 1526 return; 1527 case 8: 1528 if (!IntegerMode) { 1529 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 1530 return; 1531 } 1532 if (OldTy->isSignedIntegerType()) 1533 NewTy = S.Context.SignedCharTy; 1534 else 1535 NewTy = S.Context.UnsignedCharTy; 1536 break; 1537 case 16: 1538 if (!IntegerMode) { 1539 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 1540 return; 1541 } 1542 if (OldTy->isSignedIntegerType()) 1543 NewTy = S.Context.ShortTy; 1544 else 1545 NewTy = S.Context.UnsignedShortTy; 1546 break; 1547 case 32: 1548 if (!IntegerMode) 1549 NewTy = S.Context.FloatTy; 1550 else if (OldTy->isSignedIntegerType()) 1551 NewTy = S.Context.IntTy; 1552 else 1553 NewTy = S.Context.UnsignedIntTy; 1554 break; 1555 case 64: 1556 if (!IntegerMode) 1557 NewTy = S.Context.DoubleTy; 1558 else if (OldTy->isSignedIntegerType()) 1559 if (S.Context.Target.getLongWidth() == 64) 1560 NewTy = S.Context.LongTy; 1561 else 1562 NewTy = S.Context.LongLongTy; 1563 else 1564 if (S.Context.Target.getLongWidth() == 64) 1565 NewTy = S.Context.UnsignedLongTy; 1566 else 1567 NewTy = S.Context.UnsignedLongLongTy; 1568 break; 1569 case 96: 1570 NewTy = S.Context.LongDoubleTy; 1571 break; 1572 case 128: 1573 if (!IntegerMode) { 1574 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 1575 return; 1576 } 1577 if (OldTy->isSignedIntegerType()) 1578 NewTy = S.Context.Int128Ty; 1579 else 1580 NewTy = S.Context.UnsignedInt128Ty; 1581 break; 1582 } 1583 1584 if (ComplexMode) { 1585 NewTy = S.Context.getComplexType(NewTy); 1586 } 1587 1588 // Install the new type. 1589 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { 1590 // FIXME: preserve existing source info. 1591 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); 1592 } else 1593 cast<ValueDecl>(D)->setType(NewTy); 1594} 1595 1596static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1597 // check the attribute arguments. 1598 if (Attr.getNumArgs() > 0) { 1599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1600 return; 1601 } 1602 1603 if (!isFunctionOrMethod(d)) { 1604 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1605 << Attr.getName() << 0 /*function*/; 1606 return; 1607 } 1608 1609 d->addAttr(::new (S.Context) NoDebugAttr()); 1610} 1611 1612static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1613 // check the attribute arguments. 1614 if (Attr.getNumArgs() != 0) { 1615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1616 return; 1617 } 1618 1619 if (!isa<FunctionDecl>(d)) { 1620 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1621 << Attr.getName() << 0 /*function*/; 1622 return; 1623 } 1624 1625 d->addAttr(::new (S.Context) NoInlineAttr()); 1626} 1627 1628static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1629 // check the attribute arguments. 1630 if (Attr.getNumArgs() != 0) { 1631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1632 return; 1633 } 1634 1635 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); 1636 if (Fn == 0) { 1637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1638 << Attr.getName() << 0 /*function*/; 1639 return; 1640 } 1641 1642 if (!Fn->isInlineSpecified()) { 1643 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); 1644 return; 1645 } 1646 1647 d->addAttr(::new (S.Context) GNUInlineAttr()); 1648} 1649 1650static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1651 // check the attribute arguments. 1652 if (Attr.getNumArgs() != 1) { 1653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1654 return; 1655 } 1656 1657 if (!isFunctionOrMethod(d)) { 1658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1659 << Attr.getName() << 0 /*function*/; 1660 return; 1661 } 1662 1663 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); 1664 llvm::APSInt NumParams(32); 1665 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { 1666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 1667 << "regparm" << NumParamsExpr->getSourceRange(); 1668 return; 1669 } 1670 1671 if (S.Context.Target.getRegParmMax() == 0) { 1672 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) 1673 << NumParamsExpr->getSourceRange(); 1674 return; 1675 } 1676 1677 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) { 1678 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) 1679 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); 1680 return; 1681 } 1682 1683 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue())); 1684} 1685 1686static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1687 // check the attribute arguments. 1688 if (Attr.getNumArgs() != 0) { 1689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1690 return; 1691 } 1692 1693 if (!isa<CXXRecordDecl>(d) 1694 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) { 1695 S.Diag(Attr.getLoc(), 1696 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 1697 : diag::warn_attribute_wrong_decl_type) 1698 << Attr.getName() << 7 /*virtual method or class*/; 1699 return; 1700 } 1701 1702 // FIXME: Conform to C++0x redeclaration rules. 1703 1704 if (d->getAttr<FinalAttr>()) { 1705 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final"; 1706 return; 1707 } 1708 1709 d->addAttr(::new (S.Context) FinalAttr()); 1710} 1711 1712//===----------------------------------------------------------------------===// 1713// C++0x member checking attributes 1714//===----------------------------------------------------------------------===// 1715 1716static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1717 if (Attr.getNumArgs() != 0) { 1718 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1719 return; 1720 } 1721 1722 if (!isa<CXXRecordDecl>(d)) { 1723 S.Diag(Attr.getLoc(), 1724 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 1725 : diag::warn_attribute_wrong_decl_type) 1726 << Attr.getName() << 9 /*class*/; 1727 return; 1728 } 1729 1730 if (d->getAttr<BaseCheckAttr>()) { 1731 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check"; 1732 return; 1733 } 1734 1735 d->addAttr(::new (S.Context) BaseCheckAttr()); 1736} 1737 1738static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1739 if (Attr.getNumArgs() != 0) { 1740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1741 return; 1742 } 1743 1744 if (!isa<RecordDecl>(d->getDeclContext())) { 1745 // FIXME: It's not the type that's the problem 1746 S.Diag(Attr.getLoc(), 1747 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 1748 : diag::warn_attribute_wrong_decl_type) 1749 << Attr.getName() << 11 /*member*/; 1750 return; 1751 } 1752 1753 // FIXME: Conform to C++0x redeclaration rules. 1754 1755 if (d->getAttr<HidingAttr>()) { 1756 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding"; 1757 return; 1758 } 1759 1760 d->addAttr(::new (S.Context) HidingAttr()); 1761} 1762 1763static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1764 if (Attr.getNumArgs() != 0) { 1765 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1766 return; 1767 } 1768 1769 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) { 1770 // FIXME: It's not the type that's the problem 1771 S.Diag(Attr.getLoc(), 1772 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 1773 : diag::warn_attribute_wrong_decl_type) 1774 << Attr.getName() << 10 /*virtual method*/; 1775 return; 1776 } 1777 1778 // FIXME: Conform to C++0x redeclaration rules. 1779 1780 if (d->getAttr<OverrideAttr>()) { 1781 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override"; 1782 return; 1783 } 1784 1785 d->addAttr(::new (S.Context) OverrideAttr()); 1786} 1787 1788//===----------------------------------------------------------------------===// 1789// Checker-specific attribute handlers. 1790//===----------------------------------------------------------------------===// 1791 1792static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr, 1793 Sema &S) { 1794 1795 QualType RetTy; 1796 1797 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) 1798 RetTy = MD->getResultType(); 1799 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) 1800 RetTy = FD->getResultType(); 1801 else { 1802 SourceLocation L = Attr.getLoc(); 1803 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) 1804 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */; 1805 return; 1806 } 1807 1808 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>() 1809 || RetTy->getAs<ObjCObjectPointerType>())) { 1810 SourceLocation L = Attr.getLoc(); 1811 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) 1812 << SourceRange(L, L) << Attr.getName(); 1813 return; 1814 } 1815 1816 switch (Attr.getKind()) { 1817 default: 1818 assert(0 && "invalid ownership attribute"); 1819 return; 1820 case AttributeList::AT_cf_returns_not_retained: 1821 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr()); 1822 return; 1823 case AttributeList::AT_ns_returns_not_retained: 1824 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr()); 1825 return; 1826 case AttributeList::AT_cf_returns_retained: 1827 d->addAttr(::new (S.Context) CFReturnsRetainedAttr()); 1828 return; 1829 case AttributeList::AT_ns_returns_retained: 1830 d->addAttr(::new (S.Context) NSReturnsRetainedAttr()); 1831 return; 1832 }; 1833} 1834 1835static bool isKnownDeclSpecAttr(const AttributeList &Attr) { 1836 return Attr.getKind() == AttributeList::AT_dllimport || 1837 Attr.getKind() == AttributeList::AT_dllexport; 1838} 1839 1840//===----------------------------------------------------------------------===// 1841// Top Level Sema Entry Points 1842//===----------------------------------------------------------------------===// 1843 1844/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if 1845/// the attribute applies to decls. If the attribute is a type attribute, just 1846/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to 1847/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). 1848static void ProcessDeclAttribute(Scope *scope, Decl *D, 1849 const AttributeList &Attr, Sema &S) { 1850 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) 1851 // FIXME: Try to deal with other __declspec attributes! 1852 return; 1853 switch (Attr.getKind()) { 1854 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; 1855 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; 1856 case AttributeList::AT_address_space: 1857 case AttributeList::AT_objc_gc: 1858 case AttributeList::AT_vector_size: 1859 // Ignore these, these are type attributes, handled by 1860 // ProcessTypeAttributes. 1861 break; 1862 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; 1863 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; 1864 case AttributeList::AT_always_inline: 1865 HandleAlwaysInlineAttr (D, Attr, S); break; 1866 case AttributeList::AT_analyzer_noreturn: 1867 HandleAnalyzerNoReturnAttr (D, Attr, S); break; 1868 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; 1869 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break; 1870 case AttributeList::AT_carries_dependency: 1871 HandleDependencyAttr (D, Attr, S); break; 1872 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; 1873 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; 1874 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; 1875 case AttributeList::AT_ext_vector_type: 1876 HandleExtVectorTypeAttr(scope, D, Attr, S); 1877 break; 1878 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break; 1879 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; 1880 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; 1881 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; 1882 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break; 1883 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; 1884 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; 1885 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; 1886 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; 1887 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; 1888 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break; 1889 1890 // Checker-specific. 1891 case AttributeList::AT_ns_returns_not_retained: 1892 case AttributeList::AT_cf_returns_not_retained: 1893 case AttributeList::AT_ns_returns_retained: 1894 case AttributeList::AT_cf_returns_retained: 1895 HandleNSReturnsRetainedAttr(D, Attr, S); break; 1896 1897 case AttributeList::AT_reqd_wg_size: 1898 HandleReqdWorkGroupSize(D, Attr, S); break; 1899 1900 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; 1901 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; 1902 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; 1903 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; 1904 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; 1905 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; 1906 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); 1907 break; 1908 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; 1909 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break; 1910 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; 1911 case AttributeList::AT_transparent_union: 1912 HandleTransparentUnionAttr(D, Attr, S); 1913 break; 1914 case AttributeList::AT_objc_exception: 1915 HandleObjCExceptionAttr(D, Attr, S); 1916 break; 1917 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; 1918 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; 1919 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; 1920 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; 1921 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; 1922 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; 1923 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; 1924 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; 1925 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; 1926 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; 1927 case AttributeList::IgnoredAttribute: 1928 case AttributeList::AT_no_instrument_function: // Interacts with -pg. 1929 // Just ignore 1930 break; 1931 case AttributeList::AT_stdcall: 1932 case AttributeList::AT_cdecl: 1933 case AttributeList::AT_fastcall: 1934 // These are all treated as type attributes. 1935 break; 1936 default: 1937 // Ask target about the attribute. 1938 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); 1939 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) 1940 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 1941 break; 1942 } 1943} 1944 1945/// ProcessDeclAttributeList - Apply all the decl attributes in the specified 1946/// attribute list to the specified decl, ignoring any type attributes. 1947void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) { 1948 for (const AttributeList* l = AttrList; l; l = l->getNext()) { 1949 ProcessDeclAttribute(S, D, *l, *this); 1950 } 1951 1952 // GCC accepts 1953 // static int a9 __attribute__((weakref)); 1954 // but that looks really pointless. We reject it. 1955 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { 1956 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << 1957 dyn_cast<NamedDecl>(D)->getNameAsString(); 1958 return; 1959 } 1960} 1961 1962/// DeclClonePragmaWeak - clone existing decl (maybe definition), 1963/// #pragma weak needs a non-definition decl and source may not have one 1964NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { 1965 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); 1966 NamedDecl *NewD = 0; 1967 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 1968 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), 1969 FD->getLocation(), DeclarationName(II), 1970 FD->getType(), FD->getTypeSourceInfo()); 1971 if (FD->getQualifier()) { 1972 FunctionDecl *NewFD = cast<FunctionDecl>(NewD); 1973 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange()); 1974 } 1975 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { 1976 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), 1977 VD->getLocation(), II, 1978 VD->getType(), VD->getTypeSourceInfo(), 1979 VD->getStorageClass(), 1980 VD->getStorageClassAsWritten()); 1981 if (VD->getQualifier()) { 1982 VarDecl *NewVD = cast<VarDecl>(NewD); 1983 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange()); 1984 } 1985 } 1986 return NewD; 1987} 1988 1989/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak 1990/// applied to it, possibly with an alias. 1991void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { 1992 if (W.getUsed()) return; // only do this once 1993 W.setUsed(true); 1994 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) 1995 IdentifierInfo *NDId = ND->getIdentifier(); 1996 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); 1997 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName())); 1998 NewD->addAttr(::new (Context) WeakAttr()); 1999 WeakTopLevelDecl.push_back(NewD); 2000 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin 2001 // to insert Decl at TU scope, sorry. 2002 DeclContext *SavedContext = CurContext; 2003 CurContext = Context.getTranslationUnitDecl(); 2004 PushOnScopeChains(NewD, S); 2005 CurContext = SavedContext; 2006 } else { // just add weak to existing 2007 ND->addAttr(::new (Context) WeakAttr()); 2008 } 2009} 2010 2011/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in 2012/// it, apply them to D. This is a bit tricky because PD can have attributes 2013/// specified in many different places, and we need to find and apply them all. 2014void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { 2015 // Handle #pragma weak 2016 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 2017 if (ND->hasLinkage()) { 2018 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier()); 2019 if (W != WeakInfo()) { 2020 // Identifier referenced by #pragma weak before it was declared 2021 DeclApplyPragmaWeak(S, ND, W); 2022 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W; 2023 } 2024 } 2025 } 2026 2027 // Apply decl attributes from the DeclSpec if present. 2028 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) 2029 ProcessDeclAttributeList(S, D, Attrs); 2030 2031 // Walk the declarator structure, applying decl attributes that were in a type 2032 // position to the decl itself. This handles cases like: 2033 // int *__attr__(x)** D; 2034 // when X is a decl attribute. 2035 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) 2036 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) 2037 ProcessDeclAttributeList(S, D, Attrs); 2038 2039 // Finally, apply any attributes on the decl itself. 2040 if (const AttributeList *Attrs = PD.getAttributes()) 2041 ProcessDeclAttributeList(S, D, Attrs); 2042} 2043 2044/// PushParsingDeclaration - Enter a new "scope" of deprecation 2045/// warnings. 2046/// 2047/// The state token we use is the start index of this scope 2048/// on the warning stack. 2049Action::ParsingDeclStackState Sema::PushParsingDeclaration() { 2050 ParsingDeclDepth++; 2051 return (ParsingDeclStackState) DelayedDiagnostics.size(); 2052} 2053 2054void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) { 2055 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack"); 2056 ParsingDeclDepth--; 2057 2058 if (DelayedDiagnostics.empty()) 2059 return; 2060 2061 unsigned SavedIndex = (unsigned) S; 2062 assert(SavedIndex <= DelayedDiagnostics.size() && 2063 "saved index is out of bounds"); 2064 2065 unsigned E = DelayedDiagnostics.size(); 2066 2067 // We only want to actually emit delayed diagnostics when we 2068 // successfully parsed a decl. 2069 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0; 2070 if (D) { 2071 // We really do want to start with 0 here. We get one push for a 2072 // decl spec and another for each declarator; in a decl group like: 2073 // deprecated_typedef foo, *bar, baz(); 2074 // only the declarator pops will be passed decls. This is correct; 2075 // we really do need to consider delayed diagnostics from the decl spec 2076 // for each of the different declarations. 2077 for (unsigned I = 0; I != E; ++I) { 2078 if (DelayedDiagnostics[I].Triggered) 2079 continue; 2080 2081 switch (DelayedDiagnostics[I].Kind) { 2082 case DelayedDiagnostic::Deprecation: 2083 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D); 2084 break; 2085 2086 case DelayedDiagnostic::Access: 2087 HandleDelayedAccessCheck(DelayedDiagnostics[I], D); 2088 break; 2089 } 2090 } 2091 } 2092 2093 // Destroy all the delayed diagnostics we're about to pop off. 2094 for (unsigned I = SavedIndex; I != E; ++I) 2095 DelayedDiagnostics[I].destroy(); 2096 2097 DelayedDiagnostics.set_size(SavedIndex); 2098} 2099 2100static bool isDeclDeprecated(Decl *D) { 2101 do { 2102 if (D->hasAttr<DeprecatedAttr>()) 2103 return true; 2104 } while ((D = cast_or_null<Decl>(D->getDeclContext()))); 2105 return false; 2106} 2107 2108void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD, 2109 Decl *Ctx) { 2110 if (isDeclDeprecated(Ctx)) 2111 return; 2112 2113 DD.Triggered = true; 2114 Diag(DD.Loc, diag::warn_deprecated) 2115 << DD.DeprecationData.Decl->getDeclName(); 2116} 2117 2118void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) { 2119 // Delay if we're currently parsing a declaration. 2120 if (ParsingDeclDepth) { 2121 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D)); 2122 return; 2123 } 2124 2125 // Otherwise, don't warn if our current context is deprecated. 2126 if (isDeclDeprecated(cast<Decl>(CurContext))) 2127 return; 2128 2129 Diag(Loc, diag::warn_deprecated) << D->getDeclName(); 2130} 2131