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