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 "clang/Sema/SemaInternal.h" 15 #include "TargetAttributesSema.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclTemplate.h" 19 #include "clang/AST/DeclObjC.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/Basic/SourceManager.h" 22 #include "clang/Basic/TargetInfo.h" 23 #include "clang/Sema/DeclSpec.h" 24 #include "clang/Sema/DelayedDiagnostic.h" 25 #include "clang/Sema/Lookup.h" 26 #include "llvm/ADT/StringExtras.h" 27 using namespace clang; 28 using namespace sema; 29 30 /// These constants match the enumerated choices of 31 /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. 32 enum AttributeDeclKind { 33 ExpectedFunction, 34 ExpectedUnion, 35 ExpectedVariableOrFunction, 36 ExpectedFunctionOrMethod, 37 ExpectedParameter, 38 ExpectedParameterOrMethod, 39 ExpectedFunctionMethodOrBlock, 40 ExpectedClassOrVirtualMethod, 41 ExpectedFunctionMethodOrParameter, 42 ExpectedClass, 43 ExpectedVirtualMethod, 44 ExpectedClassMember, 45 ExpectedVariable, 46 ExpectedMethod, 47 ExpectedVariableFunctionOrLabel, 48 ExpectedFieldOrGlobalVar 49 }; 50 51 //===----------------------------------------------------------------------===// 52 // Helper functions 53 //===----------------------------------------------------------------------===// 54 55 static const FunctionType *getFunctionType(const Decl *D, 56 bool blocksToo = true) { 57 QualType Ty; 58 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) 59 Ty = decl->getType(); 60 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) 61 Ty = decl->getType(); 62 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) 63 Ty = decl->getUnderlyingType(); 64 else 65 return 0; 66 67 if (Ty->isFunctionPointerType()) 68 Ty = Ty->getAs<PointerType>()->getPointeeType(); 69 else if (blocksToo && Ty->isBlockPointerType()) 70 Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); 71 72 return Ty->getAs<FunctionType>(); 73 } 74 75 // FIXME: We should provide an abstraction around a method or function 76 // to provide the following bits of information. 77 78 /// isFunction - Return true if the given decl has function 79 /// type (function or function-typed variable). 80 static bool isFunction(const Decl *D) { 81 return getFunctionType(D, false) != NULL; 82 } 83 84 /// isFunctionOrMethod - Return true if the given decl has function 85 /// type (function or function-typed variable) or an Objective-C 86 /// method. 87 static bool isFunctionOrMethod(const Decl *D) { 88 return isFunction(D)|| isa<ObjCMethodDecl>(D); 89 } 90 91 /// isFunctionOrMethodOrBlock - Return true if the given decl has function 92 /// type (function or function-typed variable) or an Objective-C 93 /// method or a block. 94 static bool isFunctionOrMethodOrBlock(const Decl *D) { 95 if (isFunctionOrMethod(D)) 96 return true; 97 // check for block is more involved. 98 if (const VarDecl *V = dyn_cast<VarDecl>(D)) { 99 QualType Ty = V->getType(); 100 return Ty->isBlockPointerType(); 101 } 102 return isa<BlockDecl>(D); 103 } 104 105 /// Return true if the given decl has a declarator that should have 106 /// been processed by Sema::GetTypeForDeclarator. 107 static bool hasDeclarator(const Decl *D) { 108 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. 109 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || 110 isa<ObjCPropertyDecl>(D); 111 } 112 113 /// hasFunctionProto - Return true if the given decl has a argument 114 /// information. This decl should have already passed 115 /// isFunctionOrMethod or isFunctionOrMethodOrBlock. 116 static bool hasFunctionProto(const Decl *D) { 117 if (const FunctionType *FnTy = getFunctionType(D)) 118 return isa<FunctionProtoType>(FnTy); 119 else { 120 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); 121 return true; 122 } 123 } 124 125 /// getFunctionOrMethodNumArgs - Return number of function or method 126 /// arguments. It is an error to call this on a K&R function (use 127 /// hasFunctionProto first). 128 static unsigned getFunctionOrMethodNumArgs(const Decl *D) { 129 if (const FunctionType *FnTy = getFunctionType(D)) 130 return cast<FunctionProtoType>(FnTy)->getNumArgs(); 131 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) 132 return BD->getNumParams(); 133 return cast<ObjCMethodDecl>(D)->param_size(); 134 } 135 136 static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { 137 if (const FunctionType *FnTy = getFunctionType(D)) 138 return cast<FunctionProtoType>(FnTy)->getArgType(Idx); 139 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) 140 return BD->getParamDecl(Idx)->getType(); 141 142 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); 143 } 144 145 static QualType getFunctionOrMethodResultType(const Decl *D) { 146 if (const FunctionType *FnTy = getFunctionType(D)) 147 return cast<FunctionProtoType>(FnTy)->getResultType(); 148 return cast<ObjCMethodDecl>(D)->getResultType(); 149 } 150 151 static bool isFunctionOrMethodVariadic(const Decl *D) { 152 if (const FunctionType *FnTy = getFunctionType(D)) { 153 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); 154 return proto->isVariadic(); 155 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) 156 return BD->isVariadic(); 157 else { 158 return cast<ObjCMethodDecl>(D)->isVariadic(); 159 } 160 } 161 162 static bool isInstanceMethod(const Decl *D) { 163 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) 164 return MethodDecl->isInstance(); 165 return false; 166 } 167 168 static inline bool isNSStringType(QualType T, ASTContext &Ctx) { 169 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); 170 if (!PT) 171 return false; 172 173 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); 174 if (!Cls) 175 return false; 176 177 IdentifierInfo* ClsName = Cls->getIdentifier(); 178 179 // FIXME: Should we walk the chain of classes? 180 return ClsName == &Ctx.Idents.get("NSString") || 181 ClsName == &Ctx.Idents.get("NSMutableString"); 182 } 183 184 static inline bool isCFStringType(QualType T, ASTContext &Ctx) { 185 const PointerType *PT = T->getAs<PointerType>(); 186 if (!PT) 187 return false; 188 189 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); 190 if (!RT) 191 return false; 192 193 const RecordDecl *RD = RT->getDecl(); 194 if (RD->getTagKind() != TTK_Struct) 195 return false; 196 197 return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); 198 } 199 200 /// \brief Check if the attribute has exactly as many args as Num. May 201 /// output an error. 202 static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, 203 unsigned int Num) { 204 if (Attr.getNumArgs() != Num) { 205 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; 206 return false; 207 } 208 209 return true; 210 } 211 212 213 /// \brief Check if the attribute has at least as many args as Num. May 214 /// output an error. 215 static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, 216 unsigned int Num) { 217 if (Attr.getNumArgs() < Num) { 218 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; 219 return false; 220 } 221 222 return true; 223 } 224 225 /// 226 /// \brief Check if passed in Decl is a field or potentially shared global var 227 /// \return true if the Decl is a field or potentially shared global variable 228 /// 229 static bool mayBeSharedVariable(const Decl *D) { 230 if (isa<FieldDecl>(D)) 231 return true; 232 if (const VarDecl *vd = dyn_cast<VarDecl>(D)) 233 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); 234 235 return false; 236 } 237 238 /// \brief Check if the passed-in expression is of type int or bool. 239 static bool isIntOrBool(Expr *Exp) { 240 QualType QT = Exp->getType(); 241 return QT->isBooleanType() || QT->isIntegerType(); 242 } 243 244 /// 245 /// \brief Check if passed in Decl is a pointer type. 246 /// Note that this function may produce an error message. 247 /// \return true if the Decl is a pointer type; false otherwise 248 /// 249 static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) { 250 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { 251 QualType QT = vd->getType(); 252 if (QT->isAnyPointerType()) 253 return true; 254 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type) 255 << Attr.getName()->getName() << QT; 256 } else { 257 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) 258 << Attr.getName(); 259 } 260 return false; 261 } 262 263 /// \brief Checks that the passed in QualType either is of RecordType or points 264 /// to RecordType. Returns the relevant RecordType, null if it does not exit. 265 static const RecordType *getRecordType(QualType QT) { 266 if (const RecordType *RT = QT->getAs<RecordType>()) 267 return RT; 268 269 // Now check if we point to record type. 270 if (const PointerType *PT = QT->getAs<PointerType>()) 271 return PT->getPointeeType()->getAs<RecordType>(); 272 273 return 0; 274 } 275 276 /// \brief Thread Safety Analysis: Checks that the passed in RecordType 277 /// resolves to a lockable object. May flag an error. 278 static bool checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, 279 const RecordType *RT) { 280 // Flag error if could not get record type for this argument. 281 if (!RT) { 282 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_class) 283 << Attr.getName(); 284 return false; 285 } 286 // Flag error if the type is not lockable. 287 if (!RT->getDecl()->getAttr<LockableAttr>()) { 288 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_lockable) 289 << Attr.getName(); 290 return false; 291 } 292 return true; 293 } 294 295 /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting 296 /// from Sidx, resolve to a lockable object. May flag an error. 297 /// \param Sidx The attribute argument index to start checking with. 298 /// \param ParamIdxOk Whether an argument can be indexing into a function 299 /// parameter list. 300 static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D, 301 const AttributeList &Attr, 302 SmallVectorImpl<Expr*> &Args, 303 int Sidx = 0, 304 bool ParamIdxOk = false) { 305 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { 306 Expr *ArgExp = Attr.getArg(Idx); 307 308 if (ArgExp->isTypeDependent()) { 309 // FIXME -- need to processs this again on template instantiation 310 Args.push_back(ArgExp); 311 continue; 312 } 313 314 QualType ArgTy = ArgExp->getType(); 315 316 // First see if we can just cast to record type, or point to record type. 317 const RecordType *RT = getRecordType(ArgTy); 318 319 // Now check if we index into a record type function param. 320 if(!RT && ParamIdxOk) { 321 FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 322 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); 323 if(FD && IL) { 324 unsigned int NumParams = FD->getNumParams(); 325 llvm::APInt ArgValue = IL->getValue(); 326 uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); 327 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; 328 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { 329 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) 330 << Attr.getName() << Idx + 1 << NumParams; 331 return false; 332 } 333 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); 334 RT = getRecordType(ArgTy); 335 } 336 } 337 338 if (!checkForLockableRecord(S, D, Attr, RT)) 339 return false; 340 341 Args.push_back(ArgExp); 342 } 343 return true; 344 } 345 346 //===----------------------------------------------------------------------===// 347 // Attribute Implementations 348 //===----------------------------------------------------------------------===// 349 350 // FIXME: All this manual attribute parsing code is gross. At the 351 // least add some helper functions to check most argument patterns (# 352 // and types of args). 353 354 static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr, 355 bool pointer = false) { 356 assert(!Attr.isInvalid()); 357 358 if (!checkAttributeNumArgs(S, Attr, 0)) 359 return; 360 361 // D must be either a member field or global (potentially shared) variable. 362 if (!mayBeSharedVariable(D)) { 363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 364 << Attr.getName() << ExpectedFieldOrGlobalVar; 365 return; 366 } 367 368 if (pointer && !checkIsPointer(S, D, Attr)) 369 return; 370 371 if (pointer) 372 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); 373 else 374 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); 375 } 376 377 static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr, 378 bool pointer = false) { 379 assert(!Attr.isInvalid()); 380 381 if (!checkAttributeNumArgs(S, Attr, 1)) 382 return; 383 384 Expr *Arg = Attr.getArg(0); 385 386 // D must be either a member field or global (potentially shared) variable. 387 if (!mayBeSharedVariable(D)) { 388 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 389 << Attr.getName() << ExpectedFieldOrGlobalVar; 390 return; 391 } 392 393 if (pointer && !checkIsPointer(S, D, Attr)) 394 return; 395 396 if (Arg->isTypeDependent()) 397 // FIXME: handle attributes with dependent types 398 return; 399 400 // check that the argument is lockable object 401 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType()))) 402 return; 403 404 if (pointer) 405 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), 406 S.Context, Arg)); 407 else 408 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); 409 } 410 411 412 static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr, 413 bool scoped = false) { 414 assert(!Attr.isInvalid()); 415 416 if (!checkAttributeNumArgs(S, Attr, 0)) 417 return; 418 419 // FIXME: Lockable structs for C code. 420 if (!isa<CXXRecordDecl>(D)) { 421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 422 << Attr.getName() << ExpectedClass; 423 return; 424 } 425 426 if (scoped) 427 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); 428 else 429 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); 430 } 431 432 static void handleNoThreadSafetyAttr(Sema &S, Decl *D, 433 const AttributeList &Attr) { 434 assert(!Attr.isInvalid()); 435 436 if (!checkAttributeNumArgs(S, Attr, 0)) 437 return; 438 439 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 440 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 441 << Attr.getName() << ExpectedFunctionOrMethod; 442 return; 443 } 444 445 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), 446 S.Context)); 447 } 448 449 static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr, 450 bool before) { 451 assert(!Attr.isInvalid()); 452 453 if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) 454 return; 455 456 // D must be either a member field or global (potentially shared) variable. 457 ValueDecl *VD = dyn_cast<ValueDecl>(D); 458 if (!VD || !mayBeSharedVariable(D)) { 459 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 460 << Attr.getName() << ExpectedFieldOrGlobalVar; 461 return; 462 } 463 464 // Check that this attribute only applies to lockable types 465 QualType QT = VD->getType(); 466 if (!QT->isDependentType()) { 467 const RecordType *RT = getRecordType(QT); 468 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { 469 S.Diag(Attr.getLoc(), diag::err_attribute_decl_not_lockable) 470 << Attr.getName(); 471 return; 472 } 473 } 474 475 SmallVector<Expr*, 1> Args; 476 // check that all arguments are lockable objects 477 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args)) 478 return; 479 480 unsigned Size = Args.size(); 481 assert(Size == Attr.getNumArgs()); 482 Expr **StartArg = Size == 0 ? 0 : &Args[0]; 483 484 if (before) 485 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, 486 StartArg, Size)); 487 else 488 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, 489 StartArg, Size)); 490 } 491 492 static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, 493 bool exclusive = false) { 494 assert(!Attr.isInvalid()); 495 496 // zero or more arguments ok 497 498 // check that the attribute is applied to a function 499 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 501 << Attr.getName() << ExpectedFunctionOrMethod; 502 return; 503 } 504 505 // check that all arguments are lockable objects 506 SmallVector<Expr*, 1> Args; 507 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true)) 508 return; 509 510 unsigned Size = Args.size(); 511 assert(Size == Attr.getNumArgs()); 512 Expr **StartArg = Size == 0 ? 0 : &Args[0]; 513 514 if (exclusive) 515 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), 516 S.Context, StartArg, 517 Size)); 518 else 519 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), 520 S.Context, StartArg, 521 Size)); 522 } 523 524 static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, 525 bool exclusive = false) { 526 assert(!Attr.isInvalid()); 527 528 if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) 529 return; 530 531 532 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 533 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 534 << Attr.getName() << ExpectedFunctionOrMethod; 535 return; 536 } 537 538 if (!isIntOrBool(Attr.getArg(0))) { 539 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) 540 << Attr.getName(); 541 return; 542 } 543 544 SmallVector<Expr*, 2> Args; 545 // check that all arguments are lockable objects 546 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1)) 547 return; 548 549 unsigned Size = Args.size(); 550 Expr **StartArg = Size == 0 ? 0 : &Args[0]; 551 552 if (exclusive) 553 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), 554 S.Context, 555 Attr.getArg(0), 556 StartArg, Size)); 557 else 558 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), 559 S.Context, 560 Attr.getArg(0), 561 StartArg, Size)); 562 } 563 564 static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr, 565 bool exclusive = false) { 566 assert(!Attr.isInvalid()); 567 568 if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) 569 return; 570 571 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 573 << Attr.getName() << ExpectedFunctionOrMethod; 574 return; 575 } 576 577 // check that all arguments are lockable objects 578 SmallVector<Expr*, 1> Args; 579 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args)) 580 return; 581 582 unsigned Size = Args.size(); 583 assert(Size == Attr.getNumArgs()); 584 Expr **StartArg = Size == 0 ? 0 : &Args[0]; 585 586 if (exclusive) 587 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), 588 S.Context, StartArg, 589 Size)); 590 else 591 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), 592 S.Context, StartArg, 593 Size)); 594 } 595 596 static void handleUnlockFunAttr(Sema &S, Decl *D, 597 const AttributeList &Attr) { 598 assert(!Attr.isInvalid()); 599 600 // zero or more arguments ok 601 602 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 603 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 604 << Attr.getName() << ExpectedFunctionOrMethod; 605 return; 606 } 607 608 // check that all arguments are lockable objects 609 SmallVector<Expr*, 1> Args; 610 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true)) 611 return; 612 613 unsigned Size = Args.size(); 614 assert(Size == Attr.getNumArgs()); 615 Expr **StartArg = Size == 0 ? 0 : &Args[0]; 616 617 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, 618 StartArg, Size)); 619 } 620 621 static void handleLockReturnedAttr(Sema &S, Decl *D, 622 const AttributeList &Attr) { 623 assert(!Attr.isInvalid()); 624 625 if (!checkAttributeNumArgs(S, Attr, 1)) 626 return; 627 Expr *Arg = Attr.getArg(0); 628 629 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 631 << Attr.getName() << ExpectedFunctionOrMethod; 632 return; 633 } 634 635 if (Arg->isTypeDependent()) 636 return; 637 638 // check that the argument is lockable object 639 if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType()))) 640 return; 641 642 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg)); 643 } 644 645 static void handleLocksExcludedAttr(Sema &S, Decl *D, 646 const AttributeList &Attr) { 647 assert(!Attr.isInvalid()); 648 649 if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) 650 return; 651 652 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { 653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 654 << Attr.getName() << ExpectedFunctionOrMethod; 655 return; 656 } 657 658 // check that all arguments are lockable objects 659 SmallVector<Expr*, 1> Args; 660 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args)) 661 return; 662 663 unsigned Size = Args.size(); 664 assert(Size == Attr.getNumArgs()); 665 Expr **StartArg = Size == 0 ? 0 : &Args[0]; 666 667 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, 668 StartArg, Size)); 669 } 670 671 672 static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, 673 const AttributeList &Attr) { 674 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); 675 if (tDecl == 0) { 676 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); 677 return; 678 } 679 680 QualType curType = tDecl->getUnderlyingType(); 681 682 Expr *sizeExpr; 683 684 // Special case where the argument is a template id. 685 if (Attr.getParameterName()) { 686 CXXScopeSpec SS; 687 UnqualifiedId id; 688 id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); 689 690 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false); 691 if (Size.isInvalid()) 692 return; 693 694 sizeExpr = Size.get(); 695 } else { 696 // check the attribute arguments. 697 if (!checkAttributeNumArgs(S, Attr, 1)) 698 return; 699 700 sizeExpr = Attr.getArg(0); 701 } 702 703 // Instantiate/Install the vector type, and let Sema build the type for us. 704 // This will run the reguired checks. 705 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); 706 if (!T.isNull()) { 707 // FIXME: preserve the old source info. 708 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); 709 710 // Remember this typedef decl, we will need it later for diagnostics. 711 S.ExtVectorDecls.push_back(tDecl); 712 } 713 } 714 715 static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 716 // check the attribute arguments. 717 if (!checkAttributeNumArgs(S, Attr, 0)) 718 return; 719 720 if (TagDecl *TD = dyn_cast<TagDecl>(D)) 721 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); 722 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { 723 // If the alignment is less than or equal to 8 bits, the packed attribute 724 // has no effect. 725 if (!FD->getType()->isIncompleteType() && 726 S.Context.getTypeAlign(FD->getType()) <= 8) 727 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) 728 << Attr.getName() << FD->getType(); 729 else 730 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); 731 } else 732 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 733 } 734 735 static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { 736 if (TagDecl *TD = dyn_cast<TagDecl>(D)) 737 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); 738 else 739 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 740 } 741 742 static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { 743 // check the attribute arguments. 744 if (!checkAttributeNumArgs(S, Attr, 0)) 745 return; 746 747 // The IBAction attributes only apply to instance methods. 748 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 749 if (MD->isInstanceMethod()) { 750 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); 751 return; 752 } 753 754 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); 755 } 756 757 static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { 758 // The IBOutlet/IBOutletCollection attributes only apply to instance 759 // variables or properties of Objective-C classes. The outlet must also 760 // have an object reference type. 761 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { 762 if (!VD->getType()->getAs<ObjCObjectPointerType>()) { 763 S.Diag(Attr.getLoc(), diag::err_iboutlet_object_type) 764 << Attr.getName() << VD->getType() << 0; 765 return false; 766 } 767 } 768 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { 769 if (!PD->getType()->getAs<ObjCObjectPointerType>()) { 770 S.Diag(Attr.getLoc(), diag::err_iboutlet_object_type) 771 << Attr.getName() << PD->getType() << 1; 772 return false; 773 } 774 } 775 else { 776 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); 777 return false; 778 } 779 780 return true; 781 } 782 783 static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { 784 // check the attribute arguments. 785 if (!checkAttributeNumArgs(S, Attr, 0)) 786 return; 787 788 if (!checkIBOutletCommon(S, D, Attr)) 789 return; 790 791 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); 792 } 793 794 static void handleIBOutletCollection(Sema &S, Decl *D, 795 const AttributeList &Attr) { 796 797 // The iboutletcollection attribute can have zero or one arguments. 798 if (Attr.getParameterName() && Attr.getNumArgs() > 0) { 799 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 800 return; 801 } 802 803 if (!checkIBOutletCommon(S, D, Attr)) 804 return; 805 806 IdentifierInfo *II = Attr.getParameterName(); 807 if (!II) 808 II = &S.Context.Idents.get("id"); 809 810 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), 811 S.getScopeForContext(D->getDeclContext()->getParent())); 812 if (!TypeRep) { 813 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; 814 return; 815 } 816 QualType QT = TypeRep.get(); 817 // Diagnose use of non-object type in iboutletcollection attribute. 818 // FIXME. Gnu attribute extension ignores use of builtin types in 819 // attributes. So, __attribute__((iboutletcollection(char))) will be 820 // treated as __attribute__((iboutletcollection())). 821 if (!QT->isObjCIdType() && !QT->isObjCClassType() && 822 !QT->isObjCObjectType()) { 823 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; 824 return; 825 } 826 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, 827 QT, Attr.getParameterLoc())); 828 } 829 830 static void possibleTransparentUnionPointerType(QualType &T) { 831 if (const RecordType *UT = T->getAsUnionType()) 832 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { 833 RecordDecl *UD = UT->getDecl(); 834 for (RecordDecl::field_iterator it = UD->field_begin(), 835 itend = UD->field_end(); it != itend; ++it) { 836 QualType QT = it->getType(); 837 if (QT->isAnyPointerType() || QT->isBlockPointerType()) { 838 T = QT; 839 return; 840 } 841 } 842 } 843 } 844 845 static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { 846 // GCC ignores the nonnull attribute on K&R style function prototypes, so we 847 // ignore it as well 848 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { 849 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 850 << Attr.getName() << ExpectedFunction; 851 return; 852 } 853 854 // In C++ the implicit 'this' function parameter also counts, and they are 855 // counted from one. 856 bool HasImplicitThisParam = isInstanceMethod(D); 857 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; 858 859 // The nonnull attribute only applies to pointers. 860 SmallVector<unsigned, 10> NonNullArgs; 861 862 for (AttributeList::arg_iterator I=Attr.arg_begin(), 863 E=Attr.arg_end(); I!=E; ++I) { 864 865 866 // The argument must be an integer constant expression. 867 Expr *Ex = *I; 868 llvm::APSInt ArgNum(32); 869 if (Ex->isTypeDependent() || Ex->isValueDependent() || 870 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { 871 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 872 << "nonnull" << Ex->getSourceRange(); 873 return; 874 } 875 876 unsigned x = (unsigned) ArgNum.getZExtValue(); 877 878 if (x < 1 || x > NumArgs) { 879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 880 << "nonnull" << I.getArgNum() << Ex->getSourceRange(); 881 return; 882 } 883 884 --x; 885 if (HasImplicitThisParam) { 886 if (x == 0) { 887 S.Diag(Attr.getLoc(), 888 diag::err_attribute_invalid_implicit_this_argument) 889 << "nonnull" << Ex->getSourceRange(); 890 return; 891 } 892 --x; 893 } 894 895 // Is the function argument a pointer type? 896 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); 897 possibleTransparentUnionPointerType(T); 898 899 if (!T->isAnyPointerType() && !T->isBlockPointerType()) { 900 // FIXME: Should also highlight argument in decl. 901 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) 902 << "nonnull" << Ex->getSourceRange(); 903 continue; 904 } 905 906 NonNullArgs.push_back(x); 907 } 908 909 // If no arguments were specified to __attribute__((nonnull)) then all pointer 910 // arguments have a nonnull attribute. 911 if (NonNullArgs.empty()) { 912 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { 913 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); 914 possibleTransparentUnionPointerType(T); 915 if (T->isAnyPointerType() || T->isBlockPointerType()) 916 NonNullArgs.push_back(I); 917 } 918 919 // No pointer arguments? 920 if (NonNullArgs.empty()) { 921 // Warn the trivial case only if attribute is not coming from a 922 // macro instantiation. 923 if (Attr.getLoc().isFileID()) 924 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); 925 return; 926 } 927 } 928 929 unsigned* start = &NonNullArgs[0]; 930 unsigned size = NonNullArgs.size(); 931 llvm::array_pod_sort(start, start + size); 932 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, 933 size)); 934 } 935 936 static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { 937 // This attribute must be applied to a function declaration. 938 // The first argument to the attribute must be a string, 939 // the name of the resource, for example "malloc". 940 // The following arguments must be argument indexes, the arguments must be 941 // of integer type for Returns, otherwise of pointer type. 942 // The difference between Holds and Takes is that a pointer may still be used 943 // after being held. free() should be __attribute((ownership_takes)), whereas 944 // a list append function may well be __attribute((ownership_holds)). 945 946 if (!AL.getParameterName()) { 947 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) 948 << AL.getName()->getName() << 1; 949 return; 950 } 951 // Figure out our Kind, and check arguments while we're at it. 952 OwnershipAttr::OwnershipKind K; 953 switch (AL.getKind()) { 954 case AttributeList::AT_ownership_takes: 955 K = OwnershipAttr::Takes; 956 if (AL.getNumArgs() < 1) { 957 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; 958 return; 959 } 960 break; 961 case AttributeList::AT_ownership_holds: 962 K = OwnershipAttr::Holds; 963 if (AL.getNumArgs() < 1) { 964 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; 965 return; 966 } 967 break; 968 case AttributeList::AT_ownership_returns: 969 K = OwnershipAttr::Returns; 970 if (AL.getNumArgs() > 1) { 971 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) 972 << AL.getNumArgs() + 1; 973 return; 974 } 975 break; 976 default: 977 // This should never happen given how we are called. 978 llvm_unreachable("Unknown ownership attribute"); 979 } 980 981 if (!isFunction(D) || !hasFunctionProto(D)) { 982 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) 983 << AL.getName() << ExpectedFunction; 984 return; 985 } 986 987 // In C++ the implicit 'this' function parameter also counts, and they are 988 // counted from one. 989 bool HasImplicitThisParam = isInstanceMethod(D); 990 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; 991 992 StringRef Module = AL.getParameterName()->getName(); 993 994 // Normalize the argument, __foo__ becomes foo. 995 if (Module.startswith("__") && Module.endswith("__")) 996 Module = Module.substr(2, Module.size() - 4); 997 998 SmallVector<unsigned, 10> OwnershipArgs; 999 1000 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; 1001 ++I) { 1002 1003 Expr *IdxExpr = *I; 1004 llvm::APSInt ArgNum(32); 1005 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() 1006 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { 1007 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) 1008 << AL.getName()->getName() << IdxExpr->getSourceRange(); 1009 continue; 1010 } 1011 1012 unsigned x = (unsigned) ArgNum.getZExtValue(); 1013 1014 if (x > NumArgs || x < 1) { 1015 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) 1016 << AL.getName()->getName() << x << IdxExpr->getSourceRange(); 1017 continue; 1018 } 1019 --x; 1020 if (HasImplicitThisParam) { 1021 if (x == 0) { 1022 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) 1023 << "ownership" << IdxExpr->getSourceRange(); 1024 return; 1025 } 1026 --x; 1027 } 1028 1029 switch (K) { 1030 case OwnershipAttr::Takes: 1031 case OwnershipAttr::Holds: { 1032 // Is the function argument a pointer type? 1033 QualType T = getFunctionOrMethodArgType(D, x); 1034 if (!T->isAnyPointerType() && !T->isBlockPointerType()) { 1035 // FIXME: Should also highlight argument in decl. 1036 S.Diag(AL.getLoc(), diag::err_ownership_type) 1037 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") 1038 << "pointer" 1039 << IdxExpr->getSourceRange(); 1040 continue; 1041 } 1042 break; 1043 } 1044 case OwnershipAttr::Returns: { 1045 if (AL.getNumArgs() > 1) { 1046 // Is the function argument an integer type? 1047 Expr *IdxExpr = AL.getArg(0); 1048 llvm::APSInt ArgNum(32); 1049 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() 1050 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { 1051 S.Diag(AL.getLoc(), diag::err_ownership_type) 1052 << "ownership_returns" << "integer" 1053 << IdxExpr->getSourceRange(); 1054 return; 1055 } 1056 } 1057 break; 1058 } 1059 default: 1060 llvm_unreachable("Unknown ownership attribute"); 1061 } // switch 1062 1063 // Check we don't have a conflict with another ownership attribute. 1064 for (specific_attr_iterator<OwnershipAttr> 1065 i = D->specific_attr_begin<OwnershipAttr>(), 1066 e = D->specific_attr_end<OwnershipAttr>(); 1067 i != e; ++i) { 1068 if ((*i)->getOwnKind() != K) { 1069 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); 1070 I!=E; ++I) { 1071 if (x == *I) { 1072 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) 1073 << AL.getName()->getName() << "ownership_*"; 1074 } 1075 } 1076 } 1077 } 1078 OwnershipArgs.push_back(x); 1079 } 1080 1081 unsigned* start = OwnershipArgs.data(); 1082 unsigned size = OwnershipArgs.size(); 1083 llvm::array_pod_sort(start, start + size); 1084 1085 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { 1086 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; 1087 return; 1088 } 1089 1090 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, 1091 start, size)); 1092 } 1093 1094 /// Whether this declaration has internal linkage for the purposes of 1095 /// things that want to complain about things not have internal linkage. 1096 static bool hasEffectivelyInternalLinkage(NamedDecl *D) { 1097 switch (D->getLinkage()) { 1098 case NoLinkage: 1099 case InternalLinkage: 1100 return true; 1101 1102 // Template instantiations that go from external to unique-external 1103 // shouldn't get diagnosed. 1104 case UniqueExternalLinkage: 1105 return true; 1106 1107 case ExternalLinkage: 1108 return false; 1109 } 1110 llvm_unreachable("unknown linkage kind!"); 1111 return false; 1112 } 1113 1114 static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1115 // Check the attribute arguments. 1116 if (Attr.getNumArgs() > 1) { 1117 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1118 return; 1119 } 1120 1121 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { 1122 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 1123 << Attr.getName() << ExpectedVariableOrFunction; 1124 return; 1125 } 1126 1127 NamedDecl *nd = cast<NamedDecl>(D); 1128 1129 // gcc rejects 1130 // class c { 1131 // static int a __attribute__((weakref ("v2"))); 1132 // static int b() __attribute__((weakref ("f3"))); 1133 // }; 1134 // and ignores the attributes of 1135 // void f(void) { 1136 // static int a __attribute__((weakref ("v2"))); 1137 // } 1138 // we reject them 1139 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); 1140 if (!Ctx->isFileContext()) { 1141 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << 1142 nd->getNameAsString(); 1143 return; 1144 } 1145 1146 // The GCC manual says 1147 // 1148 // At present, a declaration to which `weakref' is attached can only 1149 // be `static'. 1150 // 1151 // It also says 1152 // 1153 // Without a TARGET, 1154 // given as an argument to `weakref' or to `alias', `weakref' is 1155 // equivalent to `weak'. 1156 // 1157 // gcc 4.4.1 will accept 1158 // int a7 __attribute__((weakref)); 1159 // as 1160 // int a7 __attribute__((weak)); 1161 // This looks like a bug in gcc. We reject that for now. We should revisit 1162 // it if this behaviour is actually used. 1163 1164 if (!hasEffectivelyInternalLinkage(nd)) { 1165 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); 1166 return; 1167 } 1168 1169 // GCC rejects 1170 // static ((alias ("y"), weakref)). 1171 // Should we? How to check that weakref is before or after alias? 1172 1173 if (Attr.getNumArgs() == 1) { 1174 Expr *Arg = Attr.getArg(0); 1175 Arg = Arg->IgnoreParenCasts(); 1176 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 1177 1178 if (!Str || !Str->isAscii()) { 1179 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1180 << "weakref" << 1; 1181 return; 1182 } 1183 // GCC will accept anything as the argument of weakref. Should we 1184 // check for an existing decl? 1185 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, 1186 Str->getString())); 1187 } 1188 1189 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); 1190 } 1191 1192 static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1193 // check the attribute arguments. 1194 if (Attr.getNumArgs() != 1) { 1195 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1196 return; 1197 } 1198 1199 Expr *Arg = Attr.getArg(0); 1200 Arg = Arg->IgnoreParenCasts(); 1201 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 1202 1203 if (!Str || !Str->isAscii()) { 1204 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1205 << "alias" << 1; 1206 return; 1207 } 1208 1209 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { 1210 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); 1211 return; 1212 } 1213 1214 // FIXME: check if target symbol exists in current file 1215 1216 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, 1217 Str->getString())); 1218 } 1219 1220 static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1221 // Check the attribute arguments. 1222 if (!checkAttributeNumArgs(S, Attr, 0)) 1223 return; 1224 1225 if (!isa<FunctionDecl>(D)) { 1226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1227 << Attr.getName() << ExpectedFunction; 1228 return; 1229 } 1230 1231 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); 1232 } 1233 1234 static void handleAlwaysInlineAttr(Sema &S, Decl *D, 1235 const AttributeList &Attr) { 1236 // Check the attribute arguments. 1237 if (Attr.hasParameterOrArguments()) { 1238 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1239 return; 1240 } 1241 1242 if (!isa<FunctionDecl>(D)) { 1243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1244 << Attr.getName() << ExpectedFunction; 1245 return; 1246 } 1247 1248 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); 1249 } 1250 1251 static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1252 // Check the attribute arguments. 1253 if (Attr.hasParameterOrArguments()) { 1254 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1255 return; 1256 } 1257 1258 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1259 QualType RetTy = FD->getResultType(); 1260 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { 1261 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); 1262 return; 1263 } 1264 } 1265 1266 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); 1267 } 1268 1269 static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1270 // check the attribute arguments. 1271 if (!checkAttributeNumArgs(S, Attr, 0)) 1272 return; 1273 1274 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); 1275 } 1276 1277 static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1278 assert(!Attr.isInvalid()); 1279 if (isa<VarDecl>(D)) 1280 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); 1281 else 1282 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1283 << Attr.getName() << ExpectedVariable; 1284 } 1285 1286 static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1287 assert(!Attr.isInvalid()); 1288 if (isa<VarDecl>(D)) 1289 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); 1290 else 1291 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1292 << Attr.getName() << ExpectedVariable; 1293 } 1294 1295 static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { 1296 if (hasDeclarator(D)) return; 1297 1298 if (S.CheckNoReturnAttr(attr)) return; 1299 1300 if (!isa<ObjCMethodDecl>(D)) { 1301 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1302 << attr.getName() << ExpectedFunctionOrMethod; 1303 return; 1304 } 1305 1306 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); 1307 } 1308 1309 bool Sema::CheckNoReturnAttr(const AttributeList &attr) { 1310 if (attr.hasParameterOrArguments()) { 1311 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1312 attr.setInvalid(); 1313 return true; 1314 } 1315 1316 return false; 1317 } 1318 1319 static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, 1320 const AttributeList &Attr) { 1321 1322 // The checking path for 'noreturn' and 'analyzer_noreturn' are different 1323 // because 'analyzer_noreturn' does not impact the type. 1324 1325 if(!checkAttributeNumArgs(S, Attr, 0)) 1326 return; 1327 1328 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { 1329 ValueDecl *VD = dyn_cast<ValueDecl>(D); 1330 if (VD == 0 || (!VD->getType()->isBlockPointerType() 1331 && !VD->getType()->isFunctionPointerType())) { 1332 S.Diag(Attr.getLoc(), 1333 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 1334 : diag::warn_attribute_wrong_decl_type) 1335 << Attr.getName() << ExpectedFunctionMethodOrBlock; 1336 return; 1337 } 1338 } 1339 1340 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); 1341 } 1342 1343 // PS3 PPU-specific. 1344 static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1345 /* 1346 Returning a Vector Class in Registers 1347 1348 According to the PPU ABI specifications, a class with a single member of 1349 vector type is returned in memory when used as the return value of a function. 1350 This results in inefficient code when implementing vector classes. To return 1351 the value in a single vector register, add the vecreturn attribute to the 1352 class definition. This attribute is also applicable to struct types. 1353 1354 Example: 1355 1356 struct Vector 1357 { 1358 __vector float xyzw; 1359 } __attribute__((vecreturn)); 1360 1361 Vector Add(Vector lhs, Vector rhs) 1362 { 1363 Vector result; 1364 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); 1365 return result; // This will be returned in a register 1366 } 1367 */ 1368 if (!isa<RecordDecl>(D)) { 1369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 1370 << Attr.getName() << ExpectedClass; 1371 return; 1372 } 1373 1374 if (D->getAttr<VecReturnAttr>()) { 1375 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; 1376 return; 1377 } 1378 1379 RecordDecl *record = cast<RecordDecl>(D); 1380 int count = 0; 1381 1382 if (!isa<CXXRecordDecl>(record)) { 1383 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); 1384 return; 1385 } 1386 1387 if (!cast<CXXRecordDecl>(record)->isPOD()) { 1388 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); 1389 return; 1390 } 1391 1392 for (RecordDecl::field_iterator iter = record->field_begin(); 1393 iter != record->field_end(); iter++) { 1394 if ((count == 1) || !iter->getType()->isVectorType()) { 1395 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); 1396 return; 1397 } 1398 count++; 1399 } 1400 1401 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); 1402 } 1403 1404 static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1405 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { 1406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 1407 << Attr.getName() << ExpectedFunctionMethodOrParameter; 1408 return; 1409 } 1410 // FIXME: Actually store the attribute on the declaration 1411 } 1412 1413 static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1414 // check the attribute arguments. 1415 if (Attr.hasParameterOrArguments()) { 1416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1417 return; 1418 } 1419 1420 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && 1421 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) { 1422 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1423 << Attr.getName() << ExpectedVariableFunctionOrLabel; 1424 return; 1425 } 1426 1427 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); 1428 } 1429 1430 static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1431 // check the attribute arguments. 1432 if (Attr.hasParameterOrArguments()) { 1433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1434 return; 1435 } 1436 1437 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 1438 if (VD->hasLocalStorage() || VD->hasExternalStorage()) { 1439 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; 1440 return; 1441 } 1442 } else if (!isFunctionOrMethod(D)) { 1443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1444 << Attr.getName() << ExpectedVariableOrFunction; 1445 return; 1446 } 1447 1448 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); 1449 } 1450 1451 static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1452 // check the attribute arguments. 1453 if (Attr.getNumArgs() > 1) { 1454 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; 1455 return; 1456 } 1457 1458 int priority = 65535; // FIXME: Do not hardcode such constants. 1459 if (Attr.getNumArgs() > 0) { 1460 Expr *E = Attr.getArg(0); 1461 llvm::APSInt Idx(32); 1462 if (E->isTypeDependent() || E->isValueDependent() || 1463 !E->isIntegerConstantExpr(Idx, S.Context)) { 1464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1465 << "constructor" << 1 << E->getSourceRange(); 1466 return; 1467 } 1468 priority = Idx.getZExtValue(); 1469 } 1470 1471 if (!isa<FunctionDecl>(D)) { 1472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1473 << Attr.getName() << ExpectedFunction; 1474 return; 1475 } 1476 1477 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, 1478 priority)); 1479 } 1480 1481 static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1482 // check the attribute arguments. 1483 if (Attr.getNumArgs() > 1) { 1484 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; 1485 return; 1486 } 1487 1488 int priority = 65535; // FIXME: Do not hardcode such constants. 1489 if (Attr.getNumArgs() > 0) { 1490 Expr *E = Attr.getArg(0); 1491 llvm::APSInt Idx(32); 1492 if (E->isTypeDependent() || E->isValueDependent() || 1493 !E->isIntegerConstantExpr(Idx, S.Context)) { 1494 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1495 << "destructor" << 1 << E->getSourceRange(); 1496 return; 1497 } 1498 priority = Idx.getZExtValue(); 1499 } 1500 1501 if (!isa<FunctionDecl>(D)) { 1502 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1503 << Attr.getName() << ExpectedFunction; 1504 return; 1505 } 1506 1507 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, 1508 priority)); 1509 } 1510 1511 static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1512 unsigned NumArgs = Attr.getNumArgs(); 1513 if (NumArgs > 1) { 1514 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; 1515 return; 1516 } 1517 1518 // Handle the case where deprecated attribute has a text message. 1519 StringRef Str; 1520 if (NumArgs == 1) { 1521 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); 1522 if (!SE) { 1523 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) 1524 << "deprecated"; 1525 return; 1526 } 1527 Str = SE->getString(); 1528 } 1529 1530 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str)); 1531 } 1532 1533 static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1534 unsigned NumArgs = Attr.getNumArgs(); 1535 if (NumArgs > 1) { 1536 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; 1537 return; 1538 } 1539 1540 // Handle the case where unavailable attribute has a text message. 1541 StringRef Str; 1542 if (NumArgs == 1) { 1543 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); 1544 if (!SE) { 1545 S.Diag(Attr.getArg(0)->getLocStart(), 1546 diag::err_attribute_not_string) << "unavailable"; 1547 return; 1548 } 1549 Str = SE->getString(); 1550 } 1551 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str)); 1552 } 1553 1554 static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, 1555 const AttributeList &Attr) { 1556 unsigned NumArgs = Attr.getNumArgs(); 1557 if (NumArgs > 0) { 1558 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; 1559 return; 1560 } 1561 1562 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( 1563 Attr.getRange(), S.Context)); 1564 } 1565 1566 static void handleAvailabilityAttr(Sema &S, Decl *D, 1567 const AttributeList &Attr) { 1568 IdentifierInfo *Platform = Attr.getParameterName(); 1569 SourceLocation PlatformLoc = Attr.getParameterLoc(); 1570 1571 StringRef PlatformName 1572 = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); 1573 if (PlatformName.empty()) { 1574 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) 1575 << Platform; 1576 1577 PlatformName = Platform->getName(); 1578 } 1579 1580 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); 1581 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); 1582 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); 1583 bool IsUnavailable = Attr.getUnavailableLoc().isValid(); 1584 1585 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all 1586 // of these steps are needed). 1587 if (Introduced.isValid() && Deprecated.isValid() && 1588 !(Introduced.Version <= Deprecated.Version)) { 1589 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) 1590 << 1 << PlatformName << Deprecated.Version.getAsString() 1591 << 0 << Introduced.Version.getAsString(); 1592 return; 1593 } 1594 1595 if (Introduced.isValid() && Obsoleted.isValid() && 1596 !(Introduced.Version <= Obsoleted.Version)) { 1597 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) 1598 << 2 << PlatformName << Obsoleted.Version.getAsString() 1599 << 0 << Introduced.Version.getAsString(); 1600 return; 1601 } 1602 1603 if (Deprecated.isValid() && Obsoleted.isValid() && 1604 !(Deprecated.Version <= Obsoleted.Version)) { 1605 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering) 1606 << 2 << PlatformName << Obsoleted.Version.getAsString() 1607 << 1 << Deprecated.Version.getAsString(); 1608 return; 1609 } 1610 1611 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context, 1612 Platform, 1613 Introduced.Version, 1614 Deprecated.Version, 1615 Obsoleted.Version, 1616 IsUnavailable)); 1617 } 1618 1619 static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1620 // check the attribute arguments. 1621 if(!checkAttributeNumArgs(S, Attr, 1)) 1622 return; 1623 1624 Expr *Arg = Attr.getArg(0); 1625 Arg = Arg->IgnoreParenCasts(); 1626 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 1627 1628 if (!Str || !Str->isAscii()) { 1629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1630 << "visibility" << 1; 1631 return; 1632 } 1633 1634 StringRef TypeStr = Str->getString(); 1635 VisibilityAttr::VisibilityType type; 1636 1637 if (TypeStr == "default") 1638 type = VisibilityAttr::Default; 1639 else if (TypeStr == "hidden") 1640 type = VisibilityAttr::Hidden; 1641 else if (TypeStr == "internal") 1642 type = VisibilityAttr::Hidden; // FIXME 1643 else if (TypeStr == "protected") 1644 type = VisibilityAttr::Protected; 1645 else { 1646 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; 1647 return; 1648 } 1649 1650 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type)); 1651 } 1652 1653 static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, 1654 const AttributeList &Attr) { 1655 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); 1656 if (!method) { 1657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 1658 << ExpectedMethod; 1659 return; 1660 } 1661 1662 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { 1663 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { 1664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1665 << "objc_method_family" << 1; 1666 } else { 1667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1668 } 1669 Attr.setInvalid(); 1670 return; 1671 } 1672 1673 StringRef param = Attr.getParameterName()->getName(); 1674 ObjCMethodFamilyAttr::FamilyKind family; 1675 if (param == "none") 1676 family = ObjCMethodFamilyAttr::OMF_None; 1677 else if (param == "alloc") 1678 family = ObjCMethodFamilyAttr::OMF_alloc; 1679 else if (param == "copy") 1680 family = ObjCMethodFamilyAttr::OMF_copy; 1681 else if (param == "init") 1682 family = ObjCMethodFamilyAttr::OMF_init; 1683 else if (param == "mutableCopy") 1684 family = ObjCMethodFamilyAttr::OMF_mutableCopy; 1685 else if (param == "new") 1686 family = ObjCMethodFamilyAttr::OMF_new; 1687 else { 1688 // Just warn and ignore it. This is future-proof against new 1689 // families being used in system headers. 1690 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); 1691 return; 1692 } 1693 1694 if (family == ObjCMethodFamilyAttr::OMF_init && 1695 !method->getResultType()->isObjCObjectPointerType()) { 1696 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) 1697 << method->getResultType(); 1698 // Ignore the attribute. 1699 return; 1700 } 1701 1702 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), 1703 S.Context, family)); 1704 } 1705 1706 static void handleObjCExceptionAttr(Sema &S, Decl *D, 1707 const AttributeList &Attr) { 1708 if (!checkAttributeNumArgs(S, Attr, 0)) 1709 return; 1710 1711 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); 1712 if (OCI == 0) { 1713 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); 1714 return; 1715 } 1716 1717 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); 1718 } 1719 1720 static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { 1721 if (Attr.getNumArgs() != 0) { 1722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1723 return; 1724 } 1725 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { 1726 QualType T = TD->getUnderlyingType(); 1727 if (!T->isPointerType() || 1728 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { 1729 S.Diag(TD->getLocation(), diag::err_nsobject_attribute); 1730 return; 1731 } 1732 } 1733 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); 1734 } 1735 1736 static void 1737 handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1738 if (Attr.getNumArgs() != 0) { 1739 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1740 return; 1741 } 1742 1743 if (!isa<FunctionDecl>(D)) { 1744 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); 1745 return; 1746 } 1747 1748 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); 1749 } 1750 1751 static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1752 if (!Attr.getParameterName()) { 1753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1754 << "blocks" << 1; 1755 return; 1756 } 1757 1758 if (Attr.getNumArgs() != 0) { 1759 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1760 return; 1761 } 1762 1763 BlocksAttr::BlockType type; 1764 if (Attr.getParameterName()->isStr("byref")) 1765 type = BlocksAttr::ByRef; 1766 else { 1767 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1768 << "blocks" << Attr.getParameterName(); 1769 return; 1770 } 1771 1772 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); 1773 } 1774 1775 static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1776 // check the attribute arguments. 1777 if (Attr.getNumArgs() > 2) { 1778 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; 1779 return; 1780 } 1781 1782 unsigned sentinel = 0; 1783 if (Attr.getNumArgs() > 0) { 1784 Expr *E = Attr.getArg(0); 1785 llvm::APSInt Idx(32); 1786 if (E->isTypeDependent() || E->isValueDependent() || 1787 !E->isIntegerConstantExpr(Idx, S.Context)) { 1788 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1789 << "sentinel" << 1 << E->getSourceRange(); 1790 return; 1791 } 1792 1793 if (Idx.isSigned() && Idx.isNegative()) { 1794 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) 1795 << E->getSourceRange(); 1796 return; 1797 } 1798 1799 sentinel = Idx.getZExtValue(); 1800 } 1801 1802 unsigned nullPos = 0; 1803 if (Attr.getNumArgs() > 1) { 1804 Expr *E = Attr.getArg(1); 1805 llvm::APSInt Idx(32); 1806 if (E->isTypeDependent() || E->isValueDependent() || 1807 !E->isIntegerConstantExpr(Idx, S.Context)) { 1808 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1809 << "sentinel" << 2 << E->getSourceRange(); 1810 return; 1811 } 1812 nullPos = Idx.getZExtValue(); 1813 1814 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { 1815 // FIXME: This error message could be improved, it would be nice 1816 // to say what the bounds actually are. 1817 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) 1818 << E->getSourceRange(); 1819 return; 1820 } 1821 } 1822 1823 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1824 const FunctionType *FT = FD->getType()->castAs<FunctionType>(); 1825 if (isa<FunctionNoProtoType>(FT)) { 1826 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); 1827 return; 1828 } 1829 1830 if (!cast<FunctionProtoType>(FT)->isVariadic()) { 1831 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; 1832 return; 1833 } 1834 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { 1835 if (!MD->isVariadic()) { 1836 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; 1837 return; 1838 } 1839 } else if (isa<BlockDecl>(D)) { 1840 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the 1841 // caller. 1842 ; 1843 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { 1844 QualType Ty = V->getType(); 1845 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { 1846 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) 1847 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); 1848 if (!cast<FunctionProtoType>(FT)->isVariadic()) { 1849 int m = Ty->isFunctionPointerType() ? 0 : 1; 1850 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; 1851 return; 1852 } 1853 } else { 1854 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1855 << Attr.getName() << ExpectedFunctionMethodOrBlock; 1856 return; 1857 } 1858 } else { 1859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1860 << Attr.getName() << ExpectedFunctionMethodOrBlock; 1861 return; 1862 } 1863 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel, 1864 nullPos)); 1865 } 1866 1867 static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { 1868 // check the attribute arguments. 1869 if (!checkAttributeNumArgs(S, Attr, 0)) 1870 return; 1871 1872 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { 1873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1874 << Attr.getName() << ExpectedFunctionOrMethod; 1875 return; 1876 } 1877 1878 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { 1879 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) 1880 << Attr.getName() << 0; 1881 return; 1882 } 1883 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 1884 if (MD->getResultType()->isVoidType()) { 1885 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) 1886 << Attr.getName() << 1; 1887 return; 1888 } 1889 1890 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context)); 1891 } 1892 1893 static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1894 // check the attribute arguments. 1895 if (Attr.hasParameterOrArguments()) { 1896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1897 return; 1898 } 1899 1900 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { 1901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1902 << Attr.getName() << ExpectedVariableOrFunction; 1903 return; 1904 } 1905 1906 NamedDecl *nd = cast<NamedDecl>(D); 1907 1908 // 'weak' only applies to declarations with external linkage. 1909 if (hasEffectivelyInternalLinkage(nd)) { 1910 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); 1911 return; 1912 } 1913 1914 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); 1915 } 1916 1917 static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1918 // check the attribute arguments. 1919 if (!checkAttributeNumArgs(S, Attr, 0)) 1920 return; 1921 1922 1923 // weak_import only applies to variable & function declarations. 1924 bool isDef = false; 1925 if (!D->canBeWeakImported(isDef)) { 1926 if (isDef) 1927 S.Diag(Attr.getLoc(), 1928 diag::warn_attribute_weak_import_invalid_on_definition) 1929 << "weak_import" << 2 /*variable and function*/; 1930 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || 1931 (S.Context.getTargetInfo().getTriple().isOSDarwin() && 1932 isa<ObjCInterfaceDecl>(D))) { 1933 // Nothing to warn about here. 1934 } else 1935 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1936 << Attr.getName() << ExpectedVariableOrFunction; 1937 1938 return; 1939 } 1940 1941 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context)); 1942 } 1943 1944 static void handleReqdWorkGroupSize(Sema &S, Decl *D, 1945 const AttributeList &Attr) { 1946 // Attribute has 3 arguments. 1947 if (!checkAttributeNumArgs(S, Attr, 3)) 1948 return; 1949 1950 unsigned WGSize[3]; 1951 for (unsigned i = 0; i < 3; ++i) { 1952 Expr *E = Attr.getArg(i); 1953 llvm::APSInt ArgNum(32); 1954 if (E->isTypeDependent() || E->isValueDependent() || 1955 !E->isIntegerConstantExpr(ArgNum, S.Context)) { 1956 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 1957 << "reqd_work_group_size" << E->getSourceRange(); 1958 return; 1959 } 1960 WGSize[i] = (unsigned) ArgNum.getZExtValue(); 1961 } 1962 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, 1963 WGSize[0], WGSize[1], 1964 WGSize[2])); 1965 } 1966 1967 static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { 1968 // Attribute has no arguments. 1969 if (!checkAttributeNumArgs(S, Attr, 1)) 1970 return; 1971 1972 // Make sure that there is a string literal as the sections's single 1973 // argument. 1974 Expr *ArgExpr = Attr.getArg(0); 1975 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); 1976 if (!SE) { 1977 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; 1978 return; 1979 } 1980 1981 // If the target wants to validate the section specifier, make it happen. 1982 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); 1983 if (!Error.empty()) { 1984 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) 1985 << Error; 1986 return; 1987 } 1988 1989 // This attribute cannot be applied to local variables. 1990 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { 1991 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); 1992 return; 1993 } 1994 1995 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context, 1996 SE->getString())); 1997 } 1998 1999 2000 static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2001 // check the attribute arguments. 2002 if (Attr.hasParameterOrArguments()) { 2003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2004 return; 2005 } 2006 2007 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { 2008 if (Existing->getLocation().isInvalid()) 2009 Existing->setRange(Attr.getRange()); 2010 } else { 2011 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context)); 2012 } 2013 } 2014 2015 static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2016 // check the attribute arguments. 2017 if (Attr.hasParameterOrArguments()) { 2018 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2019 return; 2020 } 2021 2022 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { 2023 if (Existing->getLocation().isInvalid()) 2024 Existing->setRange(Attr.getRange()); 2025 } else { 2026 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context)); 2027 } 2028 } 2029 2030 static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2031 // check the attribute arguments. 2032 if (!checkAttributeNumArgs(S, Attr, 0)) 2033 return; 2034 2035 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context)); 2036 } 2037 2038 static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2039 if (!Attr.getParameterName()) { 2040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2041 return; 2042 } 2043 2044 if (Attr.getNumArgs() != 0) { 2045 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2046 return; 2047 } 2048 2049 VarDecl *VD = dyn_cast<VarDecl>(D); 2050 2051 if (!VD || !VD->hasLocalStorage()) { 2052 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; 2053 return; 2054 } 2055 2056 // Look up the function 2057 // FIXME: Lookup probably isn't looking in the right place 2058 NamedDecl *CleanupDecl 2059 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), 2060 Attr.getParameterLoc(), Sema::LookupOrdinaryName); 2061 if (!CleanupDecl) { 2062 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << 2063 Attr.getParameterName(); 2064 return; 2065 } 2066 2067 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); 2068 if (!FD) { 2069 S.Diag(Attr.getParameterLoc(), 2070 diag::err_attribute_cleanup_arg_not_function) 2071 << Attr.getParameterName(); 2072 return; 2073 } 2074 2075 if (FD->getNumParams() != 1) { 2076 S.Diag(Attr.getParameterLoc(), 2077 diag::err_attribute_cleanup_func_must_take_one_arg) 2078 << Attr.getParameterName(); 2079 return; 2080 } 2081 2082 // We're currently more strict than GCC about what function types we accept. 2083 // If this ever proves to be a problem it should be easy to fix. 2084 QualType Ty = S.Context.getPointerType(VD->getType()); 2085 QualType ParamTy = FD->getParamDecl(0)->getType(); 2086 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), 2087 ParamTy, Ty) != Sema::Compatible) { 2088 S.Diag(Attr.getParameterLoc(), 2089 diag::err_attribute_cleanup_func_arg_incompatible_type) << 2090 Attr.getParameterName() << ParamTy << Ty; 2091 return; 2092 } 2093 2094 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD)); 2095 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); 2096 } 2097 2098 /// Handle __attribute__((format_arg((idx)))) attribute based on 2099 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 2100 static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2101 if (!checkAttributeNumArgs(S, Attr, 1)) 2102 return; 2103 2104 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { 2105 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2106 << Attr.getName() << ExpectedFunction; 2107 return; 2108 } 2109 2110 // In C++ the implicit 'this' function parameter also counts, and they are 2111 // counted from one. 2112 bool HasImplicitThisParam = isInstanceMethod(D); 2113 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; 2114 unsigned FirstIdx = 1; 2115 2116 // checks for the 2nd argument 2117 Expr *IdxExpr = Attr.getArg(0); 2118 llvm::APSInt Idx(32); 2119 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || 2120 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { 2121 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 2122 << "format" << 2 << IdxExpr->getSourceRange(); 2123 return; 2124 } 2125 2126 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { 2127 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 2128 << "format" << 2 << IdxExpr->getSourceRange(); 2129 return; 2130 } 2131 2132 unsigned ArgIdx = Idx.getZExtValue() - 1; 2133 2134 if (HasImplicitThisParam) { 2135 if (ArgIdx == 0) { 2136 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) 2137 << "format_arg" << IdxExpr->getSourceRange(); 2138 return; 2139 } 2140 ArgIdx--; 2141 } 2142 2143 // make sure the format string is really a string 2144 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); 2145 2146 bool not_nsstring_type = !isNSStringType(Ty, S.Context); 2147 if (not_nsstring_type && 2148 !isCFStringType(Ty, S.Context) && 2149 (!Ty->isPointerType() || 2150 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { 2151 // FIXME: Should highlight the actual expression that has the wrong type. 2152 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 2153 << (not_nsstring_type ? "a string type" : "an NSString") 2154 << IdxExpr->getSourceRange(); 2155 return; 2156 } 2157 Ty = getFunctionOrMethodResultType(D); 2158 if (!isNSStringType(Ty, S.Context) && 2159 !isCFStringType(Ty, S.Context) && 2160 (!Ty->isPointerType() || 2161 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { 2162 // FIXME: Should highlight the actual expression that has the wrong type. 2163 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) 2164 << (not_nsstring_type ? "string type" : "NSString") 2165 << IdxExpr->getSourceRange(); 2166 return; 2167 } 2168 2169 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context, 2170 Idx.getZExtValue())); 2171 } 2172 2173 enum FormatAttrKind { 2174 CFStringFormat, 2175 NSStringFormat, 2176 StrftimeFormat, 2177 SupportedFormat, 2178 IgnoredFormat, 2179 InvalidFormat 2180 }; 2181 2182 /// getFormatAttrKind - Map from format attribute names to supported format 2183 /// types. 2184 static FormatAttrKind getFormatAttrKind(StringRef Format) { 2185 // Check for formats that get handled specially. 2186 if (Format == "NSString") 2187 return NSStringFormat; 2188 if (Format == "CFString") 2189 return CFStringFormat; 2190 if (Format == "strftime") 2191 return StrftimeFormat; 2192 2193 // Otherwise, check for supported formats. 2194 if (Format == "scanf" || Format == "printf" || Format == "printf0" || 2195 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || 2196 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || 2197 Format == "zcmn_err" || 2198 Format == "kprintf") // OpenBSD. 2199 return SupportedFormat; 2200 2201 if (Format == "gcc_diag" || Format == "gcc_cdiag" || 2202 Format == "gcc_cxxdiag" || Format == "gcc_tdiag") 2203 return IgnoredFormat; 2204 2205 return InvalidFormat; 2206 } 2207 2208 /// Handle __attribute__((init_priority(priority))) attributes based on 2209 /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html 2210 static void handleInitPriorityAttr(Sema &S, Decl *D, 2211 const AttributeList &Attr) { 2212 if (!S.getLangOptions().CPlusPlus) { 2213 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 2214 return; 2215 } 2216 2217 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { 2218 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); 2219 Attr.setInvalid(); 2220 return; 2221 } 2222 QualType T = dyn_cast<VarDecl>(D)->getType(); 2223 if (S.Context.getAsArrayType(T)) 2224 T = S.Context.getBaseElementType(T); 2225 if (!T->getAs<RecordType>()) { 2226 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); 2227 Attr.setInvalid(); 2228 return; 2229 } 2230 2231 if (Attr.getNumArgs() != 1) { 2232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2233 Attr.setInvalid(); 2234 return; 2235 } 2236 Expr *priorityExpr = Attr.getArg(0); 2237 2238 llvm::APSInt priority(32); 2239 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || 2240 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { 2241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 2242 << "init_priority" << priorityExpr->getSourceRange(); 2243 Attr.setInvalid(); 2244 return; 2245 } 2246 unsigned prioritynum = priority.getZExtValue(); 2247 if (prioritynum < 101 || prioritynum > 65535) { 2248 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) 2249 << priorityExpr->getSourceRange(); 2250 Attr.setInvalid(); 2251 return; 2252 } 2253 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context, 2254 prioritynum)); 2255 } 2256 2257 /// Handle __attribute__((format(type,idx,firstarg))) attributes based on 2258 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 2259 static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2260 2261 if (!Attr.getParameterName()) { 2262 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 2263 << "format" << 1; 2264 return; 2265 } 2266 2267 if (Attr.getNumArgs() != 2) { 2268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; 2269 return; 2270 } 2271 2272 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { 2273 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2274 << Attr.getName() << ExpectedFunction; 2275 return; 2276 } 2277 2278 // In C++ the implicit 'this' function parameter also counts, and they are 2279 // counted from one. 2280 bool HasImplicitThisParam = isInstanceMethod(D); 2281 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; 2282 unsigned FirstIdx = 1; 2283 2284 StringRef Format = Attr.getParameterName()->getName(); 2285 2286 // Normalize the argument, __foo__ becomes foo. 2287 if (Format.startswith("__") && Format.endswith("__")) 2288 Format = Format.substr(2, Format.size() - 4); 2289 2290 // Check for supported formats. 2291 FormatAttrKind Kind = getFormatAttrKind(Format); 2292 2293 if (Kind == IgnoredFormat) 2294 return; 2295 2296 if (Kind == InvalidFormat) { 2297 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 2298 << "format" << Attr.getParameterName()->getName(); 2299 return; 2300 } 2301 2302 // checks for the 2nd argument 2303 Expr *IdxExpr = Attr.getArg(0); 2304 llvm::APSInt Idx(32); 2305 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || 2306 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { 2307 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 2308 << "format" << 2 << IdxExpr->getSourceRange(); 2309 return; 2310 } 2311 2312 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { 2313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 2314 << "format" << 2 << IdxExpr->getSourceRange(); 2315 return; 2316 } 2317 2318 // FIXME: Do we need to bounds check? 2319 unsigned ArgIdx = Idx.getZExtValue() - 1; 2320 2321 if (HasImplicitThisParam) { 2322 if (ArgIdx == 0) { 2323 S.Diag(Attr.getLoc(), 2324 diag::err_format_attribute_implicit_this_format_string) 2325 << IdxExpr->getSourceRange(); 2326 return; 2327 } 2328 ArgIdx--; 2329 } 2330 2331 // make sure the format string is really a string 2332 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); 2333 2334 if (Kind == CFStringFormat) { 2335 if (!isCFStringType(Ty, S.Context)) { 2336 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 2337 << "a CFString" << IdxExpr->getSourceRange(); 2338 return; 2339 } 2340 } else if (Kind == NSStringFormat) { 2341 // FIXME: do we need to check if the type is NSString*? What are the 2342 // semantics? 2343 if (!isNSStringType(Ty, S.Context)) { 2344 // FIXME: Should highlight the actual expression that has the wrong type. 2345 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 2346 << "an NSString" << IdxExpr->getSourceRange(); 2347 return; 2348 } 2349 } else if (!Ty->isPointerType() || 2350 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { 2351 // FIXME: Should highlight the actual expression that has the wrong type. 2352 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 2353 << "a string type" << IdxExpr->getSourceRange(); 2354 return; 2355 } 2356 2357 // check the 3rd argument 2358 Expr *FirstArgExpr = Attr.getArg(1); 2359 llvm::APSInt FirstArg(32); 2360 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || 2361 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { 2362 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 2363 << "format" << 3 << FirstArgExpr->getSourceRange(); 2364 return; 2365 } 2366 2367 // check if the function is variadic if the 3rd argument non-zero 2368 if (FirstArg != 0) { 2369 if (isFunctionOrMethodVariadic(D)) { 2370 ++NumArgs; // +1 for ... 2371 } else { 2372 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); 2373 return; 2374 } 2375 } 2376 2377 // strftime requires FirstArg to be 0 because it doesn't read from any 2378 // variable the input is just the current time + the format string. 2379 if (Kind == StrftimeFormat) { 2380 if (FirstArg != 0) { 2381 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) 2382 << FirstArgExpr->getSourceRange(); 2383 return; 2384 } 2385 // if 0 it disables parameter checking (to use with e.g. va_list) 2386 } else if (FirstArg != 0 && FirstArg != NumArgs) { 2387 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 2388 << "format" << 3 << FirstArgExpr->getSourceRange(); 2389 return; 2390 } 2391 2392 // Check whether we already have an equivalent format attribute. 2393 for (specific_attr_iterator<FormatAttr> 2394 i = D->specific_attr_begin<FormatAttr>(), 2395 e = D->specific_attr_end<FormatAttr>(); 2396 i != e ; ++i) { 2397 FormatAttr *f = *i; 2398 if (f->getType() == Format && 2399 f->getFormatIdx() == (int)Idx.getZExtValue() && 2400 f->getFirstArg() == (int)FirstArg.getZExtValue()) { 2401 // If we don't have a valid location for this attribute, adopt the 2402 // location. 2403 if (f->getLocation().isInvalid()) 2404 f->setRange(Attr.getRange()); 2405 return; 2406 } 2407 } 2408 2409 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format, 2410 Idx.getZExtValue(), 2411 FirstArg.getZExtValue())); 2412 } 2413 2414 static void handleTransparentUnionAttr(Sema &S, Decl *D, 2415 const AttributeList &Attr) { 2416 // check the attribute arguments. 2417 if (!checkAttributeNumArgs(S, Attr, 0)) 2418 return; 2419 2420 2421 // Try to find the underlying union declaration. 2422 RecordDecl *RD = 0; 2423 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); 2424 if (TD && TD->getUnderlyingType()->isUnionType()) 2425 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); 2426 else 2427 RD = dyn_cast<RecordDecl>(D); 2428 2429 if (!RD || !RD->isUnion()) { 2430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2431 << Attr.getName() << ExpectedUnion; 2432 return; 2433 } 2434 2435 if (!RD->isDefinition()) { 2436 S.Diag(Attr.getLoc(), 2437 diag::warn_transparent_union_attribute_not_definition); 2438 return; 2439 } 2440 2441 RecordDecl::field_iterator Field = RD->field_begin(), 2442 FieldEnd = RD->field_end(); 2443 if (Field == FieldEnd) { 2444 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); 2445 return; 2446 } 2447 2448 FieldDecl *FirstField = *Field; 2449 QualType FirstType = FirstField->getType(); 2450 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { 2451 S.Diag(FirstField->getLocation(), 2452 diag::warn_transparent_union_attribute_floating) 2453 << FirstType->isVectorType() << FirstType; 2454 return; 2455 } 2456 2457 uint64_t FirstSize = S.Context.getTypeSize(FirstType); 2458 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); 2459 for (; Field != FieldEnd; ++Field) { 2460 QualType FieldType = Field->getType(); 2461 if (S.Context.getTypeSize(FieldType) != FirstSize || 2462 S.Context.getTypeAlign(FieldType) != FirstAlign) { 2463 // Warn if we drop the attribute. 2464 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; 2465 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) 2466 : S.Context.getTypeAlign(FieldType); 2467 S.Diag(Field->getLocation(), 2468 diag::warn_transparent_union_attribute_field_size_align) 2469 << isSize << Field->getDeclName() << FieldBits; 2470 unsigned FirstBits = isSize? FirstSize : FirstAlign; 2471 S.Diag(FirstField->getLocation(), 2472 diag::note_transparent_union_first_field_size_align) 2473 << isSize << FirstBits; 2474 return; 2475 } 2476 } 2477 2478 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context)); 2479 } 2480 2481 static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2482 // check the attribute arguments. 2483 if (!checkAttributeNumArgs(S, Attr, 1)) 2484 return; 2485 2486 Expr *ArgExpr = Attr.getArg(0); 2487 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); 2488 2489 // Make sure that there is a string literal as the annotation's single 2490 // argument. 2491 if (!SE) { 2492 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; 2493 return; 2494 } 2495 2496 // Don't duplicate annotations that are already set. 2497 for (specific_attr_iterator<AnnotateAttr> 2498 i = D->specific_attr_begin<AnnotateAttr>(), 2499 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { 2500 if ((*i)->getAnnotation() == SE->getString()) 2501 return; 2502 } 2503 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context, 2504 SE->getString())); 2505 } 2506 2507 static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2508 // check the attribute arguments. 2509 if (Attr.getNumArgs() > 1) { 2510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2511 return; 2512 } 2513 2514 //FIXME: The C++0x version of this attribute has more limited applicabilty 2515 // than GNU's, and should error out when it is used to specify a 2516 // weaker alignment, rather than being silently ignored. 2517 2518 if (Attr.getNumArgs() == 0) { 2519 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0)); 2520 return; 2521 } 2522 2523 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0)); 2524 } 2525 2526 void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) { 2527 if (E->isTypeDependent() || E->isValueDependent()) { 2528 // Save dependent expressions in the AST to be instantiated. 2529 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E)); 2530 return; 2531 } 2532 2533 SourceLocation AttrLoc = AttrRange.getBegin(); 2534 // FIXME: Cache the number on the Attr object? 2535 llvm::APSInt Alignment(32); 2536 if (!E->isIntegerConstantExpr(Alignment, Context)) { 2537 Diag(AttrLoc, diag::err_attribute_argument_not_int) 2538 << "aligned" << E->getSourceRange(); 2539 return; 2540 } 2541 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { 2542 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) 2543 << E->getSourceRange(); 2544 return; 2545 } 2546 2547 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E)); 2548 } 2549 2550 void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) { 2551 // FIXME: Cache the number on the Attr object if non-dependent? 2552 // FIXME: Perform checking of type validity 2553 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS)); 2554 return; 2555 } 2556 2557 /// handleModeAttr - This attribute modifies the width of a decl with primitive 2558 /// type. 2559 /// 2560 /// Despite what would be logical, the mode attribute is a decl attribute, not a 2561 /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be 2562 /// HImode, not an intermediate pointer. 2563 static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2564 // This attribute isn't documented, but glibc uses it. It changes 2565 // the width of an int or unsigned int to the specified size. 2566 2567 // Check that there aren't any arguments 2568 if (!checkAttributeNumArgs(S, Attr, 0)) 2569 return; 2570 2571 2572 IdentifierInfo *Name = Attr.getParameterName(); 2573 if (!Name) { 2574 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); 2575 return; 2576 } 2577 2578 StringRef Str = Attr.getParameterName()->getName(); 2579 2580 // Normalize the attribute name, __foo__ becomes foo. 2581 if (Str.startswith("__") && Str.endswith("__")) 2582 Str = Str.substr(2, Str.size() - 4); 2583 2584 unsigned DestWidth = 0; 2585 bool IntegerMode = true; 2586 bool ComplexMode = false; 2587 switch (Str.size()) { 2588 case 2: 2589 switch (Str[0]) { 2590 case 'Q': DestWidth = 8; break; 2591 case 'H': DestWidth = 16; break; 2592 case 'S': DestWidth = 32; break; 2593 case 'D': DestWidth = 64; break; 2594 case 'X': DestWidth = 96; break; 2595 case 'T': DestWidth = 128; break; 2596 } 2597 if (Str[1] == 'F') { 2598 IntegerMode = false; 2599 } else if (Str[1] == 'C') { 2600 IntegerMode = false; 2601 ComplexMode = true; 2602 } else if (Str[1] != 'I') { 2603 DestWidth = 0; 2604 } 2605 break; 2606 case 4: 2607 // FIXME: glibc uses 'word' to define register_t; this is narrower than a 2608 // pointer on PIC16 and other embedded platforms. 2609 if (Str == "word") 2610 DestWidth = S.Context.getTargetInfo().getPointerWidth(0); 2611 else if (Str == "byte") 2612 DestWidth = S.Context.getTargetInfo().getCharWidth(); 2613 break; 2614 case 7: 2615 if (Str == "pointer") 2616 DestWidth = S.Context.getTargetInfo().getPointerWidth(0); 2617 break; 2618 } 2619 2620 QualType OldTy; 2621 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) 2622 OldTy = TD->getUnderlyingType(); 2623 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) 2624 OldTy = VD->getType(); 2625 else { 2626 S.Diag(D->getLocation(), diag::err_attr_wrong_decl) 2627 << "mode" << Attr.getRange(); 2628 return; 2629 } 2630 2631 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) 2632 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); 2633 else if (IntegerMode) { 2634 if (!OldTy->isIntegralOrEnumerationType()) 2635 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 2636 } else if (ComplexMode) { 2637 if (!OldTy->isComplexType()) 2638 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 2639 } else { 2640 if (!OldTy->isFloatingType()) 2641 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 2642 } 2643 2644 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t 2645 // and friends, at least with glibc. 2646 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong 2647 // width on unusual platforms. 2648 // FIXME: Make sure floating-point mappings are accurate 2649 // FIXME: Support XF and TF types 2650 QualType NewTy; 2651 switch (DestWidth) { 2652 case 0: 2653 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; 2654 return; 2655 default: 2656 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2657 return; 2658 case 8: 2659 if (!IntegerMode) { 2660 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2661 return; 2662 } 2663 if (OldTy->isSignedIntegerType()) 2664 NewTy = S.Context.SignedCharTy; 2665 else 2666 NewTy = S.Context.UnsignedCharTy; 2667 break; 2668 case 16: 2669 if (!IntegerMode) { 2670 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2671 return; 2672 } 2673 if (OldTy->isSignedIntegerType()) 2674 NewTy = S.Context.ShortTy; 2675 else 2676 NewTy = S.Context.UnsignedShortTy; 2677 break; 2678 case 32: 2679 if (!IntegerMode) 2680 NewTy = S.Context.FloatTy; 2681 else if (OldTy->isSignedIntegerType()) 2682 NewTy = S.Context.IntTy; 2683 else 2684 NewTy = S.Context.UnsignedIntTy; 2685 break; 2686 case 64: 2687 if (!IntegerMode) 2688 NewTy = S.Context.DoubleTy; 2689 else if (OldTy->isSignedIntegerType()) 2690 if (S.Context.getTargetInfo().getLongWidth() == 64) 2691 NewTy = S.Context.LongTy; 2692 else 2693 NewTy = S.Context.LongLongTy; 2694 else 2695 if (S.Context.getTargetInfo().getLongWidth() == 64) 2696 NewTy = S.Context.UnsignedLongTy; 2697 else 2698 NewTy = S.Context.UnsignedLongLongTy; 2699 break; 2700 case 96: 2701 NewTy = S.Context.LongDoubleTy; 2702 break; 2703 case 128: 2704 if (!IntegerMode) { 2705 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2706 return; 2707 } 2708 if (OldTy->isSignedIntegerType()) 2709 NewTy = S.Context.Int128Ty; 2710 else 2711 NewTy = S.Context.UnsignedInt128Ty; 2712 break; 2713 } 2714 2715 if (ComplexMode) { 2716 NewTy = S.Context.getComplexType(NewTy); 2717 } 2718 2719 // Install the new type. 2720 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { 2721 // FIXME: preserve existing source info. 2722 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); 2723 } else 2724 cast<ValueDecl>(D)->setType(NewTy); 2725 } 2726 2727 static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2728 // check the attribute arguments. 2729 if (!checkAttributeNumArgs(S, Attr, 0)) 2730 return; 2731 2732 if (!isFunctionOrMethod(D)) { 2733 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2734 << Attr.getName() << ExpectedFunction; 2735 return; 2736 } 2737 2738 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context)); 2739 } 2740 2741 static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2742 // check the attribute arguments. 2743 if (!checkAttributeNumArgs(S, Attr, 0)) 2744 return; 2745 2746 2747 if (!isa<FunctionDecl>(D)) { 2748 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2749 << Attr.getName() << ExpectedFunction; 2750 return; 2751 } 2752 2753 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context)); 2754 } 2755 2756 static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, 2757 const AttributeList &Attr) { 2758 // check the attribute arguments. 2759 if (!checkAttributeNumArgs(S, Attr, 0)) 2760 return; 2761 2762 2763 if (!isa<FunctionDecl>(D)) { 2764 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2765 << Attr.getName() << ExpectedFunction; 2766 return; 2767 } 2768 2769 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(), 2770 S.Context)); 2771 } 2772 2773 static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2774 if (S.LangOpts.CUDA) { 2775 // check the attribute arguments. 2776 if (Attr.hasParameterOrArguments()) { 2777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2778 return; 2779 } 2780 2781 if (!isa<VarDecl>(D)) { 2782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2783 << Attr.getName() << ExpectedVariable; 2784 return; 2785 } 2786 2787 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context)); 2788 } else { 2789 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; 2790 } 2791 } 2792 2793 static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2794 if (S.LangOpts.CUDA) { 2795 // check the attribute arguments. 2796 if (Attr.getNumArgs() != 0) { 2797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2798 return; 2799 } 2800 2801 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { 2802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2803 << Attr.getName() << ExpectedVariableOrFunction; 2804 return; 2805 } 2806 2807 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context)); 2808 } else { 2809 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; 2810 } 2811 } 2812 2813 static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2814 if (S.LangOpts.CUDA) { 2815 // check the attribute arguments. 2816 if (!checkAttributeNumArgs(S, Attr, 0)) 2817 return; 2818 2819 if (!isa<FunctionDecl>(D)) { 2820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2821 << Attr.getName() << ExpectedFunction; 2822 return; 2823 } 2824 2825 FunctionDecl *FD = cast<FunctionDecl>(D); 2826 if (!FD->getResultType()->isVoidType()) { 2827 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); 2828 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { 2829 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) 2830 << FD->getType() 2831 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), 2832 "void"); 2833 } else { 2834 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) 2835 << FD->getType(); 2836 } 2837 return; 2838 } 2839 2840 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context)); 2841 } else { 2842 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; 2843 } 2844 } 2845 2846 static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2847 if (S.LangOpts.CUDA) { 2848 // check the attribute arguments. 2849 if (!checkAttributeNumArgs(S, Attr, 0)) 2850 return; 2851 2852 2853 if (!isa<FunctionDecl>(D)) { 2854 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2855 << Attr.getName() << ExpectedFunction; 2856 return; 2857 } 2858 2859 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context)); 2860 } else { 2861 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; 2862 } 2863 } 2864 2865 static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2866 if (S.LangOpts.CUDA) { 2867 // check the attribute arguments. 2868 if (!checkAttributeNumArgs(S, Attr, 0)) 2869 return; 2870 2871 2872 if (!isa<VarDecl>(D)) { 2873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2874 << Attr.getName() << ExpectedVariable; 2875 return; 2876 } 2877 2878 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context)); 2879 } else { 2880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; 2881 } 2882 } 2883 2884 static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2885 // check the attribute arguments. 2886 if (!checkAttributeNumArgs(S, Attr, 0)) 2887 return; 2888 2889 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); 2890 if (Fn == 0) { 2891 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2892 << Attr.getName() << ExpectedFunction; 2893 return; 2894 } 2895 2896 if (!Fn->isInlineSpecified()) { 2897 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); 2898 return; 2899 } 2900 2901 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context)); 2902 } 2903 2904 static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { 2905 if (hasDeclarator(D)) return; 2906 2907 // Diagnostic is emitted elsewhere: here we store the (valid) Attr 2908 // in the Decl node for syntactic reasoning, e.g., pretty-printing. 2909 CallingConv CC; 2910 if (S.CheckCallingConvAttr(Attr, CC)) 2911 return; 2912 2913 if (!isa<ObjCMethodDecl>(D)) { 2914 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2915 << Attr.getName() << ExpectedFunctionOrMethod; 2916 return; 2917 } 2918 2919 switch (Attr.getKind()) { 2920 case AttributeList::AT_fastcall: 2921 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context)); 2922 return; 2923 case AttributeList::AT_stdcall: 2924 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context)); 2925 return; 2926 case AttributeList::AT_thiscall: 2927 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context)); 2928 return; 2929 case AttributeList::AT_cdecl: 2930 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context)); 2931 return; 2932 case AttributeList::AT_pascal: 2933 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context)); 2934 return; 2935 case AttributeList::AT_pcs: { 2936 Expr *Arg = Attr.getArg(0); 2937 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 2938 if (!Str || !Str->isAscii()) { 2939 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 2940 << "pcs" << 1; 2941 Attr.setInvalid(); 2942 return; 2943 } 2944 2945 StringRef StrRef = Str->getString(); 2946 PcsAttr::PCSType PCS; 2947 if (StrRef == "aapcs") 2948 PCS = PcsAttr::AAPCS; 2949 else if (StrRef == "aapcs-vfp") 2950 PCS = PcsAttr::AAPCS_VFP; 2951 else { 2952 S.Diag(Attr.getLoc(), diag::err_invalid_pcs); 2953 Attr.setInvalid(); 2954 return; 2955 } 2956 2957 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS)); 2958 } 2959 default: 2960 llvm_unreachable("unexpected attribute kind"); 2961 return; 2962 } 2963 } 2964 2965 static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ 2966 assert(!Attr.isInvalid()); 2967 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); 2968 } 2969 2970 bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { 2971 if (attr.isInvalid()) 2972 return true; 2973 2974 if ((attr.getNumArgs() != 0 && 2975 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) || 2976 attr.getParameterName()) { 2977 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2978 attr.setInvalid(); 2979 return true; 2980 } 2981 2982 // TODO: diagnose uses of these conventions on the wrong target. Or, better 2983 // move to TargetAttributesSema one day. 2984 switch (attr.getKind()) { 2985 case AttributeList::AT_cdecl: CC = CC_C; break; 2986 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; 2987 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; 2988 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; 2989 case AttributeList::AT_pascal: CC = CC_X86Pascal; break; 2990 case AttributeList::AT_pcs: { 2991 Expr *Arg = attr.getArg(0); 2992 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 2993 if (!Str || !Str->isAscii()) { 2994 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) 2995 << "pcs" << 1; 2996 attr.setInvalid(); 2997 return true; 2998 } 2999 3000 StringRef StrRef = Str->getString(); 3001 if (StrRef == "aapcs") { 3002 CC = CC_AAPCS; 3003 break; 3004 } else if (StrRef == "aapcs-vfp") { 3005 CC = CC_AAPCS_VFP; 3006 break; 3007 } 3008 // FALLS THROUGH 3009 } 3010 default: llvm_unreachable("unexpected attribute kind"); return true; 3011 } 3012 3013 return false; 3014 } 3015 3016 static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { 3017 if (hasDeclarator(D)) return; 3018 3019 unsigned numParams; 3020 if (S.CheckRegparmAttr(Attr, numParams)) 3021 return; 3022 3023 if (!isa<ObjCMethodDecl>(D)) { 3024 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 3025 << Attr.getName() << ExpectedFunctionOrMethod; 3026 return; 3027 } 3028 3029 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams)); 3030 } 3031 3032 /// Checks a regparm attribute, returning true if it is ill-formed and 3033 /// otherwise setting numParams to the appropriate value. 3034 bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { 3035 if (Attr.isInvalid()) 3036 return true; 3037 3038 if (Attr.getNumArgs() != 1) { 3039 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 3040 Attr.setInvalid(); 3041 return true; 3042 } 3043 3044 Expr *NumParamsExpr = Attr.getArg(0); 3045 llvm::APSInt NumParams(32); 3046 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || 3047 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { 3048 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 3049 << "regparm" << NumParamsExpr->getSourceRange(); 3050 Attr.setInvalid(); 3051 return true; 3052 } 3053 3054 if (Context.getTargetInfo().getRegParmMax() == 0) { 3055 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) 3056 << NumParamsExpr->getSourceRange(); 3057 Attr.setInvalid(); 3058 return true; 3059 } 3060 3061 numParams = NumParams.getZExtValue(); 3062 if (numParams > Context.getTargetInfo().getRegParmMax()) { 3063 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) 3064 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); 3065 Attr.setInvalid(); 3066 return true; 3067 } 3068 3069 return false; 3070 } 3071 3072 static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ 3073 if (S.LangOpts.CUDA) { 3074 // check the attribute arguments. 3075 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { 3076 // FIXME: 0 is not okay. 3077 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; 3078 return; 3079 } 3080 3081 if (!isFunctionOrMethod(D)) { 3082 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 3083 << Attr.getName() << ExpectedFunctionOrMethod; 3084 return; 3085 } 3086 3087 Expr *MaxThreadsExpr = Attr.getArg(0); 3088 llvm::APSInt MaxThreads(32); 3089 if (MaxThreadsExpr->isTypeDependent() || 3090 MaxThreadsExpr->isValueDependent() || 3091 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { 3092 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 3093 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); 3094 return; 3095 } 3096 3097 llvm::APSInt MinBlocks(32); 3098 if (Attr.getNumArgs() > 1) { 3099 Expr *MinBlocksExpr = Attr.getArg(1); 3100 if (MinBlocksExpr->isTypeDependent() || 3101 MinBlocksExpr->isValueDependent() || 3102 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { 3103 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 3104 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); 3105 return; 3106 } 3107 } 3108 3109 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context, 3110 MaxThreads.getZExtValue(), 3111 MinBlocks.getZExtValue())); 3112 } else { 3113 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; 3114 } 3115 } 3116 3117 //===----------------------------------------------------------------------===// 3118 // Checker-specific attribute handlers. 3119 //===----------------------------------------------------------------------===// 3120 3121 static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { 3122 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); 3123 } 3124 static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { 3125 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); 3126 } 3127 3128 static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { 3129 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); 3130 if (!param) { 3131 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) 3132 << Attr.getRange() << Attr.getName() << ExpectedParameter; 3133 return; 3134 } 3135 3136 bool typeOK, cf; 3137 if (Attr.getKind() == AttributeList::AT_ns_consumed) { 3138 typeOK = isValidSubjectOfNSAttribute(S, param->getType()); 3139 cf = false; 3140 } else { 3141 typeOK = isValidSubjectOfCFAttribute(S, param->getType()); 3142 cf = true; 3143 } 3144 3145 if (!typeOK) { 3146 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) 3147 << Attr.getRange() << Attr.getName() << cf; 3148 return; 3149 } 3150 3151 if (cf) 3152 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context)); 3153 else 3154 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context)); 3155 } 3156 3157 static void handleNSConsumesSelfAttr(Sema &S, Decl *D, 3158 const AttributeList &Attr) { 3159 if (!isa<ObjCMethodDecl>(D)) { 3160 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) 3161 << Attr.getRange() << Attr.getName() << ExpectedMethod; 3162 return; 3163 } 3164 3165 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context)); 3166 } 3167 3168 static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, 3169 const AttributeList &Attr) { 3170 3171 QualType returnType; 3172 3173 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 3174 returnType = MD->getResultType(); 3175 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) 3176 returnType = PD->getType(); 3177 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) && 3178 (Attr.getKind() == AttributeList::AT_ns_returns_retained)) 3179 return; // ignore: was handled as a type attribute 3180 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 3181 returnType = FD->getResultType(); 3182 else { 3183 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) 3184 << Attr.getRange() << Attr.getName() 3185 << ExpectedFunctionOrMethod; 3186 return; 3187 } 3188 3189 bool typeOK; 3190 bool cf; 3191 switch (Attr.getKind()) { 3192 default: llvm_unreachable("invalid ownership attribute"); return; 3193 case AttributeList::AT_ns_returns_autoreleased: 3194 case AttributeList::AT_ns_returns_retained: 3195 case AttributeList::AT_ns_returns_not_retained: 3196 typeOK = isValidSubjectOfNSAttribute(S, returnType); 3197 cf = false; 3198 break; 3199 3200 case AttributeList::AT_cf_returns_retained: 3201 case AttributeList::AT_cf_returns_not_retained: 3202 typeOK = isValidSubjectOfCFAttribute(S, returnType); 3203 cf = true; 3204 break; 3205 } 3206 3207 if (!typeOK) { 3208 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) 3209 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; 3210 return; 3211 } 3212 3213 switch (Attr.getKind()) { 3214 default: 3215 llvm_unreachable("invalid ownership attribute"); 3216 case AttributeList::AT_ns_returns_autoreleased: 3217 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(), 3218 S.Context)); 3219 return; 3220 case AttributeList::AT_cf_returns_not_retained: 3221 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(), 3222 S.Context)); 3223 return; 3224 case AttributeList::AT_ns_returns_not_retained: 3225 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(), 3226 S.Context)); 3227 return; 3228 case AttributeList::AT_cf_returns_retained: 3229 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(), 3230 S.Context)); 3231 return; 3232 case AttributeList::AT_ns_returns_retained: 3233 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(), 3234 S.Context)); 3235 return; 3236 }; 3237 } 3238 3239 static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, 3240 const AttributeList &attr) { 3241 SourceLocation loc = attr.getLoc(); 3242 3243 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); 3244 3245 if (!isa<ObjCMethodDecl>(method)) { 3246 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type) 3247 << SourceRange(loc, loc) << attr.getName() << 13 /* methods */; 3248 return; 3249 } 3250 3251 // Check that the method returns a normal pointer. 3252 QualType resultType = method->getResultType(); 3253 3254 if (!resultType->isReferenceType() && 3255 (!resultType->isPointerType() || resultType->isObjCRetainableType())) { 3256 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) 3257 << SourceRange(loc) 3258 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; 3259 3260 // Drop the attribute. 3261 return; 3262 } 3263 3264 method->addAttr( 3265 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context)); 3266 } 3267 3268 /// Handle cf_audited_transfer and cf_unknown_transfer. 3269 static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { 3270 if (!isa<FunctionDecl>(D)) { 3271 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) 3272 << A.getRange() << A.getName() << 0 /*function*/; 3273 return; 3274 } 3275 3276 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer); 3277 3278 // Check whether there's a conflicting attribute already present. 3279 Attr *Existing; 3280 if (IsAudited) { 3281 Existing = D->getAttr<CFUnknownTransferAttr>(); 3282 } else { 3283 Existing = D->getAttr<CFAuditedTransferAttr>(); 3284 } 3285 if (Existing) { 3286 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) 3287 << A.getName() 3288 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") 3289 << A.getRange() << Existing->getRange(); 3290 return; 3291 } 3292 3293 // All clear; add the attribute. 3294 if (IsAudited) { 3295 D->addAttr( 3296 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context)); 3297 } else { 3298 D->addAttr( 3299 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context)); 3300 } 3301 } 3302 3303 static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, 3304 const AttributeList &Attr) { 3305 RecordDecl *RD = dyn_cast<RecordDecl>(D); 3306 if (!RD || RD->isUnion()) { 3307 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) 3308 << Attr.getRange() << Attr.getName() << 14 /*struct */; 3309 } 3310 3311 IdentifierInfo *ParmName = Attr.getParameterName(); 3312 3313 // In Objective-C, verify that the type names an Objective-C type. 3314 // We don't want to check this outside of ObjC because people sometimes 3315 // do crazy C declarations of Objective-C types. 3316 if (ParmName && S.getLangOptions().ObjC1) { 3317 // Check for an existing type with this name. 3318 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), 3319 Sema::LookupOrdinaryName); 3320 if (S.LookupName(R, Sc)) { 3321 NamedDecl *Target = R.getFoundDecl(); 3322 if (Target && !isa<ObjCInterfaceDecl>(Target)) { 3323 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); 3324 S.Diag(Target->getLocStart(), diag::note_declared_at); 3325 } 3326 } 3327 } 3328 3329 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context, 3330 ParmName)); 3331 } 3332 3333 static void handleObjCOwnershipAttr(Sema &S, Decl *D, 3334 const AttributeList &Attr) { 3335 if (hasDeclarator(D)) return; 3336 3337 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) 3338 << Attr.getRange() << Attr.getName() << 12 /* variable */; 3339 } 3340 3341 static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, 3342 const AttributeList &Attr) { 3343 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { 3344 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) 3345 << Attr.getRange() << Attr.getName() << 12 /* variable */; 3346 return; 3347 } 3348 3349 ValueDecl *vd = cast<ValueDecl>(D); 3350 QualType type = vd->getType(); 3351 3352 if (!type->isDependentType() && 3353 !type->isObjCLifetimeType()) { 3354 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) 3355 << type; 3356 return; 3357 } 3358 3359 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); 3360 3361 // If we have no lifetime yet, check the lifetime we're presumably 3362 // going to infer. 3363 if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) 3364 lifetime = type->getObjCARCImplicitLifetime(); 3365 3366 switch (lifetime) { 3367 case Qualifiers::OCL_None: 3368 assert(type->isDependentType() && 3369 "didn't infer lifetime for non-dependent type?"); 3370 break; 3371 3372 case Qualifiers::OCL_Weak: // meaningful 3373 case Qualifiers::OCL_Strong: // meaningful 3374 break; 3375 3376 case Qualifiers::OCL_ExplicitNone: 3377 case Qualifiers::OCL_Autoreleasing: 3378 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) 3379 << (lifetime == Qualifiers::OCL_Autoreleasing); 3380 break; 3381 } 3382 3383 D->addAttr(::new (S.Context) 3384 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context)); 3385 } 3386 3387 static bool isKnownDeclSpecAttr(const AttributeList &Attr) { 3388 return Attr.getKind() == AttributeList::AT_dllimport || 3389 Attr.getKind() == AttributeList::AT_dllexport || 3390 Attr.getKind() == AttributeList::AT_uuid; 3391 } 3392 3393 //===----------------------------------------------------------------------===// 3394 // Microsoft specific attribute handlers. 3395 //===----------------------------------------------------------------------===// 3396 3397 static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { 3398 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { 3399 // check the attribute arguments. 3400 if (!checkAttributeNumArgs(S, Attr, 1)) 3401 return; 3402 3403 Expr *Arg = Attr.getArg(0); 3404 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 3405 if (!Str || !Str->isAscii()) { 3406 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 3407 << "uuid" << 1; 3408 return; 3409 } 3410 3411 StringRef StrRef = Str->getString(); 3412 3413 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && 3414 StrRef.back() == '}'; 3415 3416 // Validate GUID length. 3417 if (IsCurly && StrRef.size() != 38) { 3418 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 3419 return; 3420 } 3421 if (!IsCurly && StrRef.size() != 36) { 3422 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 3423 return; 3424 } 3425 3426 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or 3427 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" 3428 StringRef::iterator I = StrRef.begin(); 3429 if (IsCurly) // Skip the optional '{' 3430 ++I; 3431 3432 for (int i = 0; i < 36; ++i) { 3433 if (i == 8 || i == 13 || i == 18 || i == 23) { 3434 if (*I != '-') { 3435 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 3436 return; 3437 } 3438 } else if (!isxdigit(*I)) { 3439 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 3440 return; 3441 } 3442 I++; 3443 } 3444 3445 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, 3446 Str->getString())); 3447 } else 3448 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; 3449 } 3450 3451 //===----------------------------------------------------------------------===// 3452 // Top Level Sema Entry Points 3453 //===----------------------------------------------------------------------===// 3454 3455 static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, 3456 const AttributeList &Attr) { 3457 switch (Attr.getKind()) { 3458 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break; 3459 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break; 3460 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break; 3461 default: 3462 break; 3463 } 3464 } 3465 3466 static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, 3467 const AttributeList &Attr) { 3468 switch (Attr.getKind()) { 3469 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; 3470 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; 3471 case AttributeList::AT_IBOutletCollection: 3472 handleIBOutletCollection(S, D, Attr); break; 3473 case AttributeList::AT_address_space: 3474 case AttributeList::AT_opencl_image_access: 3475 case AttributeList::AT_objc_gc: 3476 case AttributeList::AT_vector_size: 3477 case AttributeList::AT_neon_vector_type: 3478 case AttributeList::AT_neon_polyvector_type: 3479 // Ignore these, these are type attributes, handled by 3480 // ProcessTypeAttributes. 3481 break; 3482 case AttributeList::AT_device: 3483 case AttributeList::AT_host: 3484 case AttributeList::AT_overloadable: 3485 // Ignore, this is a non-inheritable attribute, handled 3486 // by ProcessNonInheritableDeclAttr. 3487 break; 3488 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break; 3489 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break; 3490 case AttributeList::AT_always_inline: 3491 handleAlwaysInlineAttr (S, D, Attr); break; 3492 case AttributeList::AT_analyzer_noreturn: 3493 handleAnalyzerNoReturnAttr (S, D, Attr); break; 3494 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break; 3495 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break; 3496 case AttributeList::AT_carries_dependency: 3497 handleDependencyAttr (S, D, Attr); break; 3498 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break; 3499 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break; 3500 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break; 3501 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break; 3502 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break; 3503 case AttributeList::AT_ext_vector_type: 3504 handleExtVectorTypeAttr(S, scope, D, Attr); 3505 break; 3506 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break; 3507 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break; 3508 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break; 3509 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break; 3510 case AttributeList::AT_launch_bounds: 3511 handleLaunchBoundsAttr(S, D, Attr); 3512 break; 3513 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break; 3514 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break; 3515 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break; 3516 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break; 3517 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break; 3518 case AttributeList::AT_ownership_returns: 3519 case AttributeList::AT_ownership_takes: 3520 case AttributeList::AT_ownership_holds: 3521 handleOwnershipAttr (S, D, Attr); break; 3522 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break; 3523 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break; 3524 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break; 3525 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break; 3526 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break; 3527 3528 case AttributeList::AT_objc_ownership: 3529 handleObjCOwnershipAttr(S, D, Attr); break; 3530 case AttributeList::AT_objc_precise_lifetime: 3531 handleObjCPreciseLifetimeAttr(S, D, Attr); break; 3532 3533 case AttributeList::AT_objc_returns_inner_pointer: 3534 handleObjCReturnsInnerPointerAttr(S, D, Attr); break; 3535 3536 case AttributeList::AT_ns_bridged: 3537 handleNSBridgedAttr(S, scope, D, Attr); break; 3538 3539 case AttributeList::AT_cf_audited_transfer: 3540 case AttributeList::AT_cf_unknown_transfer: 3541 handleCFTransferAttr(S, D, Attr); break; 3542 3543 // Checker-specific. 3544 case AttributeList::AT_cf_consumed: 3545 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break; 3546 case AttributeList::AT_ns_consumes_self: 3547 handleNSConsumesSelfAttr(S, D, Attr); break; 3548 3549 case AttributeList::AT_ns_returns_autoreleased: 3550 case AttributeList::AT_ns_returns_not_retained: 3551 case AttributeList::AT_cf_returns_not_retained: 3552 case AttributeList::AT_ns_returns_retained: 3553 case AttributeList::AT_cf_returns_retained: 3554 handleNSReturnsRetainedAttr(S, D, Attr); break; 3555 3556 case AttributeList::AT_reqd_wg_size: 3557 handleReqdWorkGroupSize(S, D, Attr); break; 3558 3559 case AttributeList::AT_init_priority: 3560 handleInitPriorityAttr(S, D, Attr); break; 3561 3562 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break; 3563 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break; 3564 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break; 3565 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break; 3566 case AttributeList::AT_arc_weakref_unavailable: 3567 handleArcWeakrefUnavailableAttr (S, D, Attr); 3568 break; 3569 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break; 3570 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break; 3571 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break; 3572 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr); 3573 break; 3574 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break; 3575 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break; 3576 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break; 3577 case AttributeList::AT_transparent_union: 3578 handleTransparentUnionAttr(S, D, Attr); 3579 break; 3580 case AttributeList::AT_objc_exception: 3581 handleObjCExceptionAttr(S, D, Attr); 3582 break; 3583 case AttributeList::AT_objc_method_family: 3584 handleObjCMethodFamilyAttr(S, D, Attr); 3585 break; 3586 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break; 3587 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break; 3588 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break; 3589 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break; 3590 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break; 3591 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break; 3592 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break; 3593 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break; 3594 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break; 3595 case AttributeList::IgnoredAttribute: 3596 // Just ignore 3597 break; 3598 case AttributeList::AT_no_instrument_function: // Interacts with -pg. 3599 handleNoInstrumentFunctionAttr(S, D, Attr); 3600 break; 3601 case AttributeList::AT_stdcall: 3602 case AttributeList::AT_cdecl: 3603 case AttributeList::AT_fastcall: 3604 case AttributeList::AT_thiscall: 3605 case AttributeList::AT_pascal: 3606 case AttributeList::AT_pcs: 3607 handleCallConvAttr(S, D, Attr); 3608 break; 3609 case AttributeList::AT_opencl_kernel_function: 3610 handleOpenCLKernelAttr(S, D, Attr); 3611 break; 3612 case AttributeList::AT_uuid: 3613 handleUuidAttr(S, D, Attr); 3614 break; 3615 3616 // Thread safety attributes: 3617 case AttributeList::AT_guarded_var: 3618 handleGuardedVarAttr(S, D, Attr); 3619 break; 3620 case AttributeList::AT_pt_guarded_var: 3621 handleGuardedVarAttr(S, D, Attr, /*pointer = */true); 3622 break; 3623 case AttributeList::AT_scoped_lockable: 3624 handleLockableAttr(S, D, Attr, /*scoped = */true); 3625 break; 3626 case AttributeList::AT_no_thread_safety_analysis: 3627 handleNoThreadSafetyAttr(S, D, Attr); 3628 break; 3629 case AttributeList::AT_lockable: 3630 handleLockableAttr(S, D, Attr); 3631 break; 3632 case AttributeList::AT_guarded_by: 3633 handleGuardedByAttr(S, D, Attr); 3634 break; 3635 case AttributeList::AT_pt_guarded_by: 3636 handleGuardedByAttr(S, D, Attr, /*pointer = */true); 3637 break; 3638 case AttributeList::AT_exclusive_lock_function: 3639 handleLockFunAttr(S, D, Attr, /*exclusive = */true); 3640 break; 3641 case AttributeList::AT_exclusive_locks_required: 3642 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true); 3643 break; 3644 case AttributeList::AT_exclusive_trylock_function: 3645 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true); 3646 break; 3647 case AttributeList::AT_lock_returned: 3648 handleLockReturnedAttr(S, D, Attr); 3649 break; 3650 case AttributeList::AT_locks_excluded: 3651 handleLocksExcludedAttr(S, D, Attr); 3652 break; 3653 case AttributeList::AT_shared_lock_function: 3654 handleLockFunAttr(S, D, Attr); 3655 break; 3656 case AttributeList::AT_shared_locks_required: 3657 handleLocksRequiredAttr(S, D, Attr); 3658 break; 3659 case AttributeList::AT_shared_trylock_function: 3660 handleTrylockFunAttr(S, D, Attr); 3661 break; 3662 case AttributeList::AT_unlock_function: 3663 handleUnlockFunAttr(S, D, Attr); 3664 break; 3665 case AttributeList::AT_acquired_before: 3666 handleAcquireOrderAttr(S, D, Attr, /*before = */true); 3667 break; 3668 case AttributeList::AT_acquired_after: 3669 handleAcquireOrderAttr(S, D, Attr, /*before = */false); 3670 break; 3671 3672 default: 3673 // Ask target about the attribute. 3674 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); 3675 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) 3676 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) 3677 << Attr.getName(); 3678 break; 3679 } 3680 } 3681 3682 /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if 3683 /// the attribute applies to decls. If the attribute is a type attribute, just 3684 /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to 3685 /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). 3686 static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, 3687 const AttributeList &Attr, 3688 bool NonInheritable, bool Inheritable) { 3689 if (Attr.isInvalid()) 3690 return; 3691 3692 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) 3693 // FIXME: Try to deal with other __declspec attributes! 3694 return; 3695 3696 if (NonInheritable) 3697 ProcessNonInheritableDeclAttr(S, scope, D, Attr); 3698 3699 if (Inheritable) 3700 ProcessInheritableDeclAttr(S, scope, D, Attr); 3701 } 3702 3703 /// ProcessDeclAttributeList - Apply all the decl attributes in the specified 3704 /// attribute list to the specified decl, ignoring any type attributes. 3705 void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, 3706 const AttributeList *AttrList, 3707 bool NonInheritable, bool Inheritable) { 3708 for (const AttributeList* l = AttrList; l; l = l->getNext()) { 3709 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); 3710 } 3711 3712 // GCC accepts 3713 // static int a9 __attribute__((weakref)); 3714 // but that looks really pointless. We reject it. 3715 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { 3716 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << 3717 dyn_cast<NamedDecl>(D)->getNameAsString(); 3718 return; 3719 } 3720 } 3721 3722 /// checkUnusedDeclAttributes - Check a list of attributes to see if it 3723 /// contains any decl attributes that we should warn about. 3724 static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { 3725 for ( ; A; A = A->getNext()) { 3726 // Only warn if the attribute is an unignored, non-type attribute. 3727 if (A->isUsedAsTypeAttr()) continue; 3728 if (A->getKind() == AttributeList::IgnoredAttribute) continue; 3729 3730 if (A->getKind() == AttributeList::UnknownAttribute) { 3731 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) 3732 << A->getName() << A->getRange(); 3733 } else { 3734 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) 3735 << A->getName() << A->getRange(); 3736 } 3737 } 3738 } 3739 3740 /// checkUnusedDeclAttributes - Given a declarator which is not being 3741 /// used to build a declaration, complain about any decl attributes 3742 /// which might be lying around on it. 3743 void Sema::checkUnusedDeclAttributes(Declarator &D) { 3744 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); 3745 ::checkUnusedDeclAttributes(*this, D.getAttributes()); 3746 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) 3747 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); 3748 } 3749 3750 /// DeclClonePragmaWeak - clone existing decl (maybe definition), 3751 /// #pragma weak needs a non-definition decl and source may not have one 3752 NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, 3753 SourceLocation Loc) { 3754 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); 3755 NamedDecl *NewD = 0; 3756 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 3757 FunctionDecl *NewFD; 3758 // FIXME: Missing call to CheckFunctionDeclaration(). 3759 // FIXME: Mangling? 3760 // FIXME: Is the qualifier info correct? 3761 // FIXME: Is the DeclContext correct? 3762 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), 3763 Loc, Loc, DeclarationName(II), 3764 FD->getType(), FD->getTypeSourceInfo(), 3765 SC_None, SC_None, 3766 false/*isInlineSpecified*/, 3767 FD->hasPrototype(), 3768 false/*isConstexprSpecified*/); 3769 NewD = NewFD; 3770 3771 if (FD->getQualifier()) 3772 NewFD->setQualifierInfo(FD->getQualifierLoc()); 3773 3774 // Fake up parameter variables; they are declared as if this were 3775 // a typedef. 3776 QualType FDTy = FD->getType(); 3777 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { 3778 SmallVector<ParmVarDecl*, 16> Params; 3779 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), 3780 AE = FT->arg_type_end(); AI != AE; ++AI) { 3781 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); 3782 Param->setScopeInfo(0, Params.size()); 3783 Params.push_back(Param); 3784 } 3785 NewFD->setParams(Params); 3786 } 3787 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { 3788 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), 3789 VD->getInnerLocStart(), VD->getLocation(), II, 3790 VD->getType(), VD->getTypeSourceInfo(), 3791 VD->getStorageClass(), 3792 VD->getStorageClassAsWritten()); 3793 if (VD->getQualifier()) { 3794 VarDecl *NewVD = cast<VarDecl>(NewD); 3795 NewVD->setQualifierInfo(VD->getQualifierLoc()); 3796 } 3797 } 3798 return NewD; 3799 } 3800 3801 /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak 3802 /// applied to it, possibly with an alias. 3803 void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { 3804 if (W.getUsed()) return; // only do this once 3805 W.setUsed(true); 3806 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) 3807 IdentifierInfo *NDId = ND->getIdentifier(); 3808 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); 3809 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, 3810 NDId->getName())); 3811 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); 3812 WeakTopLevelDecl.push_back(NewD); 3813 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin 3814 // to insert Decl at TU scope, sorry. 3815 DeclContext *SavedContext = CurContext; 3816 CurContext = Context.getTranslationUnitDecl(); 3817 PushOnScopeChains(NewD, S); 3818 CurContext = SavedContext; 3819 } else { // just add weak to existing 3820 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); 3821 } 3822 } 3823 3824 /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in 3825 /// it, apply them to D. This is a bit tricky because PD can have attributes 3826 /// specified in many different places, and we need to find and apply them all. 3827 void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, 3828 bool NonInheritable, bool Inheritable) { 3829 // It's valid to "forward-declare" #pragma weak, in which case we 3830 // have to do this. 3831 if (Inheritable) { 3832 LoadExternalWeakUndeclaredIdentifiers(); 3833 if (!WeakUndeclaredIdentifiers.empty()) { 3834 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 3835 if (IdentifierInfo *Id = ND->getIdentifier()) { 3836 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I 3837 = WeakUndeclaredIdentifiers.find(Id); 3838 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { 3839 WeakInfo W = I->second; 3840 DeclApplyPragmaWeak(S, ND, W); 3841 WeakUndeclaredIdentifiers[Id] = W; 3842 } 3843 } 3844 } 3845 } 3846 } 3847 3848 // Apply decl attributes from the DeclSpec if present. 3849 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) 3850 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); 3851 3852 // Walk the declarator structure, applying decl attributes that were in a type 3853 // position to the decl itself. This handles cases like: 3854 // int *__attr__(x)** D; 3855 // when X is a decl attribute. 3856 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) 3857 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) 3858 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); 3859 3860 // Finally, apply any attributes on the decl itself. 3861 if (const AttributeList *Attrs = PD.getAttributes()) 3862 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); 3863 } 3864 3865 /// Is the given declaration allowed to use a forbidden type? 3866 static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { 3867 // Private ivars are always okay. Unfortunately, people don't 3868 // always properly make their ivars private, even in system headers. 3869 // Plus we need to make fields okay, too. 3870 // Function declarations in sys headers will be marked unavailable. 3871 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && 3872 !isa<FunctionDecl>(decl)) 3873 return false; 3874 3875 // Require it to be declared in a system header. 3876 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); 3877 } 3878 3879 /// Handle a delayed forbidden-type diagnostic. 3880 static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, 3881 Decl *decl) { 3882 if (decl && isForbiddenTypeAllowed(S, decl)) { 3883 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, 3884 "this system declaration uses an unsupported type")); 3885 return; 3886 } 3887 3888 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) 3889 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); 3890 diag.Triggered = true; 3891 } 3892 3893 // This duplicates a vector push_back but hides the need to know the 3894 // size of the type. 3895 void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { 3896 assert(StackSize <= StackCapacity); 3897 3898 // Grow the stack if necessary. 3899 if (StackSize == StackCapacity) { 3900 unsigned newCapacity = 2 * StackCapacity + 2; 3901 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; 3902 const char *oldBuffer = (const char*) Stack; 3903 3904 if (StackCapacity) 3905 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); 3906 3907 delete[] oldBuffer; 3908 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); 3909 StackCapacity = newCapacity; 3910 } 3911 3912 assert(StackSize < StackCapacity); 3913 new (&Stack[StackSize++]) DelayedDiagnostic(diag); 3914 } 3915 3916 void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, 3917 Decl *decl) { 3918 DelayedDiagnostics &DD = S.DelayedDiagnostics; 3919 3920 // Check the invariants. 3921 assert(DD.StackSize >= state.SavedStackSize); 3922 assert(state.SavedStackSize >= DD.ActiveStackBase); 3923 assert(DD.ParsingDepth > 0); 3924 3925 // Drop the parsing depth. 3926 DD.ParsingDepth--; 3927 3928 // If there are no active diagnostics, we're done. 3929 if (DD.StackSize == DD.ActiveStackBase) 3930 return; 3931 3932 // We only want to actually emit delayed diagnostics when we 3933 // successfully parsed a decl. 3934 if (decl && !decl->isInvalidDecl()) { 3935 // We emit all the active diagnostics, not just those starting 3936 // from the saved state. The idea is this: we get one push for a 3937 // decl spec and another for each declarator; in a decl group like: 3938 // deprecated_typedef foo, *bar, baz(); 3939 // only the declarator pops will be passed decls. This is correct; 3940 // we really do need to consider delayed diagnostics from the decl spec 3941 // for each of the different declarations. 3942 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { 3943 DelayedDiagnostic &diag = DD.Stack[i]; 3944 if (diag.Triggered) 3945 continue; 3946 3947 switch (diag.Kind) { 3948 case DelayedDiagnostic::Deprecation: 3949 S.HandleDelayedDeprecationCheck(diag, decl); 3950 break; 3951 3952 case DelayedDiagnostic::Access: 3953 S.HandleDelayedAccessCheck(diag, decl); 3954 break; 3955 3956 case DelayedDiagnostic::ForbiddenType: 3957 handleDelayedForbiddenType(S, diag, decl); 3958 break; 3959 } 3960 } 3961 } 3962 3963 // Destroy all the delayed diagnostics we're about to pop off. 3964 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) 3965 DD.Stack[i].Destroy(); 3966 3967 DD.StackSize = state.SavedStackSize; 3968 } 3969 3970 static bool isDeclDeprecated(Decl *D) { 3971 do { 3972 if (D->isDeprecated()) 3973 return true; 3974 } while ((D = cast_or_null<Decl>(D->getDeclContext()))); 3975 return false; 3976 } 3977 3978 void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, 3979 Decl *Ctx) { 3980 if (isDeclDeprecated(Ctx)) 3981 return; 3982 3983 DD.Triggered = true; 3984 if (!DD.getDeprecationMessage().empty()) 3985 Diag(DD.Loc, diag::warn_deprecated_message) 3986 << DD.getDeprecationDecl()->getDeclName() 3987 << DD.getDeprecationMessage(); 3988 else 3989 Diag(DD.Loc, diag::warn_deprecated) 3990 << DD.getDeprecationDecl()->getDeclName(); 3991 } 3992 3993 void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, 3994 SourceLocation Loc, 3995 const ObjCInterfaceDecl *UnknownObjCClass) { 3996 // Delay if we're currently parsing a declaration. 3997 if (DelayedDiagnostics.shouldDelayDiagnostics()) { 3998 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message)); 3999 return; 4000 } 4001 4002 // Otherwise, don't warn if our current context is deprecated. 4003 if (isDeclDeprecated(cast<Decl>(CurContext))) 4004 return; 4005 if (!Message.empty()) 4006 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() 4007 << Message; 4008 else { 4009 if (!UnknownObjCClass) 4010 Diag(Loc, diag::warn_deprecated) << D->getDeclName(); 4011 else { 4012 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); 4013 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); 4014 } 4015 } 4016 } 4017