SemaDeclAttr.cpp revision f315fa81eef1977b3457fd7a7d4639e060fe7278
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/DeclObjC.h" 19#include "clang/AST/Expr.h" 20#include "clang/Basic/TargetInfo.h" 21#include "clang/Sema/DeclSpec.h" 22#include "clang/Sema/DelayedDiagnostic.h" 23#include "llvm/ADT/StringExtras.h" 24using namespace clang; 25using namespace sema; 26 27//===----------------------------------------------------------------------===// 28// Helper functions 29//===----------------------------------------------------------------------===// 30 31static const FunctionType *getFunctionType(const Decl *d, 32 bool blocksToo = true) { 33 QualType Ty; 34 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d)) 35 Ty = decl->getType(); 36 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d)) 37 Ty = decl->getType(); 38 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) 39 Ty = decl->getUnderlyingType(); 40 else 41 return 0; 42 43 if (Ty->isFunctionPointerType()) 44 Ty = Ty->getAs<PointerType>()->getPointeeType(); 45 else if (blocksToo && Ty->isBlockPointerType()) 46 Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); 47 48 return Ty->getAs<FunctionType>(); 49} 50 51// FIXME: We should provide an abstraction around a method or function 52// to provide the following bits of information. 53 54/// isFunction - Return true if the given decl has function 55/// type (function or function-typed variable). 56static bool isFunction(const Decl *d) { 57 return getFunctionType(d, false) != NULL; 58} 59 60/// isFunctionOrMethod - Return true if the given decl has function 61/// type (function or function-typed variable) or an Objective-C 62/// method. 63static bool isFunctionOrMethod(const Decl *d) { 64 return isFunction(d)|| isa<ObjCMethodDecl>(d); 65} 66 67/// isFunctionOrMethodOrBlock - Return true if the given decl has function 68/// type (function or function-typed variable) or an Objective-C 69/// method or a block. 70static bool isFunctionOrMethodOrBlock(const Decl *d) { 71 if (isFunctionOrMethod(d)) 72 return true; 73 // check for block is more involved. 74 if (const VarDecl *V = dyn_cast<VarDecl>(d)) { 75 QualType Ty = V->getType(); 76 return Ty->isBlockPointerType(); 77 } 78 return isa<BlockDecl>(d); 79} 80 81/// Return true if the given decl has a declarator that should have 82/// been processed by Sema::GetTypeForDeclarator. 83static bool hasDeclarator(const Decl *d) { 84 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. 85 return isa<DeclaratorDecl>(d) || isa<BlockDecl>(d) || isa<TypedefDecl>(d); 86} 87 88/// hasFunctionProto - Return true if the given decl has a argument 89/// information. This decl should have already passed 90/// isFunctionOrMethod or isFunctionOrMethodOrBlock. 91static bool hasFunctionProto(const Decl *d) { 92 if (const FunctionType *FnTy = getFunctionType(d)) 93 return isa<FunctionProtoType>(FnTy); 94 else { 95 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d)); 96 return true; 97 } 98} 99 100/// getFunctionOrMethodNumArgs - Return number of function or method 101/// arguments. It is an error to call this on a K&R function (use 102/// hasFunctionProto first). 103static unsigned getFunctionOrMethodNumArgs(const Decl *d) { 104 if (const FunctionType *FnTy = getFunctionType(d)) 105 return cast<FunctionProtoType>(FnTy)->getNumArgs(); 106 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) 107 return BD->getNumParams(); 108 return cast<ObjCMethodDecl>(d)->param_size(); 109} 110 111static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) { 112 if (const FunctionType *FnTy = getFunctionType(d)) 113 return cast<FunctionProtoType>(FnTy)->getArgType(Idx); 114 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) 115 return BD->getParamDecl(Idx)->getType(); 116 117 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType(); 118} 119 120static QualType getFunctionOrMethodResultType(const Decl *d) { 121 if (const FunctionType *FnTy = getFunctionType(d)) 122 return cast<FunctionProtoType>(FnTy)->getResultType(); 123 return cast<ObjCMethodDecl>(d)->getResultType(); 124} 125 126static bool isFunctionOrMethodVariadic(const Decl *d) { 127 if (const FunctionType *FnTy = getFunctionType(d)) { 128 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); 129 return proto->isVariadic(); 130 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) 131 return BD->isVariadic(); 132 else { 133 return cast<ObjCMethodDecl>(d)->isVariadic(); 134 } 135} 136 137static bool isInstanceMethod(const Decl *d) { 138 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d)) 139 return MethodDecl->isInstance(); 140 return false; 141} 142 143static inline bool isNSStringType(QualType T, ASTContext &Ctx) { 144 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); 145 if (!PT) 146 return false; 147 148 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); 149 if (!Cls) 150 return false; 151 152 IdentifierInfo* ClsName = Cls->getIdentifier(); 153 154 // FIXME: Should we walk the chain of classes? 155 return ClsName == &Ctx.Idents.get("NSString") || 156 ClsName == &Ctx.Idents.get("NSMutableString"); 157} 158 159static inline bool isCFStringType(QualType T, ASTContext &Ctx) { 160 const PointerType *PT = T->getAs<PointerType>(); 161 if (!PT) 162 return false; 163 164 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); 165 if (!RT) 166 return false; 167 168 const RecordDecl *RD = RT->getDecl(); 169 if (RD->getTagKind() != TTK_Struct) 170 return false; 171 172 return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); 173} 174 175//===----------------------------------------------------------------------===// 176// Attribute Implementations 177//===----------------------------------------------------------------------===// 178 179// FIXME: All this manual attribute parsing code is gross. At the 180// least add some helper functions to check most argument patterns (# 181// and types of args). 182 183static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, 184 const AttributeList &Attr, Sema &S) { 185 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); 186 if (tDecl == 0) { 187 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); 188 return; 189 } 190 191 QualType curType = tDecl->getUnderlyingType(); 192 193 Expr *sizeExpr; 194 195 // Special case where the argument is a template id. 196 if (Attr.getParameterName()) { 197 CXXScopeSpec SS; 198 UnqualifiedId id; 199 id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); 200 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>(); 201 } else { 202 // check the attribute arguments. 203 if (Attr.getNumArgs() != 1) { 204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 205 return; 206 } 207 sizeExpr = Attr.getArg(0); 208 } 209 210 // Instantiate/Install the vector type, and let Sema build the type for us. 211 // This will run the reguired checks. 212 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); 213 if (!T.isNull()) { 214 // FIXME: preserve the old source info. 215 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); 216 217 // Remember this typedef decl, we will need it later for diagnostics. 218 S.ExtVectorDecls.push_back(tDecl); 219 } 220} 221 222static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 223 // check the attribute arguments. 224 if (Attr.getNumArgs() > 0) { 225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 226 return; 227 } 228 229 if (TagDecl *TD = dyn_cast<TagDecl>(d)) 230 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); 231 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { 232 // If the alignment is less than or equal to 8 bits, the packed attribute 233 // has no effect. 234 if (!FD->getType()->isIncompleteType() && 235 S.Context.getTypeAlign(FD->getType()) <= 8) 236 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) 237 << Attr.getName() << FD->getType(); 238 else 239 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); 240 } else 241 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 242} 243 244static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { 245 // check the attribute arguments. 246 if (Attr.getNumArgs() > 0) { 247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 248 return; 249 } 250 251 // The IBAction attributes only apply to instance methods. 252 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) 253 if (MD->isInstanceMethod()) { 254 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context)); 255 return; 256 } 257 258 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); 259} 260 261static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) { 262 // check the attribute arguments. 263 if (Attr.getNumArgs() > 0) { 264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 265 return; 266 } 267 268 // The IBOutlet attributes only apply to instance variables of 269 // Objective-C classes. 270 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) { 271 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context)); 272 return; 273 } 274 275 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); 276} 277 278static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr, 279 Sema &S) { 280 281 // The iboutletcollection attribute can have zero or one arguments. 282 if (Attr.getParameterName() && Attr.getNumArgs() > 0) { 283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 284 return; 285 } 286 287 // The IBOutletCollection attributes only apply to instance variables of 288 // Objective-C classes. 289 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) { 290 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); 291 return; 292 } 293 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d)) 294 if (!VD->getType()->getAs<ObjCObjectPointerType>()) { 295 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) 296 << VD->getType() << 0; 297 return; 298 } 299 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d)) 300 if (!PD->getType()->getAs<ObjCObjectPointerType>()) { 301 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) 302 << PD->getType() << 1; 303 return; 304 } 305 306 IdentifierInfo *II = Attr.getParameterName(); 307 if (!II) 308 II = &S.Context.Idents.get("id"); 309 310 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), 311 S.getScopeForContext(d->getDeclContext()->getParent())); 312 if (!TypeRep) { 313 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; 314 return; 315 } 316 QualType QT = TypeRep.get(); 317 // Diagnose use of non-object type in iboutletcollection attribute. 318 // FIXME. Gnu attribute extension ignores use of builtin types in 319 // attributes. So, __attribute__((iboutletcollection(char))) will be 320 // treated as __attribute__((iboutletcollection())). 321 if (!QT->isObjCIdType() && !QT->isObjCClassType() && 322 !QT->isObjCObjectType()) { 323 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; 324 return; 325 } 326 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context, 327 QT)); 328} 329 330static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { 331 // GCC ignores the nonnull attribute on K&R style function prototypes, so we 332 // ignore it as well 333 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { 334 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 335 << Attr.getName() << 0 /*function*/; 336 return; 337 } 338 339 // In C++ the implicit 'this' function parameter also counts, and they are 340 // counted from one. 341 bool HasImplicitThisParam = isInstanceMethod(d); 342 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; 343 344 // The nonnull attribute only applies to pointers. 345 llvm::SmallVector<unsigned, 10> NonNullArgs; 346 347 for (AttributeList::arg_iterator I=Attr.arg_begin(), 348 E=Attr.arg_end(); I!=E; ++I) { 349 350 351 // The argument must be an integer constant expression. 352 Expr *Ex = *I; 353 llvm::APSInt ArgNum(32); 354 if (Ex->isTypeDependent() || Ex->isValueDependent() || 355 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { 356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 357 << "nonnull" << Ex->getSourceRange(); 358 return; 359 } 360 361 unsigned x = (unsigned) ArgNum.getZExtValue(); 362 363 if (x < 1 || x > NumArgs) { 364 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 365 << "nonnull" << I.getArgNum() << Ex->getSourceRange(); 366 return; 367 } 368 369 --x; 370 if (HasImplicitThisParam) { 371 if (x == 0) { 372 S.Diag(Attr.getLoc(), 373 diag::err_attribute_invalid_implicit_this_argument) 374 << "nonnull" << Ex->getSourceRange(); 375 return; 376 } 377 --x; 378 } 379 380 // Is the function argument a pointer type? 381 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType(); 382 if (!T->isAnyPointerType() && !T->isBlockPointerType()) { 383 // FIXME: Should also highlight argument in decl. 384 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) 385 << "nonnull" << Ex->getSourceRange(); 386 continue; 387 } 388 389 NonNullArgs.push_back(x); 390 } 391 392 // If no arguments were specified to __attribute__((nonnull)) then all pointer 393 // arguments have a nonnull attribute. 394 if (NonNullArgs.empty()) { 395 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { 396 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType(); 397 if (T->isAnyPointerType() || T->isBlockPointerType()) 398 NonNullArgs.push_back(I); 399 else if (const RecordType *UT = T->getAsUnionType()) { 400 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { 401 RecordDecl *UD = UT->getDecl(); 402 for (RecordDecl::field_iterator it = UD->field_begin(), 403 itend = UD->field_end(); it != itend; ++it) { 404 T = it->getType(); 405 if (T->isAnyPointerType() || T->isBlockPointerType()) { 406 NonNullArgs.push_back(I); 407 break; 408 } 409 } 410 } 411 } 412 } 413 414 // No pointer arguments? 415 if (NonNullArgs.empty()) { 416 // Warn the trivial case only if attribute is not coming from a 417 // macro instantiation. 418 if (Attr.getLoc().isFileID()) 419 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); 420 return; 421 } 422 } 423 424 unsigned* start = &NonNullArgs[0]; 425 unsigned size = NonNullArgs.size(); 426 llvm::array_pod_sort(start, start + size); 427 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start, 428 size)); 429} 430 431static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) { 432 // This attribute must be applied to a function declaration. 433 // The first argument to the attribute must be a string, 434 // the name of the resource, for example "malloc". 435 // The following arguments must be argument indexes, the arguments must be 436 // of integer type for Returns, otherwise of pointer type. 437 // The difference between Holds and Takes is that a pointer may still be used 438 // after being held. free() should be __attribute((ownership_takes)), whereas 439 // a list append function may well be __attribute((ownership_holds)). 440 441 if (!AL.getParameterName()) { 442 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) 443 << AL.getName()->getName() << 1; 444 return; 445 } 446 // Figure out our Kind, and check arguments while we're at it. 447 OwnershipAttr::OwnershipKind K; 448 switch (AL.getKind()) { 449 case AttributeList::AT_ownership_takes: 450 K = OwnershipAttr::Takes; 451 if (AL.getNumArgs() < 1) { 452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; 453 return; 454 } 455 break; 456 case AttributeList::AT_ownership_holds: 457 K = OwnershipAttr::Holds; 458 if (AL.getNumArgs() < 1) { 459 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; 460 return; 461 } 462 break; 463 case AttributeList::AT_ownership_returns: 464 K = OwnershipAttr::Returns; 465 if (AL.getNumArgs() > 1) { 466 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) 467 << AL.getNumArgs() + 1; 468 return; 469 } 470 break; 471 default: 472 // This should never happen given how we are called. 473 llvm_unreachable("Unknown ownership attribute"); 474 } 475 476 if (!isFunction(d) || !hasFunctionProto(d)) { 477 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName() 478 << 0 /*function*/; 479 return; 480 } 481 482 // In C++ the implicit 'this' function parameter also counts, and they are 483 // counted from one. 484 bool HasImplicitThisParam = isInstanceMethod(d); 485 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; 486 487 llvm::StringRef Module = AL.getParameterName()->getName(); 488 489 // Normalize the argument, __foo__ becomes foo. 490 if (Module.startswith("__") && Module.endswith("__")) 491 Module = Module.substr(2, Module.size() - 4); 492 493 llvm::SmallVector<unsigned, 10> OwnershipArgs; 494 495 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; 496 ++I) { 497 498 Expr *IdxExpr = *I; 499 llvm::APSInt ArgNum(32); 500 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() 501 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { 502 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) 503 << AL.getName()->getName() << IdxExpr->getSourceRange(); 504 continue; 505 } 506 507 unsigned x = (unsigned) ArgNum.getZExtValue(); 508 509 if (x > NumArgs || x < 1) { 510 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) 511 << AL.getName()->getName() << x << IdxExpr->getSourceRange(); 512 continue; 513 } 514 --x; 515 if (HasImplicitThisParam) { 516 if (x == 0) { 517 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) 518 << "ownership" << IdxExpr->getSourceRange(); 519 return; 520 } 521 --x; 522 } 523 524 switch (K) { 525 case OwnershipAttr::Takes: 526 case OwnershipAttr::Holds: { 527 // Is the function argument a pointer type? 528 QualType T = getFunctionOrMethodArgType(d, x); 529 if (!T->isAnyPointerType() && !T->isBlockPointerType()) { 530 // FIXME: Should also highlight argument in decl. 531 S.Diag(AL.getLoc(), diag::err_ownership_type) 532 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") 533 << "pointer" 534 << IdxExpr->getSourceRange(); 535 continue; 536 } 537 break; 538 } 539 case OwnershipAttr::Returns: { 540 if (AL.getNumArgs() > 1) { 541 // Is the function argument an integer type? 542 Expr *IdxExpr = AL.getArg(0); 543 llvm::APSInt ArgNum(32); 544 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() 545 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { 546 S.Diag(AL.getLoc(), diag::err_ownership_type) 547 << "ownership_returns" << "integer" 548 << IdxExpr->getSourceRange(); 549 return; 550 } 551 } 552 break; 553 } 554 default: 555 llvm_unreachable("Unknown ownership attribute"); 556 } // switch 557 558 // Check we don't have a conflict with another ownership attribute. 559 for (specific_attr_iterator<OwnershipAttr> 560 i = d->specific_attr_begin<OwnershipAttr>(), 561 e = d->specific_attr_end<OwnershipAttr>(); 562 i != e; ++i) { 563 if ((*i)->getOwnKind() != K) { 564 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); 565 I!=E; ++I) { 566 if (x == *I) { 567 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) 568 << AL.getName()->getName() << "ownership_*"; 569 } 570 } 571 } 572 } 573 OwnershipArgs.push_back(x); 574 } 575 576 unsigned* start = OwnershipArgs.data(); 577 unsigned size = OwnershipArgs.size(); 578 llvm::array_pod_sort(start, start + size); 579 580 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { 581 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; 582 return; 583 } 584 585 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, 586 start, size)); 587} 588 589/// Whether this declaration has internal linkage for the purposes of 590/// things that want to complain about things not have internal linkage. 591static bool hasEffectivelyInternalLinkage(NamedDecl *D) { 592 switch (D->getLinkage()) { 593 case NoLinkage: 594 case InternalLinkage: 595 return true; 596 597 // Template instantiations that go from external to unique-external 598 // shouldn't get diagnosed. 599 case UniqueExternalLinkage: 600 return true; 601 602 case ExternalLinkage: 603 return false; 604 } 605 llvm_unreachable("unknown linkage kind!"); 606 return false; 607} 608 609static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) { 610 // Check the attribute arguments. 611 if (Attr.getNumArgs() > 1) { 612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 613 return; 614 } 615 616 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) { 617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 618 << Attr.getName() << 2 /*variables and functions*/; 619 return; 620 } 621 622 NamedDecl *nd = cast<NamedDecl>(d); 623 624 // gcc rejects 625 // class c { 626 // static int a __attribute__((weakref ("v2"))); 627 // static int b() __attribute__((weakref ("f3"))); 628 // }; 629 // and ignores the attributes of 630 // void f(void) { 631 // static int a __attribute__((weakref ("v2"))); 632 // } 633 // we reject them 634 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext(); 635 if (!Ctx->isFileContext()) { 636 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << 637 nd->getNameAsString(); 638 return; 639 } 640 641 // The GCC manual says 642 // 643 // At present, a declaration to which `weakref' is attached can only 644 // be `static'. 645 // 646 // It also says 647 // 648 // Without a TARGET, 649 // given as an argument to `weakref' or to `alias', `weakref' is 650 // equivalent to `weak'. 651 // 652 // gcc 4.4.1 will accept 653 // int a7 __attribute__((weakref)); 654 // as 655 // int a7 __attribute__((weak)); 656 // This looks like a bug in gcc. We reject that for now. We should revisit 657 // it if this behaviour is actually used. 658 659 if (!hasEffectivelyInternalLinkage(nd)) { 660 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); 661 return; 662 } 663 664 // GCC rejects 665 // static ((alias ("y"), weakref)). 666 // Should we? How to check that weakref is before or after alias? 667 668 if (Attr.getNumArgs() == 1) { 669 Expr *Arg = Attr.getArg(0); 670 Arg = Arg->IgnoreParenCasts(); 671 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 672 673 if (Str == 0 || Str->isWide()) { 674 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 675 << "weakref" << 1; 676 return; 677 } 678 // GCC will accept anything as the argument of weakref. Should we 679 // check for an existing decl? 680 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, 681 Str->getString())); 682 } 683 684 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context)); 685} 686 687static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { 688 // check the attribute arguments. 689 if (Attr.getNumArgs() != 1) { 690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 691 return; 692 } 693 694 Expr *Arg = Attr.getArg(0); 695 Arg = Arg->IgnoreParenCasts(); 696 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 697 698 if (Str == 0 || Str->isWide()) { 699 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 700 << "alias" << 1; 701 return; 702 } 703 704 if (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin) { 705 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); 706 return; 707 } 708 709 // FIXME: check if target symbol exists in current file 710 711 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, 712 Str->getString())); 713} 714 715static void HandleNakedAttr(Decl *d, const AttributeList &Attr, 716 Sema &S) { 717 // Check the attribute arguments. 718 if (Attr.getNumArgs() != 0) { 719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 720 return; 721 } 722 723 if (!isa<FunctionDecl>(d)) { 724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 725 << Attr.getName() << 0 /*function*/; 726 return; 727 } 728 729 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context)); 730} 731 732static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, 733 Sema &S) { 734 // Check the attribute arguments. 735 if (Attr.getNumArgs() != 0) { 736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 737 return; 738 } 739 740 if (!isa<FunctionDecl>(d)) { 741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 742 << Attr.getName() << 0 /*function*/; 743 return; 744 } 745 746 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context)); 747} 748 749static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { 750 // Check the attribute arguments. 751 if (Attr.getNumArgs() != 0) { 752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 753 return; 754 } 755 756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { 757 QualType RetTy = FD->getResultType(); 758 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { 759 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context)); 760 return; 761 } 762 } 763 764 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); 765} 766 767static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { 768 // check the attribute arguments. 769 if (Attr.getNumArgs() != 0) { 770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 771 return; 772 } 773 774 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context)); 775} 776 777static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { 778 assert(Attr.isInvalid() == false); 779 if (isa<VarDecl>(d)) 780 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context)); 781 else 782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 783 << Attr.getName() << 12 /* variable */; 784} 785 786static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { 787 assert(Attr.isInvalid() == false); 788 if (isa<VarDecl>(d)) 789 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context)); 790 else 791 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 792 << Attr.getName() << 12 /* variable */; 793} 794 795static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) { 796 if (hasDeclarator(d)) return; 797 798 if (S.CheckNoReturnAttr(attr)) return; 799 800 if (!isa<ObjCMethodDecl>(d)) { 801 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) 802 << attr.getName() << 0 /*function*/; 803 return; 804 } 805 806 d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context)); 807} 808 809bool Sema::CheckNoReturnAttr(const AttributeList &attr) { 810 if (attr.getNumArgs() != 0) { 811 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 812 attr.setInvalid(); 813 return true; 814 } 815 816 return false; 817} 818 819static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, 820 Sema &S) { 821 822 // The checking path for 'noreturn' and 'analyzer_noreturn' are different 823 // because 'analyzer_noreturn' does not impact the type. 824 825 if (Attr.getNumArgs() != 0) { 826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 827 return; 828 } 829 830 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { 831 ValueDecl *VD = dyn_cast<ValueDecl>(d); 832 if (VD == 0 || (!VD->getType()->isBlockPointerType() 833 && !VD->getType()->isFunctionPointerType())) { 834 S.Diag(Attr.getLoc(), 835 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type 836 : diag::warn_attribute_wrong_decl_type) 837 << Attr.getName() << 0 /*function*/; 838 return; 839 } 840 } 841 842 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context)); 843} 844 845// PS3 PPU-specific. 846static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr, 847 Sema &S) { 848/* 849 Returning a Vector Class in Registers 850 851 According to the PPU ABI specifications, a class with a single member of 852 vector type is returned in memory when used as the return value of a function. 853 This results in inefficient code when implementing vector classes. To return 854 the value in a single vector register, add the vecreturn attribute to the 855 class definition. This attribute is also applicable to struct types. 856 857 Example: 858 859 struct Vector 860 { 861 __vector float xyzw; 862 } __attribute__((vecreturn)); 863 864 Vector Add(Vector lhs, Vector rhs) 865 { 866 Vector result; 867 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); 868 return result; // This will be returned in a register 869 } 870*/ 871 if (!isa<RecordDecl>(d)) { 872 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 873 << Attr.getName() << 9 /*class*/; 874 return; 875 } 876 877 if (d->getAttr<VecReturnAttr>()) { 878 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; 879 return; 880 } 881 882 RecordDecl *record = cast<RecordDecl>(d); 883 int count = 0; 884 885 if (!isa<CXXRecordDecl>(record)) { 886 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); 887 return; 888 } 889 890 if (!cast<CXXRecordDecl>(record)->isPOD()) { 891 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); 892 return; 893 } 894 895 for (RecordDecl::field_iterator iter = record->field_begin(); 896 iter != record->field_end(); iter++) { 897 if ((count == 1) || !iter->getType()->isVectorType()) { 898 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); 899 return; 900 } 901 count++; 902 } 903 904 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context)); 905} 906 907static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) { 908 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) { 909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) 910 << Attr.getName() << 8 /*function, method, or parameter*/; 911 return; 912 } 913 // FIXME: Actually store the attribute on the declaration 914} 915 916static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 917 // check the attribute arguments. 918 if (Attr.getNumArgs() != 0) { 919 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 920 return; 921 } 922 923 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) && 924 !isa<TypeDecl>(d)) { 925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 926 << Attr.getName() << 2 /*variable and function*/; 927 return; 928 } 929 930 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context)); 931} 932 933static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 934 // check the attribute arguments. 935 if (Attr.getNumArgs() != 0) { 936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 937 return; 938 } 939 940 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { 941 if (VD->hasLocalStorage() || VD->hasExternalStorage()) { 942 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; 943 return; 944 } 945 } else if (!isFunctionOrMethod(d)) { 946 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 947 << Attr.getName() << 2 /*variable and function*/; 948 return; 949 } 950 951 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); 952} 953 954static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { 955 // check the attribute arguments. 956 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { 957 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 958 << "0 or 1"; 959 return; 960 } 961 962 int priority = 65535; // FIXME: Do not hardcode such constants. 963 if (Attr.getNumArgs() > 0) { 964 Expr *E = Attr.getArg(0); 965 llvm::APSInt Idx(32); 966 if (E->isTypeDependent() || E->isValueDependent() || 967 !E->isIntegerConstantExpr(Idx, S.Context)) { 968 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 969 << "constructor" << 1 << E->getSourceRange(); 970 return; 971 } 972 priority = Idx.getZExtValue(); 973 } 974 975 if (!isa<FunctionDecl>(d)) { 976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 977 << Attr.getName() << 0 /*function*/; 978 return; 979 } 980 981 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, 982 priority)); 983} 984 985static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { 986 // check the attribute arguments. 987 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { 988 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 989 << "0 or 1"; 990 return; 991 } 992 993 int priority = 65535; // FIXME: Do not hardcode such constants. 994 if (Attr.getNumArgs() > 0) { 995 Expr *E = Attr.getArg(0); 996 llvm::APSInt Idx(32); 997 if (E->isTypeDependent() || E->isValueDependent() || 998 !E->isIntegerConstantExpr(Idx, S.Context)) { 999 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1000 << "destructor" << 1 << E->getSourceRange(); 1001 return; 1002 } 1003 priority = Idx.getZExtValue(); 1004 } 1005 1006 if (!isa<FunctionDecl>(d)) { 1007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1008 << Attr.getName() << 0 /*function*/; 1009 return; 1010 } 1011 1012 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, 1013 priority)); 1014} 1015 1016static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1017 // check the attribute arguments. 1018 int noArgs = Attr.getNumArgs(); 1019 if (noArgs > 1) { 1020 S.Diag(Attr.getLoc(), 1021 diag::err_attribute_wrong_number_arguments) << "0 or 1"; 1022 return; 1023 } 1024 // Handle the case where deprecated attribute has a text message. 1025 StringLiteral *SE; 1026 if (noArgs == 1) { 1027 Expr *ArgExpr = Attr.getArg(0); 1028 SE = dyn_cast<StringLiteral>(ArgExpr); 1029 if (!SE) { 1030 S.Diag(ArgExpr->getLocStart(), 1031 diag::err_attribute_not_string) << "deprecated"; 1032 return; 1033 } 1034 } 1035 else 1036 SE = StringLiteral::CreateEmpty(S.Context, 1); 1037 1038 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, 1039 SE->getString())); 1040} 1041 1042static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1043 // check the attribute arguments. 1044 int noArgs = Attr.getNumArgs(); 1045 if (noArgs > 1) { 1046 S.Diag(Attr.getLoc(), 1047 diag::err_attribute_wrong_number_arguments) << "0 or 1"; 1048 return; 1049 } 1050 // Handle the case where unavailable attribute has a text message. 1051 StringLiteral *SE; 1052 if (noArgs == 1) { 1053 Expr *ArgExpr = Attr.getArg(0); 1054 SE = dyn_cast<StringLiteral>(ArgExpr); 1055 if (!SE) { 1056 S.Diag(ArgExpr->getLocStart(), 1057 diag::err_attribute_not_string) << "unavailable"; 1058 return; 1059 } 1060 } 1061 else 1062 SE = StringLiteral::CreateEmpty(S.Context, 1); 1063 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, 1064 SE->getString())); 1065} 1066 1067static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1068 // check the attribute arguments. 1069 if (Attr.getNumArgs() != 1) { 1070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1071 return; 1072 } 1073 1074 Expr *Arg = Attr.getArg(0); 1075 Arg = Arg->IgnoreParenCasts(); 1076 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 1077 1078 if (Str == 0 || Str->isWide()) { 1079 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1080 << "visibility" << 1; 1081 return; 1082 } 1083 1084 llvm::StringRef TypeStr = Str->getString(); 1085 VisibilityAttr::VisibilityType type; 1086 1087 if (TypeStr == "default") 1088 type = VisibilityAttr::Default; 1089 else if (TypeStr == "hidden") 1090 type = VisibilityAttr::Hidden; 1091 else if (TypeStr == "internal") 1092 type = VisibilityAttr::Hidden; // FIXME 1093 else if (TypeStr == "protected") 1094 type = VisibilityAttr::Protected; 1095 else { 1096 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; 1097 return; 1098 } 1099 1100 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type)); 1101} 1102 1103static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, 1104 Sema &S) { 1105 if (Attr.getNumArgs() != 0) { 1106 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1107 return; 1108 } 1109 1110 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); 1111 if (OCI == 0) { 1112 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); 1113 return; 1114 } 1115 1116 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context)); 1117} 1118 1119static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { 1120 if (Attr.getNumArgs() != 0) { 1121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1122 return; 1123 } 1124 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { 1125 QualType T = TD->getUnderlyingType(); 1126 if (!T->isPointerType() || 1127 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { 1128 S.Diag(TD->getLocation(), diag::err_nsobject_attribute); 1129 return; 1130 } 1131 } 1132 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context)); 1133} 1134 1135static void 1136HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { 1137 if (Attr.getNumArgs() != 0) { 1138 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1139 return; 1140 } 1141 1142 if (!isa<FunctionDecl>(D)) { 1143 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); 1144 return; 1145 } 1146 1147 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context)); 1148} 1149 1150static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1151 if (!Attr.getParameterName()) { 1152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1153 << "blocks" << 1; 1154 return; 1155 } 1156 1157 if (Attr.getNumArgs() != 0) { 1158 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1159 return; 1160 } 1161 1162 BlocksAttr::BlockType type; 1163 if (Attr.getParameterName()->isStr("byref")) 1164 type = BlocksAttr::ByRef; 1165 else { 1166 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1167 << "blocks" << Attr.getParameterName(); 1168 return; 1169 } 1170 1171 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type)); 1172} 1173 1174static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1175 // check the attribute arguments. 1176 if (Attr.getNumArgs() > 2) { 1177 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 1178 << "0, 1 or 2"; 1179 return; 1180 } 1181 1182 int sentinel = 0; 1183 if (Attr.getNumArgs() > 0) { 1184 Expr *E = Attr.getArg(0); 1185 llvm::APSInt Idx(32); 1186 if (E->isTypeDependent() || E->isValueDependent() || 1187 !E->isIntegerConstantExpr(Idx, S.Context)) { 1188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1189 << "sentinel" << 1 << E->getSourceRange(); 1190 return; 1191 } 1192 sentinel = Idx.getZExtValue(); 1193 1194 if (sentinel < 0) { 1195 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) 1196 << E->getSourceRange(); 1197 return; 1198 } 1199 } 1200 1201 int nullPos = 0; 1202 if (Attr.getNumArgs() > 1) { 1203 Expr *E = Attr.getArg(1); 1204 llvm::APSInt Idx(32); 1205 if (E->isTypeDependent() || E->isValueDependent() || 1206 !E->isIntegerConstantExpr(Idx, S.Context)) { 1207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1208 << "sentinel" << 2 << E->getSourceRange(); 1209 return; 1210 } 1211 nullPos = Idx.getZExtValue(); 1212 1213 if (nullPos > 1 || nullPos < 0) { 1214 // FIXME: This error message could be improved, it would be nice 1215 // to say what the bounds actually are. 1216 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) 1217 << E->getSourceRange(); 1218 return; 1219 } 1220 } 1221 1222 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { 1223 const FunctionType *FT = FD->getType()->getAs<FunctionType>(); 1224 assert(FT && "FunctionDecl has non-function type?"); 1225 1226 if (isa<FunctionNoProtoType>(FT)) { 1227 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); 1228 return; 1229 } 1230 1231 if (!cast<FunctionProtoType>(FT)->isVariadic()) { 1232 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; 1233 return; 1234 } 1235 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { 1236 if (!MD->isVariadic()) { 1237 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; 1238 return; 1239 } 1240 } else if (isa<BlockDecl>(d)) { 1241 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the 1242 // caller. 1243 ; 1244 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { 1245 QualType Ty = V->getType(); 1246 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { 1247 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) 1248 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); 1249 if (!cast<FunctionProtoType>(FT)->isVariadic()) { 1250 int m = Ty->isFunctionPointerType() ? 0 : 1; 1251 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; 1252 return; 1253 } 1254 } else { 1255 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1256 << Attr.getName() << 6 /*function, method or block */; 1257 return; 1258 } 1259 } else { 1260 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1261 << Attr.getName() << 6 /*function, method or block */; 1262 return; 1263 } 1264 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, 1265 nullPos)); 1266} 1267 1268static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { 1269 // check the attribute arguments. 1270 if (Attr.getNumArgs() != 0) { 1271 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1272 return; 1273 } 1274 1275 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { 1276 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1277 << Attr.getName() << 0 /*function*/; 1278 return; 1279 } 1280 1281 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { 1282 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) 1283 << Attr.getName() << 0; 1284 return; 1285 } 1286 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 1287 if (MD->getResultType()->isVoidType()) { 1288 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) 1289 << Attr.getName() << 1; 1290 return; 1291 } 1292 1293 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context)); 1294} 1295 1296static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) { 1297 // check the attribute arguments. 1298 if (attr.getNumArgs() != 0) { 1299 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1300 return; 1301 } 1302 1303 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) { 1304 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1305 << attr.getName() << 2 /*variables and functions*/; 1306 return; 1307 } 1308 1309 NamedDecl *nd = cast<NamedDecl>(d); 1310 1311 // 'weak' only applies to declarations with external linkage. 1312 if (hasEffectivelyInternalLinkage(nd)) { 1313 S.Diag(attr.getLoc(), diag::err_attribute_weak_static); 1314 return; 1315 } 1316 1317 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context)); 1318} 1319 1320static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { 1321 // check the attribute arguments. 1322 if (Attr.getNumArgs() != 0) { 1323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1324 return; 1325 } 1326 1327 // weak_import only applies to variable & function declarations. 1328 bool isDef = false; 1329 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 1330 isDef = (!VD->hasExternalStorage() || VD->getInit()); 1331 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1332 isDef = FD->hasBody(); 1333 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) { 1334 // We ignore weak import on properties and methods 1335 return; 1336 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) { 1337 // Don't issue the warning for darwin as target; yet, ignore the attribute. 1338 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin || 1339 !isa<ObjCInterfaceDecl>(D)) 1340 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1341 << Attr.getName() << 2 /*variable and function*/; 1342 return; 1343 } 1344 1345 // Merge should handle any subsequent violations. 1346 if (isDef) { 1347 S.Diag(Attr.getLoc(), 1348 diag::warn_attribute_weak_import_invalid_on_definition) 1349 << "weak_import" << 2 /*variable and function*/; 1350 return; 1351 } 1352 1353 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context)); 1354} 1355 1356static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, 1357 Sema &S) { 1358 // Attribute has 3 arguments. 1359 if (Attr.getNumArgs() != 3) { 1360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1361 return; 1362 } 1363 1364 unsigned WGSize[3]; 1365 for (unsigned i = 0; i < 3; ++i) { 1366 Expr *E = Attr.getArg(i); 1367 llvm::APSInt ArgNum(32); 1368 if (E->isTypeDependent() || E->isValueDependent() || 1369 !E->isIntegerConstantExpr(ArgNum, S.Context)) { 1370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 1371 << "reqd_work_group_size" << E->getSourceRange(); 1372 return; 1373 } 1374 WGSize[i] = (unsigned) ArgNum.getZExtValue(); 1375 } 1376 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context, 1377 WGSize[0], WGSize[1], 1378 WGSize[2])); 1379} 1380 1381static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { 1382 // Attribute has no arguments. 1383 if (Attr.getNumArgs() != 1) { 1384 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1385 return; 1386 } 1387 1388 // Make sure that there is a string literal as the sections's single 1389 // argument. 1390 Expr *ArgExpr = Attr.getArg(0); 1391 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); 1392 if (!SE) { 1393 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; 1394 return; 1395 } 1396 1397 // If the target wants to validate the section specifier, make it happen. 1398 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); 1399 if (!Error.empty()) { 1400 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) 1401 << Error; 1402 return; 1403 } 1404 1405 // This attribute cannot be applied to local variables. 1406 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { 1407 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); 1408 return; 1409 } 1410 1411 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, 1412 SE->getString())); 1413} 1414 1415 1416static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1417 // check the attribute arguments. 1418 if (Attr.getNumArgs() != 0) { 1419 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1420 return; 1421 } 1422 1423 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); 1424} 1425 1426static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1427 // check the attribute arguments. 1428 if (Attr.getNumArgs() != 0) { 1429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1430 return; 1431 } 1432 1433 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); 1434} 1435 1436static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1437 // check the attribute arguments. 1438 if (Attr.getNumArgs() != 0) { 1439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1440 return; 1441 } 1442 1443 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); 1444} 1445 1446static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1447 if (!Attr.getParameterName()) { 1448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1449 return; 1450 } 1451 1452 if (Attr.getNumArgs() != 0) { 1453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1454 return; 1455 } 1456 1457 VarDecl *VD = dyn_cast<VarDecl>(d); 1458 1459 if (!VD || !VD->hasLocalStorage()) { 1460 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; 1461 return; 1462 } 1463 1464 // Look up the function 1465 // FIXME: Lookup probably isn't looking in the right place 1466 NamedDecl *CleanupDecl 1467 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), 1468 Attr.getParameterLoc(), Sema::LookupOrdinaryName); 1469 if (!CleanupDecl) { 1470 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << 1471 Attr.getParameterName(); 1472 return; 1473 } 1474 1475 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); 1476 if (!FD) { 1477 S.Diag(Attr.getParameterLoc(), 1478 diag::err_attribute_cleanup_arg_not_function) 1479 << Attr.getParameterName(); 1480 return; 1481 } 1482 1483 if (FD->getNumParams() != 1) { 1484 S.Diag(Attr.getParameterLoc(), 1485 diag::err_attribute_cleanup_func_must_take_one_arg) 1486 << Attr.getParameterName(); 1487 return; 1488 } 1489 1490 // We're currently more strict than GCC about what function types we accept. 1491 // If this ever proves to be a problem it should be easy to fix. 1492 QualType Ty = S.Context.getPointerType(VD->getType()); 1493 QualType ParamTy = FD->getParamDecl(0)->getType(); 1494 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), 1495 ParamTy, Ty) != Sema::Compatible) { 1496 S.Diag(Attr.getParameterLoc(), 1497 diag::err_attribute_cleanup_func_arg_incompatible_type) << 1498 Attr.getParameterName() << ParamTy << Ty; 1499 return; 1500 } 1501 1502 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD)); 1503 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); 1504} 1505 1506/// Handle __attribute__((format_arg((idx)))) attribute based on 1507/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 1508static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1509 if (Attr.getNumArgs() != 1) { 1510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1511 return; 1512 } 1513 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { 1514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1515 << Attr.getName() << 0 /*function*/; 1516 return; 1517 } 1518 1519 // In C++ the implicit 'this' function parameter also counts, and they are 1520 // counted from one. 1521 bool HasImplicitThisParam = isInstanceMethod(d); 1522 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; 1523 unsigned FirstIdx = 1; 1524 1525 // checks for the 2nd argument 1526 Expr *IdxExpr = Attr.getArg(0); 1527 llvm::APSInt Idx(32); 1528 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || 1529 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { 1530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1531 << "format" << 2 << IdxExpr->getSourceRange(); 1532 return; 1533 } 1534 1535 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { 1536 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 1537 << "format" << 2 << IdxExpr->getSourceRange(); 1538 return; 1539 } 1540 1541 unsigned ArgIdx = Idx.getZExtValue() - 1; 1542 1543 if (HasImplicitThisParam) { 1544 if (ArgIdx == 0) { 1545 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) 1546 << "format_arg" << IdxExpr->getSourceRange(); 1547 return; 1548 } 1549 ArgIdx--; 1550 } 1551 1552 // make sure the format string is really a string 1553 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); 1554 1555 bool not_nsstring_type = !isNSStringType(Ty, S.Context); 1556 if (not_nsstring_type && 1557 !isCFStringType(Ty, S.Context) && 1558 (!Ty->isPointerType() || 1559 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { 1560 // FIXME: Should highlight the actual expression that has the wrong type. 1561 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1562 << (not_nsstring_type ? "a string type" : "an NSString") 1563 << IdxExpr->getSourceRange(); 1564 return; 1565 } 1566 Ty = getFunctionOrMethodResultType(d); 1567 if (!isNSStringType(Ty, S.Context) && 1568 !isCFStringType(Ty, S.Context) && 1569 (!Ty->isPointerType() || 1570 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { 1571 // FIXME: Should highlight the actual expression that has the wrong type. 1572 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) 1573 << (not_nsstring_type ? "string type" : "NSString") 1574 << IdxExpr->getSourceRange(); 1575 return; 1576 } 1577 1578 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, 1579 Idx.getZExtValue())); 1580} 1581 1582enum FormatAttrKind { 1583 CFStringFormat, 1584 NSStringFormat, 1585 StrftimeFormat, 1586 SupportedFormat, 1587 IgnoredFormat, 1588 InvalidFormat 1589}; 1590 1591/// getFormatAttrKind - Map from format attribute names to supported format 1592/// types. 1593static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { 1594 // Check for formats that get handled specially. 1595 if (Format == "NSString") 1596 return NSStringFormat; 1597 if (Format == "CFString") 1598 return CFStringFormat; 1599 if (Format == "strftime") 1600 return StrftimeFormat; 1601 1602 // Otherwise, check for supported formats. 1603 if (Format == "scanf" || Format == "printf" || Format == "printf0" || 1604 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || 1605 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || 1606 Format == "zcmn_err") 1607 return SupportedFormat; 1608 1609 if (Format == "gcc_diag" || Format == "gcc_cdiag" || 1610 Format == "gcc_cxxdiag" || Format == "gcc_tdiag") 1611 return IgnoredFormat; 1612 1613 return InvalidFormat; 1614} 1615 1616/// Handle __attribute__((init_priority(priority))) attributes based on 1617/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html 1618static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr, 1619 Sema &S) { 1620 if (!S.getLangOptions().CPlusPlus) { 1621 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); 1622 return; 1623 } 1624 1625 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) { 1626 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); 1627 Attr.setInvalid(); 1628 return; 1629 } 1630 QualType T = dyn_cast<VarDecl>(d)->getType(); 1631 if (S.Context.getAsArrayType(T)) 1632 T = S.Context.getBaseElementType(T); 1633 if (!T->getAs<RecordType>()) { 1634 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); 1635 Attr.setInvalid(); 1636 return; 1637 } 1638 1639 if (Attr.getNumArgs() != 1) { 1640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1641 Attr.setInvalid(); 1642 return; 1643 } 1644 Expr *priorityExpr = Attr.getArg(0); 1645 1646 llvm::APSInt priority(32); 1647 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || 1648 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { 1649 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 1650 << "init_priority" << priorityExpr->getSourceRange(); 1651 Attr.setInvalid(); 1652 return; 1653 } 1654 unsigned prioritynum = priority.getZExtValue(); 1655 if (prioritynum < 101 || prioritynum > 65535) { 1656 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) 1657 << priorityExpr->getSourceRange(); 1658 Attr.setInvalid(); 1659 return; 1660 } 1661 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, 1662 prioritynum)); 1663} 1664 1665/// Handle __attribute__((format(type,idx,firstarg))) attributes based on 1666/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 1667static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1668 1669 if (!Attr.getParameterName()) { 1670 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1671 << "format" << 1; 1672 return; 1673 } 1674 1675 if (Attr.getNumArgs() != 2) { 1676 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; 1677 return; 1678 } 1679 1680 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { 1681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1682 << Attr.getName() << 0 /*function*/; 1683 return; 1684 } 1685 1686 // In C++ the implicit 'this' function parameter also counts, and they are 1687 // counted from one. 1688 bool HasImplicitThisParam = isInstanceMethod(d); 1689 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; 1690 unsigned FirstIdx = 1; 1691 1692 llvm::StringRef Format = Attr.getParameterName()->getName(); 1693 1694 // Normalize the argument, __foo__ becomes foo. 1695 if (Format.startswith("__") && Format.endswith("__")) 1696 Format = Format.substr(2, Format.size() - 4); 1697 1698 // Check for supported formats. 1699 FormatAttrKind Kind = getFormatAttrKind(Format); 1700 1701 if (Kind == IgnoredFormat) 1702 return; 1703 1704 if (Kind == InvalidFormat) { 1705 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1706 << "format" << Attr.getParameterName()->getName(); 1707 return; 1708 } 1709 1710 // checks for the 2nd argument 1711 Expr *IdxExpr = Attr.getArg(0); 1712 llvm::APSInt Idx(32); 1713 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || 1714 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { 1715 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1716 << "format" << 2 << IdxExpr->getSourceRange(); 1717 return; 1718 } 1719 1720 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { 1721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 1722 << "format" << 2 << IdxExpr->getSourceRange(); 1723 return; 1724 } 1725 1726 // FIXME: Do we need to bounds check? 1727 unsigned ArgIdx = Idx.getZExtValue() - 1; 1728 1729 if (HasImplicitThisParam) { 1730 if (ArgIdx == 0) { 1731 S.Diag(Attr.getLoc(), 1732 diag::err_format_attribute_implicit_this_format_string) 1733 << IdxExpr->getSourceRange(); 1734 return; 1735 } 1736 ArgIdx--; 1737 } 1738 1739 // make sure the format string is really a string 1740 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); 1741 1742 if (Kind == CFStringFormat) { 1743 if (!isCFStringType(Ty, S.Context)) { 1744 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1745 << "a CFString" << IdxExpr->getSourceRange(); 1746 return; 1747 } 1748 } else if (Kind == NSStringFormat) { 1749 // FIXME: do we need to check if the type is NSString*? What are the 1750 // semantics? 1751 if (!isNSStringType(Ty, S.Context)) { 1752 // FIXME: Should highlight the actual expression that has the wrong type. 1753 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1754 << "an NSString" << IdxExpr->getSourceRange(); 1755 return; 1756 } 1757 } else if (!Ty->isPointerType() || 1758 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { 1759 // FIXME: Should highlight the actual expression that has the wrong type. 1760 S.Diag(Attr.getLoc(), diag::err_format_attribute_not) 1761 << "a string type" << IdxExpr->getSourceRange(); 1762 return; 1763 } 1764 1765 // check the 3rd argument 1766 Expr *FirstArgExpr = Attr.getArg(1); 1767 llvm::APSInt FirstArg(32); 1768 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || 1769 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { 1770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 1771 << "format" << 3 << FirstArgExpr->getSourceRange(); 1772 return; 1773 } 1774 1775 // check if the function is variadic if the 3rd argument non-zero 1776 if (FirstArg != 0) { 1777 if (isFunctionOrMethodVariadic(d)) { 1778 ++NumArgs; // +1 for ... 1779 } else { 1780 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); 1781 return; 1782 } 1783 } 1784 1785 // strftime requires FirstArg to be 0 because it doesn't read from any 1786 // variable the input is just the current time + the format string. 1787 if (Kind == StrftimeFormat) { 1788 if (FirstArg != 0) { 1789 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) 1790 << FirstArgExpr->getSourceRange(); 1791 return; 1792 } 1793 // if 0 it disables parameter checking (to use with e.g. va_list) 1794 } else if (FirstArg != 0 && FirstArg != NumArgs) { 1795 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) 1796 << "format" << 3 << FirstArgExpr->getSourceRange(); 1797 return; 1798 } 1799 1800 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format, 1801 Idx.getZExtValue(), 1802 FirstArg.getZExtValue())); 1803} 1804 1805static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, 1806 Sema &S) { 1807 // check the attribute arguments. 1808 if (Attr.getNumArgs() != 0) { 1809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1810 return; 1811 } 1812 1813 // Try to find the underlying union declaration. 1814 RecordDecl *RD = 0; 1815 TypedefDecl *TD = dyn_cast<TypedefDecl>(d); 1816 if (TD && TD->getUnderlyingType()->isUnionType()) 1817 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); 1818 else 1819 RD = dyn_cast<RecordDecl>(d); 1820 1821 if (!RD || !RD->isUnion()) { 1822 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 1823 << Attr.getName() << 1 /*union*/; 1824 return; 1825 } 1826 1827 if (!RD->isDefinition()) { 1828 S.Diag(Attr.getLoc(), 1829 diag::warn_transparent_union_attribute_not_definition); 1830 return; 1831 } 1832 1833 RecordDecl::field_iterator Field = RD->field_begin(), 1834 FieldEnd = RD->field_end(); 1835 if (Field == FieldEnd) { 1836 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); 1837 return; 1838 } 1839 1840 FieldDecl *FirstField = *Field; 1841 QualType FirstType = FirstField->getType(); 1842 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { 1843 S.Diag(FirstField->getLocation(), 1844 diag::warn_transparent_union_attribute_floating) 1845 << FirstType->isVectorType() << FirstType; 1846 return; 1847 } 1848 1849 uint64_t FirstSize = S.Context.getTypeSize(FirstType); 1850 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); 1851 for (; Field != FieldEnd; ++Field) { 1852 QualType FieldType = Field->getType(); 1853 if (S.Context.getTypeSize(FieldType) != FirstSize || 1854 S.Context.getTypeAlign(FieldType) != FirstAlign) { 1855 // Warn if we drop the attribute. 1856 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; 1857 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) 1858 : S.Context.getTypeAlign(FieldType); 1859 S.Diag(Field->getLocation(), 1860 diag::warn_transparent_union_attribute_field_size_align) 1861 << isSize << Field->getDeclName() << FieldBits; 1862 unsigned FirstBits = isSize? FirstSize : FirstAlign; 1863 S.Diag(FirstField->getLocation(), 1864 diag::note_transparent_union_first_field_size_align) 1865 << isSize << FirstBits; 1866 return; 1867 } 1868 } 1869 1870 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context)); 1871} 1872 1873static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { 1874 // check the attribute arguments. 1875 if (Attr.getNumArgs() != 1) { 1876 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1877 return; 1878 } 1879 Expr *ArgExpr = Attr.getArg(0); 1880 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); 1881 1882 // Make sure that there is a string literal as the annotation's single 1883 // argument. 1884 if (!SE) { 1885 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; 1886 return; 1887 } 1888 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, 1889 SE->getString())); 1890} 1891 1892static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) { 1893 // check the attribute arguments. 1894 if (Attr.getNumArgs() > 1) { 1895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1896 return; 1897 } 1898 1899 //FIXME: The C++0x version of this attribute has more limited applicabilty 1900 // than GNU's, and should error out when it is used to specify a 1901 // weaker alignment, rather than being silently ignored. 1902 1903 if (Attr.getNumArgs() == 0) { 1904 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0)); 1905 return; 1906 } 1907 1908 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0)); 1909} 1910 1911void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { 1912 if (E->isTypeDependent() || E->isValueDependent()) { 1913 // Save dependent expressions in the AST to be instantiated. 1914 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); 1915 return; 1916 } 1917 1918 // FIXME: Cache the number on the Attr object? 1919 llvm::APSInt Alignment(32); 1920 if (!E->isIntegerConstantExpr(Alignment, Context)) { 1921 Diag(AttrLoc, diag::err_attribute_argument_not_int) 1922 << "aligned" << E->getSourceRange(); 1923 return; 1924 } 1925 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { 1926 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) 1927 << E->getSourceRange(); 1928 return; 1929 } 1930 1931 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); 1932} 1933 1934void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) { 1935 // FIXME: Cache the number on the Attr object if non-dependent? 1936 // FIXME: Perform checking of type validity 1937 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS)); 1938 return; 1939} 1940 1941/// HandleModeAttr - This attribute modifies the width of a decl with primitive 1942/// type. 1943/// 1944/// Despite what would be logical, the mode attribute is a decl attribute, not a 1945/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be 1946/// HImode, not an intermediate pointer. 1947static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { 1948 // This attribute isn't documented, but glibc uses it. It changes 1949 // the width of an int or unsigned int to the specified size. 1950 1951 // Check that there aren't any arguments 1952 if (Attr.getNumArgs() != 0) { 1953 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1954 return; 1955 } 1956 1957 IdentifierInfo *Name = Attr.getParameterName(); 1958 if (!Name) { 1959 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); 1960 return; 1961 } 1962 1963 llvm::StringRef Str = Attr.getParameterName()->getName(); 1964 1965 // Normalize the attribute name, __foo__ becomes foo. 1966 if (Str.startswith("__") && Str.endswith("__")) 1967 Str = Str.substr(2, Str.size() - 4); 1968 1969 unsigned DestWidth = 0; 1970 bool IntegerMode = true; 1971 bool ComplexMode = false; 1972 switch (Str.size()) { 1973 case 2: 1974 switch (Str[0]) { 1975 case 'Q': DestWidth = 8; break; 1976 case 'H': DestWidth = 16; break; 1977 case 'S': DestWidth = 32; break; 1978 case 'D': DestWidth = 64; break; 1979 case 'X': DestWidth = 96; break; 1980 case 'T': DestWidth = 128; break; 1981 } 1982 if (Str[1] == 'F') { 1983 IntegerMode = false; 1984 } else if (Str[1] == 'C') { 1985 IntegerMode = false; 1986 ComplexMode = true; 1987 } else if (Str[1] != 'I') { 1988 DestWidth = 0; 1989 } 1990 break; 1991 case 4: 1992 // FIXME: glibc uses 'word' to define register_t; this is narrower than a 1993 // pointer on PIC16 and other embedded platforms. 1994 if (Str == "word") 1995 DestWidth = S.Context.Target.getPointerWidth(0); 1996 else if (Str == "byte") 1997 DestWidth = S.Context.Target.getCharWidth(); 1998 break; 1999 case 7: 2000 if (Str == "pointer") 2001 DestWidth = S.Context.Target.getPointerWidth(0); 2002 break; 2003 } 2004 2005 QualType OldTy; 2006 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) 2007 OldTy = TD->getUnderlyingType(); 2008 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) 2009 OldTy = VD->getType(); 2010 else { 2011 S.Diag(D->getLocation(), diag::err_attr_wrong_decl) 2012 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); 2013 return; 2014 } 2015 2016 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) 2017 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); 2018 else if (IntegerMode) { 2019 if (!OldTy->isIntegralOrEnumerationType()) 2020 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 2021 } else if (ComplexMode) { 2022 if (!OldTy->isComplexType()) 2023 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 2024 } else { 2025 if (!OldTy->isFloatingType()) 2026 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); 2027 } 2028 2029 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t 2030 // and friends, at least with glibc. 2031 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong 2032 // width on unusual platforms. 2033 // FIXME: Make sure floating-point mappings are accurate 2034 // FIXME: Support XF and TF types 2035 QualType NewTy; 2036 switch (DestWidth) { 2037 case 0: 2038 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; 2039 return; 2040 default: 2041 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2042 return; 2043 case 8: 2044 if (!IntegerMode) { 2045 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2046 return; 2047 } 2048 if (OldTy->isSignedIntegerType()) 2049 NewTy = S.Context.SignedCharTy; 2050 else 2051 NewTy = S.Context.UnsignedCharTy; 2052 break; 2053 case 16: 2054 if (!IntegerMode) { 2055 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2056 return; 2057 } 2058 if (OldTy->isSignedIntegerType()) 2059 NewTy = S.Context.ShortTy; 2060 else 2061 NewTy = S.Context.UnsignedShortTy; 2062 break; 2063 case 32: 2064 if (!IntegerMode) 2065 NewTy = S.Context.FloatTy; 2066 else if (OldTy->isSignedIntegerType()) 2067 NewTy = S.Context.IntTy; 2068 else 2069 NewTy = S.Context.UnsignedIntTy; 2070 break; 2071 case 64: 2072 if (!IntegerMode) 2073 NewTy = S.Context.DoubleTy; 2074 else if (OldTy->isSignedIntegerType()) 2075 if (S.Context.Target.getLongWidth() == 64) 2076 NewTy = S.Context.LongTy; 2077 else 2078 NewTy = S.Context.LongLongTy; 2079 else 2080 if (S.Context.Target.getLongWidth() == 64) 2081 NewTy = S.Context.UnsignedLongTy; 2082 else 2083 NewTy = S.Context.UnsignedLongLongTy; 2084 break; 2085 case 96: 2086 NewTy = S.Context.LongDoubleTy; 2087 break; 2088 case 128: 2089 if (!IntegerMode) { 2090 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; 2091 return; 2092 } 2093 if (OldTy->isSignedIntegerType()) 2094 NewTy = S.Context.Int128Ty; 2095 else 2096 NewTy = S.Context.UnsignedInt128Ty; 2097 break; 2098 } 2099 2100 if (ComplexMode) { 2101 NewTy = S.Context.getComplexType(NewTy); 2102 } 2103 2104 // Install the new type. 2105 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { 2106 // FIXME: preserve existing source info. 2107 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); 2108 } else 2109 cast<ValueDecl>(D)->setType(NewTy); 2110} 2111 2112static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2113 // check the attribute arguments. 2114 if (Attr.getNumArgs() > 0) { 2115 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2116 return; 2117 } 2118 2119 if (!isFunctionOrMethod(d)) { 2120 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2121 << Attr.getName() << 0 /*function*/; 2122 return; 2123 } 2124 2125 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context)); 2126} 2127 2128static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2129 // check the attribute arguments. 2130 if (Attr.getNumArgs() != 0) { 2131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2132 return; 2133 } 2134 2135 if (!isa<FunctionDecl>(d)) { 2136 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2137 << Attr.getName() << 0 /*function*/; 2138 return; 2139 } 2140 2141 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context)); 2142} 2143 2144static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr, 2145 Sema &S) { 2146 // check the attribute arguments. 2147 if (Attr.getNumArgs() != 0) { 2148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2149 return; 2150 } 2151 2152 if (!isa<FunctionDecl>(d)) { 2153 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2154 << Attr.getName() << 0 /*function*/; 2155 return; 2156 } 2157 2158 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), 2159 S.Context)); 2160} 2161 2162static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2163 if (S.LangOpts.CUDA) { 2164 // check the attribute arguments. 2165 if (Attr.getNumArgs() != 0) { 2166 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2167 return; 2168 } 2169 2170 if (!isa<VarDecl>(d)) { 2171 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2172 << Attr.getName() << 12 /*variable*/; 2173 return; 2174 } 2175 2176 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context)); 2177 } else { 2178 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; 2179 } 2180} 2181 2182static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2183 if (S.LangOpts.CUDA) { 2184 // check the attribute arguments. 2185 if (Attr.getNumArgs() != 0) { 2186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2187 return; 2188 } 2189 2190 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) { 2191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2192 << Attr.getName() << 2 /*variable and function*/; 2193 return; 2194 } 2195 2196 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context)); 2197 } else { 2198 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; 2199 } 2200} 2201 2202static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2203 if (S.LangOpts.CUDA) { 2204 // check the attribute arguments. 2205 if (Attr.getNumArgs() != 0) { 2206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2207 return; 2208 } 2209 2210 if (!isa<FunctionDecl>(d)) { 2211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2212 << Attr.getName() << 0 /*function*/; 2213 return; 2214 } 2215 2216 FunctionDecl *FD = cast<FunctionDecl>(d); 2217 if (!FD->getResultType()->isVoidType()) { 2218 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); 2219 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { 2220 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) 2221 << FD->getType() 2222 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), 2223 "void"); 2224 } else { 2225 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) 2226 << FD->getType(); 2227 } 2228 return; 2229 } 2230 2231 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context)); 2232 } else { 2233 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; 2234 } 2235} 2236 2237static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2238 if (S.LangOpts.CUDA) { 2239 // check the attribute arguments. 2240 if (Attr.getNumArgs() != 0) { 2241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2242 return; 2243 } 2244 2245 if (!isa<FunctionDecl>(d)) { 2246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2247 << Attr.getName() << 0 /*function*/; 2248 return; 2249 } 2250 2251 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context)); 2252 } else { 2253 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; 2254 } 2255} 2256 2257static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2258 if (S.LangOpts.CUDA) { 2259 // check the attribute arguments. 2260 if (Attr.getNumArgs() != 0) { 2261 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2262 return; 2263 } 2264 2265 if (!isa<VarDecl>(d)) { 2266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2267 << Attr.getName() << 12 /*variable*/; 2268 return; 2269 } 2270 2271 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context)); 2272 } else { 2273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; 2274 } 2275} 2276 2277static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2278 // check the attribute arguments. 2279 if (Attr.getNumArgs() != 0) { 2280 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2281 return; 2282 } 2283 2284 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); 2285 if (Fn == 0) { 2286 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2287 << Attr.getName() << 0 /*function*/; 2288 return; 2289 } 2290 2291 if (!Fn->isInlineSpecified()) { 2292 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); 2293 return; 2294 } 2295 2296 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context)); 2297} 2298 2299static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) { 2300 if (hasDeclarator(d)) return; 2301 2302 // Diagnostic is emitted elsewhere: here we store the (valid) attr 2303 // in the Decl node for syntactic reasoning, e.g., pretty-printing. 2304 CallingConv CC; 2305 if (S.CheckCallingConvAttr(attr, CC)) 2306 return; 2307 2308 if (!isa<ObjCMethodDecl>(d)) { 2309 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2310 << attr.getName() << 0 /*function*/; 2311 return; 2312 } 2313 2314 switch (attr.getKind()) { 2315 case AttributeList::AT_fastcall: 2316 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context)); 2317 return; 2318 case AttributeList::AT_stdcall: 2319 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context)); 2320 return; 2321 case AttributeList::AT_thiscall: 2322 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context)); 2323 return; 2324 case AttributeList::AT_cdecl: 2325 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context)); 2326 return; 2327 case AttributeList::AT_pascal: 2328 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context)); 2329 return; 2330 default: 2331 llvm_unreachable("unexpected attribute kind"); 2332 return; 2333 } 2334} 2335 2336static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){ 2337 assert(Attr.isInvalid() == false); 2338 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context)); 2339} 2340 2341bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { 2342 if (attr.isInvalid()) 2343 return true; 2344 2345 if (attr.getNumArgs() != 0) { 2346 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 2347 attr.setInvalid(); 2348 return true; 2349 } 2350 2351 // TODO: diagnose uses of these conventions on the wrong target. 2352 switch (attr.getKind()) { 2353 case AttributeList::AT_cdecl: CC = CC_C; break; 2354 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; 2355 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; 2356 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; 2357 case AttributeList::AT_pascal: CC = CC_X86Pascal; break; 2358 default: llvm_unreachable("unexpected attribute kind"); return true; 2359 } 2360 2361 return false; 2362} 2363 2364static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) { 2365 if (hasDeclarator(d)) return; 2366 2367 unsigned numParams; 2368 if (S.CheckRegparmAttr(attr, numParams)) 2369 return; 2370 2371 if (!isa<ObjCMethodDecl>(d)) { 2372 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2373 << attr.getName() << 0 /*function*/; 2374 return; 2375 } 2376 2377 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams)); 2378} 2379 2380/// Checks a regparm attribute, returning true if it is ill-formed and 2381/// otherwise setting numParams to the appropriate value. 2382bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) { 2383 if (attr.isInvalid()) 2384 return true; 2385 2386 if (attr.getNumArgs() != 1) { 2387 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2388 attr.setInvalid(); 2389 return true; 2390 } 2391 2392 Expr *NumParamsExpr = attr.getArg(0); 2393 llvm::APSInt NumParams(32); 2394 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || 2395 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { 2396 Diag(attr.getLoc(), diag::err_attribute_argument_not_int) 2397 << "regparm" << NumParamsExpr->getSourceRange(); 2398 attr.setInvalid(); 2399 return true; 2400 } 2401 2402 if (Context.Target.getRegParmMax() == 0) { 2403 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform) 2404 << NumParamsExpr->getSourceRange(); 2405 attr.setInvalid(); 2406 return true; 2407 } 2408 2409 numParams = NumParams.getZExtValue(); 2410 if (numParams > Context.Target.getRegParmMax()) { 2411 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number) 2412 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); 2413 attr.setInvalid(); 2414 return true; 2415 } 2416 2417 return false; 2418} 2419 2420static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){ 2421 if (S.LangOpts.CUDA) { 2422 // check the attribute arguments. 2423 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { 2424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) 2425 << "1 or 2"; 2426 return; 2427 } 2428 2429 if (!isFunctionOrMethod(d)) { 2430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) 2431 << Attr.getName() << 0 /*function*/; 2432 return; 2433 } 2434 2435 Expr *MaxThreadsExpr = Attr.getArg(0); 2436 llvm::APSInt MaxThreads(32); 2437 if (MaxThreadsExpr->isTypeDependent() || 2438 MaxThreadsExpr->isValueDependent() || 2439 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { 2440 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 2441 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); 2442 return; 2443 } 2444 2445 llvm::APSInt MinBlocks(32); 2446 if (Attr.getNumArgs() > 1) { 2447 Expr *MinBlocksExpr = Attr.getArg(1); 2448 if (MinBlocksExpr->isTypeDependent() || 2449 MinBlocksExpr->isValueDependent() || 2450 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { 2451 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) 2452 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); 2453 return; 2454 } 2455 } 2456 2457 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context, 2458 MaxThreads.getZExtValue(), 2459 MinBlocks.getZExtValue())); 2460 } else { 2461 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; 2462 } 2463} 2464 2465//===----------------------------------------------------------------------===// 2466// Checker-specific attribute handlers. 2467//===----------------------------------------------------------------------===// 2468 2469static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { 2470 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); 2471} 2472static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { 2473 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); 2474} 2475 2476static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) { 2477 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d); 2478 if (!param) { 2479 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) 2480 << SourceRange(attr.getLoc()) << attr.getName() << 4 /*parameter*/; 2481 return; 2482 } 2483 2484 bool typeOK, cf; 2485 if (attr.getKind() == AttributeList::AT_ns_consumed) { 2486 typeOK = isValidSubjectOfNSAttribute(S, param->getType()); 2487 cf = false; 2488 } else { 2489 typeOK = isValidSubjectOfCFAttribute(S, param->getType()); 2490 cf = true; 2491 } 2492 2493 if (!typeOK) { 2494 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) 2495 << SourceRange(attr.getLoc()) << attr.getName() << cf; 2496 return; 2497 } 2498 2499 if (cf) 2500 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context)); 2501 else 2502 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context)); 2503} 2504 2505static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr, 2506 Sema &S) { 2507 if (!isa<ObjCMethodDecl>(d)) { 2508 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) 2509 << SourceRange(attr.getLoc()) << attr.getName() << 13 /*method*/; 2510 return; 2511 } 2512 2513 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context)); 2514} 2515 2516static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr, 2517 Sema &S) { 2518 2519 QualType returnType; 2520 2521 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) 2522 returnType = MD->getResultType(); 2523 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) 2524 returnType = FD->getResultType(); 2525 else { 2526 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) 2527 << SourceRange(attr.getLoc()) << attr.getName() 2528 << 3 /* function or method */; 2529 return; 2530 } 2531 2532 bool typeOK; 2533 bool cf; 2534 switch (attr.getKind()) { 2535 default: llvm_unreachable("invalid ownership attribute"); return; 2536 case AttributeList::AT_ns_returns_autoreleased: 2537 case AttributeList::AT_ns_returns_retained: 2538 case AttributeList::AT_ns_returns_not_retained: 2539 typeOK = isValidSubjectOfNSAttribute(S, returnType); 2540 cf = false; 2541 break; 2542 2543 case AttributeList::AT_cf_returns_retained: 2544 case AttributeList::AT_cf_returns_not_retained: 2545 typeOK = isValidSubjectOfCFAttribute(S, returnType); 2546 cf = true; 2547 break; 2548 } 2549 2550 if (!typeOK) { 2551 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) 2552 << SourceRange(attr.getLoc()) 2553 << attr.getName() << isa<ObjCMethodDecl>(d) << cf; 2554 return; 2555 } 2556 2557 switch (attr.getKind()) { 2558 default: 2559 assert(0 && "invalid ownership attribute"); 2560 return; 2561 case AttributeList::AT_ns_returns_autoreleased: 2562 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(), 2563 S.Context)); 2564 return; 2565 case AttributeList::AT_cf_returns_not_retained: 2566 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(), 2567 S.Context)); 2568 return; 2569 case AttributeList::AT_ns_returns_not_retained: 2570 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(), 2571 S.Context)); 2572 return; 2573 case AttributeList::AT_cf_returns_retained: 2574 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(), 2575 S.Context)); 2576 return; 2577 case AttributeList::AT_ns_returns_retained: 2578 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(), 2579 S.Context)); 2580 return; 2581 }; 2582} 2583 2584static bool isKnownDeclSpecAttr(const AttributeList &Attr) { 2585 return Attr.getKind() == AttributeList::AT_dllimport || 2586 Attr.getKind() == AttributeList::AT_dllexport || 2587 Attr.getKind() == AttributeList::AT_uuid; 2588} 2589 2590//===----------------------------------------------------------------------===// 2591// Microsoft specific attribute handlers. 2592//===----------------------------------------------------------------------===// 2593 2594static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) { 2595 if (S.LangOpts.Microsoft || S.LangOpts.Borland) { 2596 // check the attribute arguments. 2597 if (Attr.getNumArgs() != 1) { 2598 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2599 return; 2600 } 2601 Expr *Arg = Attr.getArg(0); 2602 StringLiteral *Str = dyn_cast<StringLiteral>(Arg); 2603 if (Str == 0 || Str->isWide()) { 2604 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 2605 << "uuid" << 1; 2606 return; 2607 } 2608 2609 llvm::StringRef StrRef = Str->getString(); 2610 2611 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && 2612 StrRef.back() == '}'; 2613 2614 // Validate GUID length. 2615 if (IsCurly && StrRef.size() != 38) { 2616 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 2617 return; 2618 } 2619 if (!IsCurly && StrRef.size() != 36) { 2620 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 2621 return; 2622 } 2623 2624 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or 2625 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" 2626 llvm::StringRef::iterator I = StrRef.begin(); 2627 if (IsCurly) // Skip the optional '{' 2628 ++I; 2629 2630 for (int i = 0; i < 36; ++i) { 2631 if (i == 8 || i == 13 || i == 18 || i == 23) { 2632 if (*I != '-') { 2633 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 2634 return; 2635 } 2636 } else if (!isxdigit(*I)) { 2637 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); 2638 return; 2639 } 2640 I++; 2641 } 2642 2643 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context, 2644 Str->getString())); 2645 } else 2646 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; 2647} 2648 2649//===----------------------------------------------------------------------===// 2650// Top Level Sema Entry Points 2651//===----------------------------------------------------------------------===// 2652 2653static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D, 2654 const AttributeList &Attr, Sema &S) { 2655 switch (Attr.getKind()) { 2656 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break; 2657 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break; 2658 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; 2659 default: 2660 break; 2661 } 2662} 2663 2664static void ProcessInheritableDeclAttr(Scope *scope, Decl *D, 2665 const AttributeList &Attr, Sema &S) { 2666 switch (Attr.getKind()) { 2667 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; 2668 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; 2669 case AttributeList::AT_IBOutletCollection: 2670 HandleIBOutletCollection(D, Attr, S); break; 2671 case AttributeList::AT_address_space: 2672 case AttributeList::AT_objc_gc: 2673 case AttributeList::AT_vector_size: 2674 case AttributeList::AT_neon_vector_type: 2675 case AttributeList::AT_neon_polyvector_type: 2676 // Ignore these, these are type attributes, handled by 2677 // ProcessTypeAttributes. 2678 break; 2679 case AttributeList::AT_device: 2680 case AttributeList::AT_host: 2681 case AttributeList::AT_overloadable: 2682 // Ignore, this is a non-inheritable attribute, handled 2683 // by ProcessNonInheritableDeclAttr. 2684 break; 2685 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; 2686 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; 2687 case AttributeList::AT_always_inline: 2688 HandleAlwaysInlineAttr (D, Attr, S); break; 2689 case AttributeList::AT_analyzer_noreturn: 2690 HandleAnalyzerNoReturnAttr (D, Attr, S); break; 2691 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; 2692 case AttributeList::AT_carries_dependency: 2693 HandleDependencyAttr (D, Attr, S); break; 2694 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break; 2695 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break; 2696 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; 2697 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; 2698 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; 2699 case AttributeList::AT_ext_vector_type: 2700 HandleExtVectorTypeAttr(scope, D, Attr, S); 2701 break; 2702 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; 2703 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; 2704 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break; 2705 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; 2706 case AttributeList::AT_launch_bounds: 2707 HandleLaunchBoundsAttr(D, Attr, S); 2708 break; 2709 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; 2710 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; 2711 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break; 2712 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break; 2713 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; 2714 case AttributeList::AT_ownership_returns: 2715 case AttributeList::AT_ownership_takes: 2716 case AttributeList::AT_ownership_holds: 2717 HandleOwnershipAttr (D, Attr, S); break; 2718 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break; 2719 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; 2720 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; 2721 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break; 2722 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break; 2723 2724 // Checker-specific. 2725 case AttributeList::AT_cf_consumed: 2726 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break; 2727 case AttributeList::AT_ns_consumes_self: 2728 HandleNSConsumesSelfAttr(D, Attr, S); break; 2729 2730 case AttributeList::AT_ns_returns_autoreleased: 2731 case AttributeList::AT_ns_returns_not_retained: 2732 case AttributeList::AT_cf_returns_not_retained: 2733 case AttributeList::AT_ns_returns_retained: 2734 case AttributeList::AT_cf_returns_retained: 2735 HandleNSReturnsRetainedAttr(D, Attr, S); break; 2736 2737 case AttributeList::AT_reqd_wg_size: 2738 HandleReqdWorkGroupSize(D, Attr, S); break; 2739 2740 case AttributeList::AT_init_priority: 2741 HandleInitPriorityAttr(D, Attr, S); break; 2742 2743 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; 2744 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; 2745 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; 2746 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; 2747 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; 2748 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; 2749 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); 2750 break; 2751 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; 2752 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break; 2753 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; 2754 case AttributeList::AT_transparent_union: 2755 HandleTransparentUnionAttr(D, Attr, S); 2756 break; 2757 case AttributeList::AT_objc_exception: 2758 HandleObjCExceptionAttr(D, Attr, S); 2759 break; 2760 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; 2761 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; 2762 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; 2763 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; 2764 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; 2765 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; 2766 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; 2767 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; 2768 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; 2769 case AttributeList::IgnoredAttribute: 2770 // Just ignore 2771 break; 2772 case AttributeList::AT_no_instrument_function: // Interacts with -pg. 2773 HandleNoInstrumentFunctionAttr(D, Attr, S); 2774 break; 2775 case AttributeList::AT_stdcall: 2776 case AttributeList::AT_cdecl: 2777 case AttributeList::AT_fastcall: 2778 case AttributeList::AT_thiscall: 2779 case AttributeList::AT_pascal: 2780 HandleCallConvAttr(D, Attr, S); 2781 break; 2782 case AttributeList::AT_opencl_kernel_function: 2783 HandleOpenCLKernelAttr(D, Attr, S); 2784 break; 2785 case AttributeList::AT_uuid: 2786 HandleUuidAttr(D, Attr, S); 2787 break; 2788 default: 2789 // Ask target about the attribute. 2790 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); 2791 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) 2792 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) 2793 << Attr.getName(); 2794 break; 2795 } 2796} 2797 2798/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if 2799/// the attribute applies to decls. If the attribute is a type attribute, just 2800/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to 2801/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). 2802static void ProcessDeclAttribute(Scope *scope, Decl *D, 2803 const AttributeList &Attr, Sema &S, 2804 bool NonInheritable, bool Inheritable) { 2805 if (Attr.isInvalid()) 2806 return; 2807 2808 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) 2809 // FIXME: Try to deal with other __declspec attributes! 2810 return; 2811 2812 if (NonInheritable) 2813 ProcessNonInheritableDeclAttr(scope, D, Attr, S); 2814 2815 if (Inheritable) 2816 ProcessInheritableDeclAttr(scope, D, Attr, S); 2817} 2818 2819/// ProcessDeclAttributeList - Apply all the decl attributes in the specified 2820/// attribute list to the specified decl, ignoring any type attributes. 2821void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, 2822 const AttributeList *AttrList, 2823 bool NonInheritable, bool Inheritable) { 2824 for (const AttributeList* l = AttrList; l; l = l->getNext()) { 2825 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable); 2826 } 2827 2828 // GCC accepts 2829 // static int a9 __attribute__((weakref)); 2830 // but that looks really pointless. We reject it. 2831 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { 2832 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << 2833 dyn_cast<NamedDecl>(D)->getNameAsString(); 2834 return; 2835 } 2836} 2837 2838/// DeclClonePragmaWeak - clone existing decl (maybe definition), 2839/// #pragma weak needs a non-definition decl and source may not have one 2840NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { 2841 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); 2842 NamedDecl *NewD = 0; 2843 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 2844 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), 2845 FD->getLocation(), DeclarationName(II), 2846 FD->getType(), FD->getTypeSourceInfo()); 2847 if (FD->getQualifier()) { 2848 FunctionDecl *NewFD = cast<FunctionDecl>(NewD); 2849 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange()); 2850 } 2851 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { 2852 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), 2853 VD->getLocation(), II, 2854 VD->getType(), VD->getTypeSourceInfo(), 2855 VD->getStorageClass(), 2856 VD->getStorageClassAsWritten()); 2857 if (VD->getQualifier()) { 2858 VarDecl *NewVD = cast<VarDecl>(NewD); 2859 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange()); 2860 } 2861 } 2862 return NewD; 2863} 2864 2865/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak 2866/// applied to it, possibly with an alias. 2867void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { 2868 if (W.getUsed()) return; // only do this once 2869 W.setUsed(true); 2870 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) 2871 IdentifierInfo *NDId = ND->getIdentifier(); 2872 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); 2873 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, 2874 NDId->getName())); 2875 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); 2876 WeakTopLevelDecl.push_back(NewD); 2877 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin 2878 // to insert Decl at TU scope, sorry. 2879 DeclContext *SavedContext = CurContext; 2880 CurContext = Context.getTranslationUnitDecl(); 2881 PushOnScopeChains(NewD, S); 2882 CurContext = SavedContext; 2883 } else { // just add weak to existing 2884 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); 2885 } 2886} 2887 2888/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in 2889/// it, apply them to D. This is a bit tricky because PD can have attributes 2890/// specified in many different places, and we need to find and apply them all. 2891void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, 2892 bool NonInheritable, bool Inheritable) { 2893 // It's valid to "forward-declare" #pragma weak, in which case we 2894 // have to do this. 2895 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) { 2896 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 2897 if (IdentifierInfo *Id = ND->getIdentifier()) { 2898 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I 2899 = WeakUndeclaredIdentifiers.find(Id); 2900 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { 2901 WeakInfo W = I->second; 2902 DeclApplyPragmaWeak(S, ND, W); 2903 WeakUndeclaredIdentifiers[Id] = W; 2904 } 2905 } 2906 } 2907 } 2908 2909 // Apply decl attributes from the DeclSpec if present. 2910 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) 2911 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); 2912 2913 // Walk the declarator structure, applying decl attributes that were in a type 2914 // position to the decl itself. This handles cases like: 2915 // int *__attr__(x)** D; 2916 // when X is a decl attribute. 2917 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) 2918 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) 2919 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); 2920 2921 // Finally, apply any attributes on the decl itself. 2922 if (const AttributeList *Attrs = PD.getAttributes()) 2923 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); 2924} 2925 2926/// PushParsingDeclaration - Enter a new "scope" of deprecation 2927/// warnings. 2928/// 2929/// The state token we use is the start index of this scope 2930/// on the warning stack. 2931Sema::ParsingDeclStackState Sema::PushParsingDeclaration() { 2932 ParsingDeclDepth++; 2933 return (ParsingDeclStackState) DelayedDiagnostics.size(); 2934} 2935 2936void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) { 2937 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack"); 2938 ParsingDeclDepth--; 2939 2940 if (DelayedDiagnostics.empty()) 2941 return; 2942 2943 unsigned SavedIndex = (unsigned) S; 2944 assert(SavedIndex <= DelayedDiagnostics.size() && 2945 "saved index is out of bounds"); 2946 2947 unsigned E = DelayedDiagnostics.size(); 2948 2949 // We only want to actually emit delayed diagnostics when we 2950 // successfully parsed a decl. 2951 if (D) { 2952 // We really do want to start with 0 here. We get one push for a 2953 // decl spec and another for each declarator; in a decl group like: 2954 // deprecated_typedef foo, *bar, baz(); 2955 // only the declarator pops will be passed decls. This is correct; 2956 // we really do need to consider delayed diagnostics from the decl spec 2957 // for each of the different declarations. 2958 for (unsigned I = 0; I != E; ++I) { 2959 if (DelayedDiagnostics[I].Triggered) 2960 continue; 2961 2962 switch (DelayedDiagnostics[I].Kind) { 2963 case DelayedDiagnostic::Deprecation: 2964 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D); 2965 break; 2966 2967 case DelayedDiagnostic::Access: 2968 HandleDelayedAccessCheck(DelayedDiagnostics[I], D); 2969 break; 2970 } 2971 } 2972 } 2973 2974 // Destroy all the delayed diagnostics we're about to pop off. 2975 for (unsigned I = SavedIndex; I != E; ++I) 2976 DelayedDiagnostics[I].destroy(); 2977 2978 DelayedDiagnostics.set_size(SavedIndex); 2979} 2980 2981static bool isDeclDeprecated(Decl *D) { 2982 do { 2983 if (D->hasAttr<DeprecatedAttr>()) 2984 return true; 2985 } while ((D = cast_or_null<Decl>(D->getDeclContext()))); 2986 return false; 2987} 2988 2989void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, 2990 Decl *Ctx) { 2991 if (isDeclDeprecated(Ctx)) 2992 return; 2993 2994 DD.Triggered = true; 2995 if (!DD.getDeprecationMessage().empty()) 2996 Diag(DD.Loc, diag::warn_deprecated_message) 2997 << DD.getDeprecationDecl()->getDeclName() 2998 << DD.getDeprecationMessage(); 2999 else 3000 Diag(DD.Loc, diag::warn_deprecated) 3001 << DD.getDeprecationDecl()->getDeclName(); 3002} 3003 3004void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message, 3005 SourceLocation Loc, 3006 bool UnknownObjCClass) { 3007 // Delay if we're currently parsing a declaration. 3008 if (ParsingDeclDepth) { 3009 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D, 3010 Message)); 3011 return; 3012 } 3013 3014 // Otherwise, don't warn if our current context is deprecated. 3015 if (isDeclDeprecated(cast<Decl>(CurContext))) 3016 return; 3017 if (!Message.empty()) 3018 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() 3019 << Message; 3020 else { 3021 if (!UnknownObjCClass) 3022 Diag(Loc, diag::warn_deprecated) << D->getDeclName(); 3023 else 3024 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); 3025 } 3026} 3027