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