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