| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/AST/ASTConsumer.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ASTMutationListener.h" |
| 16 | #include "clang/AST/CXXInheritance.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/DeclTemplate.h" |
| 20 | #include "clang/AST/Expr.h" |
| 21 | #include "clang/AST/ExprCXX.h" |
| 22 | #include "clang/AST/Mangle.h" |
| 23 | #include "clang/AST/RecursiveASTVisitor.h" |
| 24 | #include "clang/Basic/CharInfo.h" |
| 25 | #include "clang/Basic/SourceManager.h" |
| 26 | #include "clang/Basic/TargetInfo.h" |
| 27 | #include "clang/Lex/Preprocessor.h" |
| 28 | #include "clang/Sema/DeclSpec.h" |
| 29 | #include "clang/Sema/DelayedDiagnostic.h" |
| 30 | #include "clang/Sema/Initialization.h" |
| 31 | #include "clang/Sema/Lookup.h" |
| 32 | #include "clang/Sema/Scope.h" |
| 33 | #include "clang/Sema/ScopeInfo.h" |
| 34 | #include "clang/Sema/SemaInternal.h" |
| 35 | #include "llvm/ADT/STLExtras.h" |
| 36 | #include "llvm/ADT/StringExtras.h" |
| 37 | #include "llvm/Support/MathExtras.h" |
| 38 | |
| 39 | using namespace clang; |
| 40 | using namespace sema; |
| 41 | |
| 42 | namespace AttributeLangSupport { |
| 43 | enum LANG { |
| 44 | C, |
| 45 | Cpp, |
| 46 | ObjC |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | static bool isFunctionOrMethod(const Decl *D) { |
| 58 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 64 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | static bool hasDeclarator(const Decl *D) { |
| 70 | |
| 71 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 72 | isa<ObjCPropertyDecl>(D); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | static bool hasFunctionProto(const Decl *D) { |
| 79 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 80 | return isa<FunctionProtoType>(FnTy); |
| 81 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
| 88 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 89 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
| 90 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 91 | return BD->getNumParams(); |
| 92 | return cast<ObjCMethodDecl>(D)->param_size(); |
| 93 | } |
| 94 | |
| 95 | static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D, |
| 96 | unsigned Idx) { |
| 97 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 98 | return FD->getParamDecl(Idx); |
| 99 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 100 | return MD->getParamDecl(Idx); |
| 101 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 102 | return BD->getParamDecl(Idx); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
| 107 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 108 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
| 109 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 110 | return BD->getParamDecl(Idx)->getType(); |
| 111 | |
| 112 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
| 113 | } |
| 114 | |
| 115 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 116 | if (auto *PVD = getFunctionOrMethodParam(D, Idx)) |
| 117 | return PVD->getSourceRange(); |
| 118 | return SourceRange(); |
| 119 | } |
| 120 | |
| 121 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 122 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 123 | return FnTy->getReturnType(); |
| 124 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
| 125 | } |
| 126 | |
| 127 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 128 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 129 | return FD->getReturnTypeSourceRange(); |
| 130 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 131 | return MD->getReturnTypeSourceRange(); |
| 132 | return SourceRange(); |
| 133 | } |
| 134 | |
| 135 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 136 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 137 | return cast<FunctionProtoType>(FnTy)->isVariadic(); |
| 138 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 139 | return BD->isVariadic(); |
| 140 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
| 141 | } |
| 142 | |
| 143 | static bool isInstanceMethod(const Decl *D) { |
| 144 | if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
| 145 | return MethodDecl->isInstance(); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
| 150 | const auto *PT = T->getAs<ObjCObjectPointerType>(); |
| 151 | if (!PT) |
| 152 | return false; |
| 153 | |
| 154 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 155 | if (!Cls) |
| 156 | return false; |
| 157 | |
| 158 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
| 159 | |
| 160 | |
| 161 | return ClsName == &Ctx.Idents.get("NSString") || |
| 162 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 163 | } |
| 164 | |
| 165 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
| 166 | const auto *PT = T->getAs<PointerType>(); |
| 167 | if (!PT) |
| 168 | return false; |
| 169 | |
| 170 | const auto *RT = PT->getPointeeType()->getAs<RecordType>(); |
| 171 | if (!RT) |
| 172 | return false; |
| 173 | |
| 174 | const RecordDecl *RD = RT->getDecl(); |
| 175 | if (RD->getTagKind() != TTK_Struct) |
| 176 | return false; |
| 177 | |
| 178 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 179 | } |
| 180 | |
| 181 | static unsigned getNumAttributeArgs(const ParsedAttr &AL) { |
| 182 | |
| 183 | return AL.getNumArgs() + AL.hasParsedType(); |
| 184 | } |
| 185 | |
| 186 | template <typename Compare> |
| 187 | static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL, |
| 188 | unsigned Num, unsigned Diag, |
| 189 | Compare Comp) { |
| 190 | if (Comp(getNumAttributeArgs(AL), Num)) { |
| 191 | S.Diag(AL.getLoc(), Diag) << AL << Num; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) { |
| 201 | return checkAttributeNumArgsImpl(S, AL, Num, |
| 202 | diag::err_attribute_wrong_number_arguments, |
| 203 | std::not_equal_to<unsigned>()); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL, |
| 209 | unsigned Num) { |
| 210 | return checkAttributeNumArgsImpl(S, AL, Num, |
| 211 | diag::err_attribute_too_few_arguments, |
| 212 | std::less<unsigned>()); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL, |
| 218 | unsigned Num) { |
| 219 | return checkAttributeNumArgsImpl(S, AL, Num, |
| 220 | diag::err_attribute_too_many_arguments, |
| 221 | std::greater<unsigned>()); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | template <typename AttrInfo> |
| 227 | static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value, |
| 228 | SourceLocation>::type |
| 229 | getAttrLoc(const AttrInfo &AL) { |
| 230 | return AL.getLocation(); |
| 231 | } |
| 232 | static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | template <typename AttrInfo> |
| 240 | static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
| 241 | uint32_t &Val, unsigned Idx = UINT_MAX, |
| 242 | bool StrictlyUnsigned = false) { |
| 243 | llvm::APSInt I(32); |
| 244 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 245 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 246 | if (Idx != UINT_MAX) |
| 247 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
| 248 | << &AI << Idx << AANT_ArgumentIntegerConstant |
| 249 | << Expr->getSourceRange(); |
| 250 | else |
| 251 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type) |
| 252 | << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange(); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | if (!I.isIntN(32)) { |
| 257 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 258 | << I.toString(10, false) << 32 << 1; |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | if (StrictlyUnsigned && I.isSigned() && I.isNegative()) { |
| 263 | S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer) |
| 264 | << &AI << 1; |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | Val = (uint32_t)I.getZExtValue(); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | template <typename AttrInfo> |
| 276 | static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
| 277 | int &Val, unsigned Idx = UINT_MAX) { |
| 278 | uint32_t UVal; |
| 279 | if (!checkUInt32Argument(S, AI, Expr, UVal, Idx)) |
| 280 | return false; |
| 281 | |
| 282 | if (UVal > (uint32_t)std::numeric_limits<int>::max()) { |
| 283 | llvm::APSInt I(32); |
| 284 | I = UVal; |
| 285 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 286 | << I.toString(10, false) << 32 << 0; |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | Val = UVal; |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | template <typename AttrTy> |
| 297 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 298 | if (const auto *A = D->getAttr<AttrTy>()) { |
| 299 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A; |
| 300 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
| 301 | return true; |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | template <typename AttrTy> |
| 307 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) { |
| 308 | if (const auto *A = D->getAttr<AttrTy>()) { |
| 309 | S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL |
| 310 | << A; |
| 311 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
| 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | template <typename AttrInfo> |
| 322 | static bool checkFunctionOrMethodParameterIndex( |
| 323 | Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum, |
| 324 | const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) { |
| 325 | assert(isFunctionOrMethodOrBlock(D)); |
| 326 | |
| 327 | |
| 328 | |
| 329 | bool HP = hasFunctionProto(D); |
| 330 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 331 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 332 | unsigned NumParams = |
| 333 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
| 334 | |
| 335 | llvm::APSInt IdxInt; |
| 336 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 337 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| 338 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
| 339 | << &AI << AttrArgNum << AANT_ArgumentIntegerConstant |
| 340 | << IdxExpr->getSourceRange(); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX); |
| 345 | if (IdxSource < 1 || (!IV && IdxSource > NumParams)) { |
| 346 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds) |
| 347 | << &AI << AttrArgNum << IdxExpr->getSourceRange(); |
| 348 | return false; |
| 349 | } |
| 350 | if (HasImplicitThisParam && !CanIndexImplicitThis) { |
| 351 | if (IdxSource == 1) { |
| 352 | S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument) |
| 353 | << &AI << IdxExpr->getSourceRange(); |
| 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | Idx = ParamIdx(IdxSource, D); |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum, |
| 367 | StringRef &Str, |
| 368 | SourceLocation *ArgLocation) { |
| 369 | |
| 370 | if (AL.isArgIdent(ArgNum)) { |
| 371 | IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum); |
| 372 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
| 373 | << AL << AANT_ArgumentString |
| 374 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
| 375 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
| 376 | Str = Loc->Ident->getName(); |
| 377 | if (ArgLocation) |
| 378 | *ArgLocation = Loc->Loc; |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | Expr *ArgExpr = AL.getArgAsExpr(ArgNum); |
| 384 | const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 385 | if (ArgLocation) |
| 386 | *ArgLocation = ArgExpr->getBeginLoc(); |
| 387 | |
| 388 | if (!Literal || !Literal->isAscii()) { |
| 389 | Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type) |
| 390 | << AL << AANT_ArgumentString; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | Str = Literal->getString(); |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | template <typename AttrType> |
| 401 | static void handleSimpleAttribute(Sema &S, Decl *D, SourceRange SR, |
| 402 | unsigned SpellingIndex) { |
| 403 | D->addAttr(::new (S.Context) AttrType(SR, S.Context, SpellingIndex)); |
| 404 | } |
| 405 | |
| 406 | template <typename AttrType> |
| 407 | static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 408 | handleSimpleAttribute<AttrType>(S, D, AL.getRange(), |
| 409 | AL.getAttributeSpellingListIndex()); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | template <typename... DiagnosticArgs> |
| 414 | static const Sema::SemaDiagnosticBuilder& |
| 415 | appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) { |
| 416 | return Bldr; |
| 417 | } |
| 418 | |
| 419 | template <typename T, typename... DiagnosticArgs> |
| 420 | static const Sema::SemaDiagnosticBuilder& |
| 421 | appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&, |
| 422 | DiagnosticArgs &&... ) { |
| 423 | return appendDiagnostics(Bldr << std::forward<T>(ExtraArg), |
| 424 | std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | <typename AttrType, typename... DiagnosticArgs> |
| 432 | static void |
| 433 | handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, SourceRange SR, |
| 434 | unsigned SpellingIndex, |
| 435 | bool PassesCheck, |
| 436 | unsigned DiagID, DiagnosticArgs&&... ) { |
| 437 | if (!PassesCheck) { |
| 438 | Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID); |
| 439 | appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 440 | return; |
| 441 | } |
| 442 | handleSimpleAttribute<AttrType>(S, D, SR, SpellingIndex); |
| 443 | } |
| 444 | |
| 445 | template <typename AttrType, typename... DiagnosticArgs> |
| 446 | static void |
| 447 | handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, const ParsedAttr &AL, |
| 448 | bool PassesCheck, |
| 449 | unsigned DiagID, |
| 450 | DiagnosticArgs&&... ) { |
| 451 | return handleSimpleAttributeOrDiagnose<AttrType>( |
| 452 | S, D, AL.getRange(), AL.getAttributeSpellingListIndex(), PassesCheck, |
| 453 | DiagID, std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 454 | } |
| 455 | |
| 456 | template <typename AttrType> |
| 457 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 458 | const ParsedAttr &AL) { |
| 459 | handleSimpleAttribute<AttrType>(S, D, AL); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | |
| 464 | template <typename AttrType, typename IncompatibleAttrType, |
| 465 | typename... IncompatibleAttrTypes> |
| 466 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 467 | const ParsedAttr &AL) { |
| 468 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL)) |
| 469 | return; |
| 470 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
| 471 | AL); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | static bool isIntOrBool(Expr *Exp) { |
| 476 | QualType QT = Exp->getType(); |
| 477 | return QT->isBooleanType() || QT->isIntegerType(); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 484 | auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record, |
| 485 | OverloadedOperatorKind Op) { |
| 486 | DeclContextLookupResult Result = |
| 487 | Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op)); |
| 488 | return !Result.empty(); |
| 489 | }; |
| 490 | |
| 491 | const RecordDecl *Record = RT->getDecl(); |
| 492 | bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star); |
| 493 | bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow); |
| 494 | if (foundStarOperator && foundArrowOperator) |
| 495 | return true; |
| 496 | |
| 497 | const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record); |
| 498 | if (!CXXRecord) |
| 499 | return false; |
| 500 | |
| 501 | for (auto BaseSpecifier : CXXRecord->bases()) { |
| 502 | if (!foundStarOperator) |
| 503 | foundStarOperator = IsOverloadedOperatorPresent( |
| 504 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Star); |
| 505 | if (!foundArrowOperator) |
| 506 | foundArrowOperator = IsOverloadedOperatorPresent( |
| 507 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow); |
| 508 | } |
| 509 | |
| 510 | if (foundStarOperator && foundArrowOperator) |
| 511 | return true; |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 520 | const ParsedAttr &AL) { |
| 521 | const auto *VD = cast<ValueDecl>(D); |
| 522 | QualType QT = VD->getType(); |
| 523 | if (QT->isAnyPointerType()) |
| 524 | return true; |
| 525 | |
| 526 | if (const auto *RT = QT->getAs<RecordType>()) { |
| 527 | |
| 528 | |
| 529 | |
| 530 | if (RT->isIncompleteType()) |
| 531 | return true; |
| 532 | |
| 533 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT; |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | |
| 543 | static const RecordType *getRecordType(QualType QT) { |
| 544 | if (const auto *RT = QT->getAs<RecordType>()) |
| 545 | return RT; |
| 546 | |
| 547 | |
| 548 | if (const auto *PT = QT->getAs<PointerType>()) |
| 549 | return PT->getPointeeType()->getAs<RecordType>(); |
| 550 | |
| 551 | return nullptr; |
| 552 | } |
| 553 | |
| 554 | template <typename AttrType> |
| 555 | static bool checkRecordDeclForAttr(const RecordDecl *RD) { |
| 556 | |
| 557 | if (RD->hasAttr<AttrType>()) |
| 558 | return true; |
| 559 | |
| 560 | |
| 561 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 562 | CXXBasePaths BPaths(false, false); |
| 563 | if (CRD->lookupInBases( |
| 564 | [](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 565 | const auto &Ty = *BS->getType(); |
| 566 | |
| 567 | if (Ty.isDependentType()) |
| 568 | return true; |
| 569 | return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>(); |
| 570 | }, |
| 571 | BPaths, true)) |
| 572 | return true; |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
| 578 | const RecordType *RT = getRecordType(Ty); |
| 579 | |
| 580 | if (!RT) |
| 581 | return false; |
| 582 | |
| 583 | |
| 584 | if (RT->isIncompleteType()) |
| 585 | return true; |
| 586 | |
| 587 | |
| 588 | |
| 589 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 590 | return true; |
| 591 | |
| 592 | return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl()); |
| 593 | } |
| 594 | |
| 595 | static bool checkTypedefTypeForCapability(QualType Ty) { |
| 596 | const auto *TD = Ty->getAs<TypedefType>(); |
| 597 | if (!TD) |
| 598 | return false; |
| 599 | |
| 600 | TypedefNameDecl *TN = TD->getDecl(); |
| 601 | if (!TN) |
| 602 | return false; |
| 603 | |
| 604 | return TN->hasAttr<CapabilityAttr>(); |
| 605 | } |
| 606 | |
| 607 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 608 | if (checkTypedefTypeForCapability(Ty)) |
| 609 | return true; |
| 610 | |
| 611 | if (checkRecordTypeForCapability(S, Ty)) |
| 612 | return true; |
| 613 | |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 624 | return isCapabilityExpr(S, E->getSubExpr()); |
| 625 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 626 | return isCapabilityExpr(S, E->getSubExpr()); |
| 627 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 628 | if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf || |
| 629 | E->getOpcode() == UO_Deref) |
| 630 | return isCapabilityExpr(S, E->getSubExpr()); |
| 631 | return false; |
| 632 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 633 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 634 | return isCapabilityExpr(S, E->getLHS()) && |
| 635 | isCapabilityExpr(S, E->getRHS()); |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | return typeHasCapability(S, Ex->getType()); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 648 | const ParsedAttr &AL, |
| 649 | SmallVectorImpl<Expr *> &Args, |
| 650 | unsigned Sidx = 0, |
| 651 | bool ParamIdxOk = false) { |
| 652 | if (Sidx == AL.getNumArgs()) { |
| 653 | |
| 654 | |
| 655 | |
| 656 | const auto *MD = dyn_cast<const CXXMethodDecl>(D); |
| 657 | if (MD && !MD->isStatic()) { |
| 658 | const CXXRecordDecl *RD = MD->getParent(); |
| 659 | |
| 660 | if (!checkRecordDeclForAttr<CapabilityAttr>(RD) && |
| 661 | !checkRecordDeclForAttr<ScopedLockableAttr>(RD)) |
| 662 | S.Diag(AL.getLoc(), |
| 663 | diag::warn_thread_attribute_not_on_capability_member) |
| 664 | << AL << MD->getParent(); |
| 665 | } else { |
| 666 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member) |
| 667 | << AL; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) { |
| 672 | Expr *ArgExp = AL.getArgAsExpr(Idx); |
| 673 | |
| 674 | if (ArgExp->isTypeDependent()) { |
| 675 | |
| 676 | Args.push_back(ArgExp); |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
| 681 | if (StrLit->getLength() == 0 || |
| 682 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
| 683 | |
| 684 | |
| 685 | Args.push_back(ArgExp); |
| 686 | continue; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL; |
| 692 | Args.push_back(ArgExp); |
| 693 | continue; |
| 694 | } |
| 695 | |
| 696 | QualType ArgTy = ArgExp->getType(); |
| 697 | |
| 698 | |
| 699 | |
| 700 | if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 701 | if (UOp->getOpcode() == UO_AddrOf) |
| 702 | if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 703 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 704 | ArgTy = DRE->getDecl()->getType(); |
| 705 | |
| 706 | |
| 707 | const RecordType *RT = getRecordType(ArgTy); |
| 708 | |
| 709 | |
| 710 | if(!RT && ParamIdxOk) { |
| 711 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| 712 | const auto *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 713 | if(FD && IL) { |
| 714 | unsigned int NumParams = FD->getNumParams(); |
| 715 | llvm::APInt ArgValue = IL->getValue(); |
| 716 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 717 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 718 | if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
| 719 | S.Diag(AL.getLoc(), |
| 720 | diag::err_attribute_argument_out_of_bounds_extra_info) |
| 721 | << AL << Idx + 1 << NumParams; |
| 722 | continue; |
| 723 | } |
| 724 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 733 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 734 | << AL << ArgTy; |
| 735 | |
| 736 | Args.push_back(ArgExp); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 745 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
| 746 | return; |
| 747 | |
| 748 | D->addAttr(::new (S.Context) |
| 749 | PtGuardedVarAttr(AL.getRange(), S.Context, |
| 750 | AL.getAttributeSpellingListIndex())); |
| 751 | } |
| 752 | |
| 753 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
| 754 | Expr *&Arg) { |
| 755 | SmallVector<Expr *, 1> Args; |
| 756 | |
| 757 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
| 758 | unsigned Size = Args.size(); |
| 759 | if (Size != 1) |
| 760 | return false; |
| 761 | |
| 762 | Arg = Args[0]; |
| 763 | |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 768 | Expr *Arg = nullptr; |
| 769 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
| 770 | return; |
| 771 | |
| 772 | D->addAttr(::new (S.Context) GuardedByAttr( |
| 773 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
| 774 | } |
| 775 | |
| 776 | static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 777 | Expr *Arg = nullptr; |
| 778 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
| 779 | return; |
| 780 | |
| 781 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
| 782 | return; |
| 783 | |
| 784 | D->addAttr(::new (S.Context) PtGuardedByAttr( |
| 785 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
| 786 | } |
| 787 | |
| 788 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
| 789 | SmallVectorImpl<Expr *> &Args) { |
| 790 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 791 | return false; |
| 792 | |
| 793 | |
| 794 | QualType QT = cast<ValueDecl>(D)->getType(); |
| 795 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 796 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL; |
| 797 | return false; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
| 802 | if (Args.empty()) |
| 803 | return false; |
| 804 | |
| 805 | return true; |
| 806 | } |
| 807 | |
| 808 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 809 | SmallVector<Expr *, 1> Args; |
| 810 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
| 811 | return; |
| 812 | |
| 813 | Expr **StartArg = &Args[0]; |
| 814 | D->addAttr(::new (S.Context) AcquiredAfterAttr( |
| 815 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 816 | AL.getAttributeSpellingListIndex())); |
| 817 | } |
| 818 | |
| 819 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 820 | SmallVector<Expr *, 1> Args; |
| 821 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
| 822 | return; |
| 823 | |
| 824 | Expr **StartArg = &Args[0]; |
| 825 | D->addAttr(::new (S.Context) AcquiredBeforeAttr( |
| 826 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 827 | AL.getAttributeSpellingListIndex())); |
| 828 | } |
| 829 | |
| 830 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
| 831 | SmallVectorImpl<Expr *> &Args) { |
| 832 | |
| 833 | |
| 834 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, ); |
| 835 | |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 840 | SmallVector<Expr *, 1> Args; |
| 841 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
| 842 | return; |
| 843 | |
| 844 | unsigned Size = Args.size(); |
| 845 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
| 846 | D->addAttr(::new (S.Context) |
| 847 | AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size, |
| 848 | AL.getAttributeSpellingListIndex())); |
| 849 | } |
| 850 | |
| 851 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 852 | const ParsedAttr &AL) { |
| 853 | SmallVector<Expr *, 1> Args; |
| 854 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
| 855 | return; |
| 856 | |
| 857 | unsigned Size = Args.size(); |
| 858 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
| 859 | D->addAttr(::new (S.Context) AssertExclusiveLockAttr( |
| 860 | AL.getRange(), S.Context, StartArg, Size, |
| 861 | AL.getAttributeSpellingListIndex())); |
| 862 | } |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | template <typename AttrInfo> |
| 870 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
| 871 | const AttrInfo &AI, unsigned AttrArgNo) { |
| 872 | (0) . __assert_fail ("AI.isArgExpr(AttrArgNo) && \"Expected expression argument\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 872, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument"); |
| 873 | Expr *AttrArg = AI.getArgAsExpr(AttrArgNo); |
| 874 | ParamIdx Idx; |
| 875 | if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg, |
| 876 | Idx)) |
| 877 | return false; |
| 878 | |
| 879 | const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex()); |
| 880 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
| 881 | SourceLocation SrcLoc = AttrArg->getBeginLoc(); |
| 882 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
| 883 | << AI << Param->getSourceRange(); |
| 884 | return false; |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 890 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 891 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
| 892 | return; |
| 893 | |
| 894 | const auto *FD = cast<FunctionDecl>(D); |
| 895 | if (!FD->getReturnType()->isPointerType()) { |
| 896 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL; |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | const Expr *SizeExpr = AL.getArgAsExpr(0); |
| 901 | int SizeArgNoVal; |
| 902 | |
| 903 | if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, )) |
| 904 | return; |
| 905 | if (!checkParamIsIntegerType(S, FD, AL, )) |
| 906 | return; |
| 907 | ParamIdx SizeArgNo(SizeArgNoVal, D); |
| 908 | |
| 909 | ParamIdx NumberArgNo; |
| 910 | if (AL.getNumArgs() == 2) { |
| 911 | const Expr *NumberExpr = AL.getArgAsExpr(1); |
| 912 | int Val; |
| 913 | |
| 914 | if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, )) |
| 915 | return; |
| 916 | if (!checkParamIsIntegerType(S, FD, AL, )) |
| 917 | return; |
| 918 | NumberArgNo = ParamIdx(Val, D); |
| 919 | } |
| 920 | |
| 921 | D->addAttr(::new (S.Context) |
| 922 | AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo, |
| 923 | AL.getAttributeSpellingListIndex())); |
| 924 | } |
| 925 | |
| 926 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
| 927 | SmallVectorImpl<Expr *> &Args) { |
| 928 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 929 | return false; |
| 930 | |
| 931 | if (!isIntOrBool(AL.getArgAsExpr(0))) { |
| 932 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 933 | << AL << 1 << AANT_ArgumentIntOrBool; |
| 934 | return false; |
| 935 | } |
| 936 | |
| 937 | |
| 938 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1); |
| 939 | |
| 940 | return true; |
| 941 | } |
| 942 | |
| 943 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
| 944 | const ParsedAttr &AL) { |
| 945 | SmallVector<Expr*, 2> Args; |
| 946 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
| 947 | return; |
| 948 | |
| 949 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr( |
| 950 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(), |
| 951 | AL.getAttributeSpellingListIndex())); |
| 952 | } |
| 953 | |
| 954 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
| 955 | const ParsedAttr &AL) { |
| 956 | SmallVector<Expr*, 2> Args; |
| 957 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
| 958 | return; |
| 959 | |
| 960 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 961 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), |
| 962 | Args.size(), AL.getAttributeSpellingListIndex())); |
| 963 | } |
| 964 | |
| 965 | static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 966 | |
| 967 | SmallVector<Expr*, 1> Args; |
| 968 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
| 969 | unsigned Size = Args.size(); |
| 970 | if (Size == 0) |
| 971 | return; |
| 972 | |
| 973 | D->addAttr(::new (S.Context) |
| 974 | LockReturnedAttr(AL.getRange(), S.Context, Args[0], |
| 975 | AL.getAttributeSpellingListIndex())); |
| 976 | } |
| 977 | |
| 978 | static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 979 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 980 | return; |
| 981 | |
| 982 | |
| 983 | SmallVector<Expr*, 1> Args; |
| 984 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
| 985 | unsigned Size = Args.size(); |
| 986 | if (Size == 0) |
| 987 | return; |
| 988 | Expr **StartArg = &Args[0]; |
| 989 | |
| 990 | D->addAttr(::new (S.Context) |
| 991 | LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size, |
| 992 | AL.getAttributeSpellingListIndex())); |
| 993 | } |
| 994 | |
| 995 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
| 996 | Expr *&Cond, StringRef &Msg) { |
| 997 | Cond = AL.getArgAsExpr(0); |
| 998 | if (!Cond->isTypeDependent()) { |
| 999 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 1000 | if (Converted.isInvalid()) |
| 1001 | return false; |
| 1002 | Cond = Converted.get(); |
| 1003 | } |
| 1004 | |
| 1005 | if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg)) |
| 1006 | return false; |
| 1007 | |
| 1008 | if (Msg.empty()) |
| 1009 | Msg = "<no message provided>"; |
| 1010 | |
| 1011 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 1012 | if (isa<FunctionDecl>(D) && !Cond->isValueDependent() && |
| 1013 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 1014 | Diags)) { |
| 1015 | S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL; |
| 1016 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 1017 | S.Diag(PDiag.first, PDiag.second); |
| 1018 | return false; |
| 1019 | } |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
| 1023 | static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1024 | S.Diag(AL.getLoc(), diag::ext_clang_enable_if); |
| 1025 | |
| 1026 | Expr *Cond; |
| 1027 | StringRef Msg; |
| 1028 | if (checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
| 1029 | D->addAttr(::new (S.Context) |
| 1030 | EnableIfAttr(AL.getRange(), S.Context, Cond, Msg, |
| 1031 | AL.getAttributeSpellingListIndex())); |
| 1032 | } |
| 1033 | |
| 1034 | namespace { |
| 1035 | |
| 1036 | |
| 1037 | class ArgumentDependenceChecker |
| 1038 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 1039 | #ifndef NDEBUG |
| 1040 | const CXXRecordDecl *ClassType; |
| 1041 | #endif |
| 1042 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 1043 | bool Result; |
| 1044 | |
| 1045 | public: |
| 1046 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 1047 | #ifndef NDEBUG |
| 1048 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1049 | ClassType = MD->getParent(); |
| 1050 | else |
| 1051 | ClassType = nullptr; |
| 1052 | #endif |
| 1053 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 1054 | } |
| 1055 | |
| 1056 | bool referencesArgs(Expr *E) { |
| 1057 | Result = false; |
| 1058 | TraverseStmt(E); |
| 1059 | return Result; |
| 1060 | } |
| 1061 | |
| 1062 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 1063 | (0) . __assert_fail ("E->getType()->getPointeeCXXRecordDecl() == ClassType && \"`this` doesn't refer to the enclosing class?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 1064, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 1064 | (0) . __assert_fail ("E->getType()->getPointeeCXXRecordDecl() == ClassType && \"`this` doesn't refer to the enclosing class?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 1064, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "`this` doesn't refer to the enclosing class?"); |
| 1065 | Result = true; |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 1070 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 1071 | if (Parms.count(PVD)) { |
| 1072 | Result = true; |
| 1073 | return false; |
| 1074 | } |
| 1075 | return true; |
| 1076 | } |
| 1077 | }; |
| 1078 | } |
| 1079 | |
| 1080 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1081 | S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if); |
| 1082 | |
| 1083 | Expr *Cond; |
| 1084 | StringRef Msg; |
| 1085 | if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
| 1086 | return; |
| 1087 | |
| 1088 | StringRef DiagTypeStr; |
| 1089 | if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr)) |
| 1090 | return; |
| 1091 | |
| 1092 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 1093 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
| 1094 | S.Diag(AL.getArgAsExpr(2)->getBeginLoc(), |
| 1095 | diag::err_diagnose_if_invalid_diagnostic_type); |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | bool ArgDependent = false; |
| 1100 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1101 | ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
| 1102 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
| 1103 | AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, |
| 1104 | cast<NamedDecl>(D), AL.getAttributeSpellingListIndex())); |
| 1105 | } |
| 1106 | |
| 1107 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1108 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 1109 | S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL; |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | Expr *E = AL.getArgAsExpr(0); |
| 1114 | uint32_t Type; |
| 1115 | if (!checkUInt32Argument(S, AL, E, Type, )) |
| 1116 | return; |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | if (Type > 3) { |
| 1122 | S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range) |
| 1123 | << AL << 0 << 3 << E->getSourceRange(); |
| 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 1132 | S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1; |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | D->addAttr(::new (S.Context) PassObjectSizeAttr( |
| 1137 | AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex())); |
| 1138 | } |
| 1139 | |
| 1140 | static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1141 | ConsumableAttr::ConsumedState DefaultState; |
| 1142 | |
| 1143 | if (AL.isArgIdent(0)) { |
| 1144 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
| 1145 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1146 | DefaultState)) { |
| 1147 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1148 | << IL->Ident; |
| 1149 | return; |
| 1150 | } |
| 1151 | } else { |
| 1152 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1153 | << AL << AANT_ArgumentIdentifier; |
| 1154 | return; |
| 1155 | } |
| 1156 | |
| 1157 | D->addAttr(::new (S.Context) |
| 1158 | ConsumableAttr(AL.getRange(), S.Context, DefaultState, |
| 1159 | AL.getAttributeSpellingListIndex())); |
| 1160 | } |
| 1161 | |
| 1162 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1163 | const ParsedAttr &AL) { |
| 1164 | QualType ThisType = MD->getThisType()->getPointeeType(); |
| 1165 | |
| 1166 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1167 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1168 | S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1169 | RD->getNameAsString(); |
| 1170 | |
| 1171 | return false; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1179 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 1180 | return; |
| 1181 | |
| 1182 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
| 1183 | return; |
| 1184 | |
| 1185 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1186 | for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) { |
| 1187 | CallableWhenAttr::ConsumedState CallableState; |
| 1188 | |
| 1189 | StringRef StateString; |
| 1190 | SourceLocation Loc; |
| 1191 | if (AL.isArgIdent(ArgIndex)) { |
| 1192 | IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex); |
| 1193 | StateString = Ident->Ident->getName(); |
| 1194 | Loc = Ident->Loc; |
| 1195 | } else { |
| 1196 | if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc)) |
| 1197 | return; |
| 1198 | } |
| 1199 | |
| 1200 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
| 1201 | CallableState)) { |
| 1202 | S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString; |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | States.push_back(CallableState); |
| 1207 | } |
| 1208 | |
| 1209 | D->addAttr(::new (S.Context) |
| 1210 | CallableWhenAttr(AL.getRange(), S.Context, States.data(), |
| 1211 | States.size(), AL.getAttributeSpellingListIndex())); |
| 1212 | } |
| 1213 | |
| 1214 | static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1215 | ParamTypestateAttr::ConsumedState ParamState; |
| 1216 | |
| 1217 | if (AL.isArgIdent(0)) { |
| 1218 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
| 1219 | StringRef StateString = Ident->Ident->getName(); |
| 1220 | |
| 1221 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1222 | ParamState)) { |
| 1223 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1224 | << AL << StateString; |
| 1225 | return; |
| 1226 | } |
| 1227 | } else { |
| 1228 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1229 | << AL << AANT_ArgumentIdentifier; |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | D->addAttr(::new (S.Context) |
| 1246 | ParamTypestateAttr(AL.getRange(), S.Context, ParamState, |
| 1247 | AL.getAttributeSpellingListIndex())); |
| 1248 | } |
| 1249 | |
| 1250 | static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1251 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1252 | |
| 1253 | if (AL.isArgIdent(0)) { |
| 1254 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
| 1255 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1256 | ReturnState)) { |
| 1257 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1258 | << IL->Ident; |
| 1259 | return; |
| 1260 | } |
| 1261 | } else { |
| 1262 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1263 | << AL << AANT_ArgumentIdentifier; |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | D->addAttr(::new (S.Context) |
| 1293 | ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState, |
| 1294 | AL.getAttributeSpellingListIndex())); |
| 1295 | } |
| 1296 | |
| 1297 | static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1298 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
| 1299 | return; |
| 1300 | |
| 1301 | SetTypestateAttr::ConsumedState NewState; |
| 1302 | if (AL.isArgIdent(0)) { |
| 1303 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
| 1304 | StringRef Param = Ident->Ident->getName(); |
| 1305 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1306 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1307 | << Param; |
| 1308 | return; |
| 1309 | } |
| 1310 | } else { |
| 1311 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1312 | << AL << AANT_ArgumentIdentifier; |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | D->addAttr(::new (S.Context) |
| 1317 | SetTypestateAttr(AL.getRange(), S.Context, NewState, |
| 1318 | AL.getAttributeSpellingListIndex())); |
| 1319 | } |
| 1320 | |
| 1321 | static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1322 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
| 1323 | return; |
| 1324 | |
| 1325 | TestTypestateAttr::ConsumedState TestState; |
| 1326 | if (AL.isArgIdent(0)) { |
| 1327 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
| 1328 | StringRef Param = Ident->Ident->getName(); |
| 1329 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
| 1330 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1331 | << Param; |
| 1332 | return; |
| 1333 | } |
| 1334 | } else { |
| 1335 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1336 | << AL << AANT_ArgumentIdentifier; |
| 1337 | return; |
| 1338 | } |
| 1339 | |
| 1340 | D->addAttr(::new (S.Context) |
| 1341 | TestTypestateAttr(AL.getRange(), S.Context, TestState, |
| 1342 | AL.getAttributeSpellingListIndex())); |
| 1343 | } |
| 1344 | |
| 1345 | static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1346 | |
| 1347 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
| 1348 | } |
| 1349 | |
| 1350 | static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1351 | if (auto *TD = dyn_cast<TagDecl>(D)) |
| 1352 | TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context, |
| 1353 | AL.getAttributeSpellingListIndex())); |
| 1354 | else if (auto *FD = dyn_cast<FieldDecl>(D)) { |
| 1355 | bool BitfieldByteAligned = (!FD->getType()->isDependentType() && |
| 1356 | !FD->getType()->isIncompleteType() && |
| 1357 | FD->isBitField() && |
| 1358 | S.Context.getTypeAlign(FD->getType()) <= 8); |
| 1359 | |
| 1360 | if (S.getASTContext().getTargetInfo().getTriple().isPS4()) { |
| 1361 | if (BitfieldByteAligned) |
| 1362 | |
| 1363 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
| 1364 | << AL << FD->getType(); |
| 1365 | else |
| 1366 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1367 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 1368 | } else { |
| 1369 | |
| 1370 | if (BitfieldByteAligned) |
| 1371 | S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1372 | |
| 1373 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1374 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 1375 | } |
| 1376 | |
| 1377 | } else |
| 1378 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
| 1379 | } |
| 1380 | |
| 1381 | static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1386 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| 1387 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
| 1388 | << AL << VD->getType() << 0; |
| 1389 | return false; |
| 1390 | } |
| 1391 | } |
| 1392 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1393 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| 1394 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
| 1395 | << AL << PD->getType() << 1; |
| 1396 | return false; |
| 1397 | } |
| 1398 | } |
| 1399 | else { |
| 1400 | S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL; |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
| 1404 | return true; |
| 1405 | } |
| 1406 | |
| 1407 | static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1408 | if (!checkIBOutletCommon(S, D, AL)) |
| 1409 | return; |
| 1410 | |
| 1411 | D->addAttr(::new (S.Context) |
| 1412 | IBOutletAttr(AL.getRange(), S.Context, |
| 1413 | AL.getAttributeSpellingListIndex())); |
| 1414 | } |
| 1415 | |
| 1416 | static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1417 | |
| 1418 | |
| 1419 | if (AL.getNumArgs() > 1) { |
| 1420 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | if (!checkIBOutletCommon(S, D, AL)) |
| 1425 | return; |
| 1426 | |
| 1427 | ParsedType PT; |
| 1428 | |
| 1429 | if (AL.hasParsedType()) |
| 1430 | PT = AL.getTypeArg(); |
| 1431 | else { |
| 1432 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(), |
| 1433 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1434 | if (!PT) { |
| 1435 | S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1436 | return; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | TypeSourceInfo *QTLoc = nullptr; |
| 1441 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1442 | if (!QTLoc) |
| 1443 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc()); |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
| 1450 | S.Diag(AL.getLoc(), |
| 1451 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1452 | : diag::err_iboutletcollection_type) << QT; |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | D->addAttr(::new (S.Context) |
| 1457 | IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc, |
| 1458 | AL.getAttributeSpellingListIndex())); |
| 1459 | } |
| 1460 | |
| 1461 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1462 | if (RefOkay) { |
| 1463 | if (T->isReferenceType()) |
| 1464 | return true; |
| 1465 | } else { |
| 1466 | T = T.getNonReferenceType(); |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | if (const RecordType *UT = T->getAsUnionType()) { |
| 1472 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1473 | RecordDecl *UD = UT->getDecl(); |
| 1474 | for (const auto *I : UD->fields()) { |
| 1475 | QualType QT = I->getType(); |
| 1476 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1477 | return true; |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | return T->isAnyPointerType() || T->isBlockPointerType(); |
| 1483 | } |
| 1484 | |
| 1485 | static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, |
| 1486 | SourceRange AttrParmRange, |
| 1487 | SourceRange TypeRange, |
| 1488 | bool isReturnValue = false) { |
| 1489 | if (!S.isValidPointerAttrType(T)) { |
| 1490 | if (isReturnValue) |
| 1491 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1492 | << AL << AttrParmRange << TypeRange; |
| 1493 | else |
| 1494 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
| 1495 | << AL << AttrParmRange << TypeRange << 0; |
| 1496 | return false; |
| 1497 | } |
| 1498 | return true; |
| 1499 | } |
| 1500 | |
| 1501 | static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1502 | SmallVector<ParamIdx, 8> NonNullArgs; |
| 1503 | for (unsigned I = 0; I < AL.getNumArgs(); ++I) { |
| 1504 | Expr *Ex = AL.getArgAsExpr(I); |
| 1505 | ParamIdx Idx; |
| 1506 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx)) |
| 1507 | return; |
| 1508 | |
| 1509 | |
| 1510 | if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) && |
| 1511 | !attrNonNullArgCheck( |
| 1512 | S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL, |
| 1513 | Ex->getSourceRange(), |
| 1514 | getFunctionOrMethodParamRange(D, Idx.getASTIndex()))) |
| 1515 | continue; |
| 1516 | |
| 1517 | NonNullArgs.push_back(Idx); |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | if (NonNullArgs.empty() && AL.getLoc().isFileID() && |
| 1525 | !S.inTemplateInstantiation()) { |
| 1526 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1527 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1528 | I != E && !AnyPointers; ++I) { |
| 1529 | QualType T = getFunctionOrMethodParamType(D, I); |
| 1530 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
| 1531 | AnyPointers = true; |
| 1532 | } |
| 1533 | |
| 1534 | if (!AnyPointers) |
| 1535 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| 1536 | } |
| 1537 | |
| 1538 | ParamIdx *Start = NonNullArgs.data(); |
| 1539 | unsigned Size = NonNullArgs.size(); |
| 1540 | llvm::array_pod_sort(Start, Start + Size); |
| 1541 | D->addAttr(::new (S.Context) |
| 1542 | NonNullAttr(AL.getRange(), S.Context, Start, Size, |
| 1543 | AL.getAttributeSpellingListIndex())); |
| 1544 | } |
| 1545 | |
| 1546 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1547 | const ParsedAttr &AL) { |
| 1548 | if (AL.getNumArgs() > 0) { |
| 1549 | if (D->getFunctionType()) { |
| 1550 | handleNonNullAttr(S, D, AL); |
| 1551 | } else { |
| 1552 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1553 | << D->getSourceRange(); |
| 1554 | } |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(), |
| 1560 | D->getSourceRange())) |
| 1561 | return; |
| 1562 | |
| 1563 | D->addAttr(::new (S.Context) |
| 1564 | NonNullAttr(AL.getRange(), S.Context, nullptr, 0, |
| 1565 | AL.getAttributeSpellingListIndex())); |
| 1566 | } |
| 1567 | |
| 1568 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1569 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1570 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1571 | if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR, |
| 1572 | true)) |
| 1573 | return; |
| 1574 | |
| 1575 | D->addAttr(::new (S.Context) |
| 1576 | ReturnsNonNullAttr(AL.getRange(), S.Context, |
| 1577 | AL.getAttributeSpellingListIndex())); |
| 1578 | } |
| 1579 | |
| 1580 | static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1581 | if (D->isInvalidDecl()) |
| 1582 | return; |
| 1583 | |
| 1584 | |
| 1585 | QualType T = cast<ParmVarDecl>(D)->getType(); |
| 1586 | if (!S.isValidPointerAttrType(T, true)) { |
| 1587 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
| 1588 | << AL << AL.getRange() << 0; |
| 1589 | return; |
| 1590 | } |
| 1591 | |
| 1592 | D->addAttr(::new (S.Context) NoEscapeAttr( |
| 1593 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 1594 | } |
| 1595 | |
| 1596 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1597 | Expr *E = AL.getArgAsExpr(0), |
| 1598 | *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr; |
| 1599 | S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE, |
| 1600 | AL.getAttributeSpellingListIndex()); |
| 1601 | } |
| 1602 | |
| 1603 | static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1604 | S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 1605 | AL.getAttributeSpellingListIndex()); |
| 1606 | } |
| 1607 | |
| 1608 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1609 | Expr *OE, unsigned SpellingListIndex) { |
| 1610 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1611 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1612 | |
| 1613 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1614 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1615 | |
| 1616 | if (!isValidPointerAttrType(ResultType, true)) { |
| 1617 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1618 | << &TmpAttr << AttrRange << SR; |
| 1619 | return; |
| 1620 | } |
| 1621 | |
| 1622 | if (!E->isValueDependent()) { |
| 1623 | llvm::APSInt I(64); |
| 1624 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1625 | if (OE) |
| 1626 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1627 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1628 | << E->getSourceRange(); |
| 1629 | else |
| 1630 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1631 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1632 | << E->getSourceRange(); |
| 1633 | return; |
| 1634 | } |
| 1635 | |
| 1636 | if (!I.isPowerOf2()) { |
| 1637 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1638 | << E->getSourceRange(); |
| 1639 | return; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (OE) { |
| 1644 | if (!OE->isValueDependent()) { |
| 1645 | llvm::APSInt I(64); |
| 1646 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1647 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1648 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1649 | << OE->getSourceRange(); |
| 1650 | return; |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | D->addAttr(::new (Context) |
| 1656 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1657 | } |
| 1658 | |
| 1659 | void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr, |
| 1660 | unsigned SpellingListIndex) { |
| 1661 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1662 | |
| 1663 | AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex); |
| 1664 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1665 | |
| 1666 | if (!ResultType->isDependentType() && |
| 1667 | !isValidPointerAttrType(ResultType, true)) { |
| 1668 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1669 | << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D); |
| 1670 | return; |
| 1671 | } |
| 1672 | |
| 1673 | ParamIdx Idx; |
| 1674 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1675 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
| 1676 | , ParamExpr, Idx)) |
| 1677 | return; |
| 1678 | |
| 1679 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
| 1680 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) { |
| 1681 | Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only) |
| 1682 | << &TmpAttr |
| 1683 | << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange(); |
| 1684 | return; |
| 1685 | } |
| 1686 | |
| 1687 | D->addAttr(::new (Context) |
| 1688 | AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex)); |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | |
| 1693 | static bool normalizeName(StringRef &AttrName) { |
| 1694 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1695 | AttrName.endswith("__")) { |
| 1696 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1697 | return true; |
| 1698 | } |
| 1699 | return false; |
| 1700 | } |
| 1701 | |
| 1702 | static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | if (!AL.isArgIdent(0)) { |
| 1712 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 1713 | << AL << 1 << AANT_ArgumentIdentifier; |
| 1714 | return; |
| 1715 | } |
| 1716 | |
| 1717 | |
| 1718 | OwnershipAttr::OwnershipKind K = |
| 1719 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
| 1720 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
| 1721 | |
| 1722 | |
| 1723 | switch (K) { |
| 1724 | case OwnershipAttr::Takes: |
| 1725 | case OwnershipAttr::Holds: |
| 1726 | if (AL.getNumArgs() < 2) { |
| 1727 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2; |
| 1728 | return; |
| 1729 | } |
| 1730 | break; |
| 1731 | case OwnershipAttr::Returns: |
| 1732 | if (AL.getNumArgs() > 2) { |
| 1733 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
| 1734 | return; |
| 1735 | } |
| 1736 | break; |
| 1737 | } |
| 1738 | |
| 1739 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
| 1740 | |
| 1741 | StringRef ModuleName = Module->getName(); |
| 1742 | if (normalizeName(ModuleName)) { |
| 1743 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1744 | } |
| 1745 | |
| 1746 | SmallVector<ParamIdx, 8> OwnershipArgs; |
| 1747 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1748 | Expr *Ex = AL.getArgAsExpr(i); |
| 1749 | ParamIdx Idx; |
| 1750 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
| 1751 | return; |
| 1752 | |
| 1753 | |
| 1754 | QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
| 1755 | int Err = -1; |
| 1756 | switch (K) { |
| 1757 | case OwnershipAttr::Takes: |
| 1758 | case OwnershipAttr::Holds: |
| 1759 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1760 | Err = 0; |
| 1761 | break; |
| 1762 | case OwnershipAttr::Returns: |
| 1763 | if (!T->isIntegerType()) |
| 1764 | Err = 1; |
| 1765 | break; |
| 1766 | } |
| 1767 | if (-1 != Err) { |
| 1768 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err |
| 1769 | << Ex->getSourceRange(); |
| 1770 | return; |
| 1771 | } |
| 1772 | |
| 1773 | |
| 1774 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
| 1775 | |
| 1776 | |
| 1777 | if (I->getOwnKind() != K && I->args_end() != |
| 1778 | std::find(I->args_begin(), I->args_end(), Idx)) { |
| 1779 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I; |
| 1780 | return; |
| 1781 | } else if (K == OwnershipAttr::Returns && |
| 1782 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1783 | |
| 1784 | |
| 1785 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1786 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1787 | << I->args_begin()->getSourceIndex(); |
| 1788 | if (I->args_size()) |
| 1789 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1790 | << Idx.getSourceIndex() << Ex->getSourceRange(); |
| 1791 | return; |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | OwnershipArgs.push_back(Idx); |
| 1796 | } |
| 1797 | |
| 1798 | ParamIdx *Start = OwnershipArgs.data(); |
| 1799 | unsigned Size = OwnershipArgs.size(); |
| 1800 | llvm::array_pod_sort(Start, Start + Size); |
| 1801 | D->addAttr(::new (S.Context) |
| 1802 | OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size, |
| 1803 | AL.getAttributeSpellingListIndex())); |
| 1804 | } |
| 1805 | |
| 1806 | static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1807 | |
| 1808 | if (AL.getNumArgs() > 1) { |
| 1809 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
| 1810 | return; |
| 1811 | } |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | |
| 1823 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
| 1824 | if (!Ctx->isFileContext()) { |
| 1825 | S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1826 | << cast<NamedDecl>(D); |
| 1827 | return; |
| 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | |
| 1842 | |
| 1843 | |
| 1844 | |
| 1845 | |
| 1846 | |
| 1847 | |
| 1848 | |
| 1849 | |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | StringRef Str; |
| 1856 | if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
| 1857 | |
| 1858 | |
| 1859 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1860 | AL.getAttributeSpellingListIndex())); |
| 1861 | |
| 1862 | D->addAttr(::new (S.Context) |
| 1863 | WeakRefAttr(AL.getRange(), S.Context, |
| 1864 | AL.getAttributeSpellingListIndex())); |
| 1865 | } |
| 1866 | |
| 1867 | static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1868 | StringRef Str; |
| 1869 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
| 1870 | return; |
| 1871 | |
| 1872 | |
| 1873 | const auto *FD = cast<FunctionDecl>(D); |
| 1874 | if (FD->isThisDeclarationADefinition()) { |
| 1875 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1; |
| 1876 | return; |
| 1877 | } |
| 1878 | |
| 1879 | D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str, |
| 1880 | AL.getAttributeSpellingListIndex())); |
| 1881 | } |
| 1882 | |
| 1883 | static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1884 | StringRef Str; |
| 1885 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
| 1886 | return; |
| 1887 | |
| 1888 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
| 1889 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1890 | return; |
| 1891 | } |
| 1892 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
| 1893 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx); |
| 1894 | } |
| 1895 | |
| 1896 | |
| 1897 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1898 | if (FD->isThisDeclarationADefinition()) { |
| 1899 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0; |
| 1900 | return; |
| 1901 | } |
| 1902 | } else { |
| 1903 | const auto *VD = cast<VarDecl>(D); |
| 1904 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
| 1905 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0; |
| 1906 | return; |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | |
| 1911 | if (!S.LangOpts.CPlusPlus) { |
| 1912 | |
| 1913 | |
| 1914 | const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc()); |
| 1915 | LookupResult LR(S, target, Sema::LookupOrdinaryName); |
| 1916 | if (S.LookupQualifiedName(LR, S.getCurLexicalContext())) |
| 1917 | for (NamedDecl *ND : LR) |
| 1918 | ND->markUsed(S.Context); |
| 1919 | } |
| 1920 | |
| 1921 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1922 | AL.getAttributeSpellingListIndex())); |
| 1923 | } |
| 1924 | |
| 1925 | static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1926 | StringRef Model; |
| 1927 | SourceLocation LiteralLoc; |
| 1928 | |
| 1929 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc)) |
| 1930 | return; |
| 1931 | |
| 1932 | |
| 1933 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1934 | && Model != "initial-exec" && Model != "local-exec") { |
| 1935 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
| 1936 | return; |
| 1937 | } |
| 1938 | |
| 1939 | D->addAttr(::new (S.Context) |
| 1940 | TLSModelAttr(AL.getRange(), S.Context, Model, |
| 1941 | AL.getAttributeSpellingListIndex())); |
| 1942 | } |
| 1943 | |
| 1944 | static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1945 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1946 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1947 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1948 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 1949 | return; |
| 1950 | } |
| 1951 | |
| 1952 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1953 | << AL << getFunctionOrMethodResultSourceRange(D); |
| 1954 | } |
| 1955 | |
| 1956 | static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1957 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 1958 | |
| 1959 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 1960 | if (MD->getParent()->isLambda()) { |
| 1961 | S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL; |
| 1962 | return; |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 1967 | return; |
| 1968 | |
| 1969 | SmallVector<IdentifierInfo *, 8> CPUs; |
| 1970 | for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) { |
| 1971 | if (!AL.isArgIdent(ArgNo)) { |
| 1972 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1973 | << AL << AANT_ArgumentIdentifier; |
| 1974 | return; |
| 1975 | } |
| 1976 | |
| 1977 | IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo); |
| 1978 | StringRef CPUName = CPUArg->Ident->getName().trim(); |
| 1979 | |
| 1980 | if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) { |
| 1981 | S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value) |
| 1982 | << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch); |
| 1983 | return; |
| 1984 | } |
| 1985 | |
| 1986 | const TargetInfo &Target = S.Context.getTargetInfo(); |
| 1987 | if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) { |
| 1988 | return Target.CPUSpecificManglingCharacter(CPUName) == |
| 1989 | Target.CPUSpecificManglingCharacter(Cur->getName()); |
| 1990 | })) { |
| 1991 | S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries); |
| 1992 | return; |
| 1993 | } |
| 1994 | CPUs.push_back(CPUArg->Ident); |
| 1995 | } |
| 1996 | |
| 1997 | FD->setIsMultiVersion(true); |
| 1998 | if (AL.getKind() == ParsedAttr::AT_CPUSpecific) |
| 1999 | D->addAttr(::new (S.Context) CPUSpecificAttr( |
| 2000 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 2001 | AL.getAttributeSpellingListIndex())); |
| 2002 | else |
| 2003 | D->addAttr(::new (S.Context) CPUDispatchAttr( |
| 2004 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 2005 | AL.getAttributeSpellingListIndex())); |
| 2006 | } |
| 2007 | |
| 2008 | static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2009 | if (S.LangOpts.CPlusPlus) { |
| 2010 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 2011 | << AL << AttributeLangSupport::Cpp; |
| 2012 | return; |
| 2013 | } |
| 2014 | |
| 2015 | if (CommonAttr *CA = S.mergeCommonAttr(D, AL)) |
| 2016 | D->addAttr(CA); |
| 2017 | } |
| 2018 | |
| 2019 | static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2020 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL)) |
| 2021 | return; |
| 2022 | |
| 2023 | if (AL.isDeclspecAttribute()) { |
| 2024 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 2025 | const auto &Arch = Triple.getArch(); |
| 2026 | if (Arch != llvm::Triple::x86 && |
| 2027 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
| 2028 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch) |
| 2029 | << AL << Triple.getArchName(); |
| 2030 | return; |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context, |
| 2035 | AL.getAttributeSpellingListIndex())); |
| 2036 | } |
| 2037 | |
| 2038 | static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
| 2039 | if (hasDeclarator(D)) return; |
| 2040 | |
| 2041 | if (!isa<ObjCMethodDecl>(D)) { |
| 2042 | S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2043 | << Attrs << ExpectedFunctionOrMethod; |
| 2044 | return; |
| 2045 | } |
| 2046 | |
| 2047 | D->addAttr(::new (S.Context) NoReturnAttr( |
| 2048 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
| 2049 | } |
| 2050 | |
| 2051 | static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
| 2052 | if (!S.getLangOpts().CFProtectionBranch) |
| 2053 | S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored); |
| 2054 | else |
| 2055 | handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs); |
| 2056 | } |
| 2057 | |
| 2058 | bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) { |
| 2059 | if (!checkAttributeNumArgs(*this, Attrs, 0)) { |
| 2060 | Attrs.setInvalid(); |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
| 2064 | return false; |
| 2065 | } |
| 2066 | |
| 2067 | bool Sema::CheckAttrTarget(const ParsedAttr &AL) { |
| 2068 | |
| 2069 | if (!AL.existsInTarget(Context.getTargetInfo())) { |
| 2070 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL; |
| 2071 | AL.setInvalid(); |
| 2072 | return true; |
| 2073 | } |
| 2074 | |
| 2075 | return false; |
| 2076 | } |
| 2077 | |
| 2078 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2079 | |
| 2080 | |
| 2081 | |
| 2082 | if (!isFunctionOrMethodOrBlock(D)) { |
| 2083 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2084 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 2085 | !VD->getType()->isFunctionPointerType())) { |
| 2086 | S.Diag(AL.getLoc(), AL.isCXX11Attribute() |
| 2087 | ? diag::err_attribute_wrong_decl_type |
| 2088 | : diag::warn_attribute_wrong_decl_type) |
| 2089 | << AL << ExpectedFunctionMethodOrBlock; |
| 2090 | return; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | D->addAttr(::new (S.Context) |
| 2095 | AnalyzerNoReturnAttr(AL.getRange(), S.Context, |
| 2096 | AL.getAttributeSpellingListIndex())); |
| 2097 | } |
| 2098 | |
| 2099 | |
| 2100 | static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2101 | |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | |
| 2110 | |
| 2111 | |
| 2112 | |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | |
| 2121 | |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 2126 | S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A; |
| 2127 | return; |
| 2128 | } |
| 2129 | |
| 2130 | const auto *R = cast<RecordDecl>(D); |
| 2131 | int count = 0; |
| 2132 | |
| 2133 | if (!isa<CXXRecordDecl>(R)) { |
| 2134 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 2135 | return; |
| 2136 | } |
| 2137 | |
| 2138 | if (!cast<CXXRecordDecl>(R)->isPOD()) { |
| 2139 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 2140 | return; |
| 2141 | } |
| 2142 | |
| 2143 | for (const auto *I : R->fields()) { |
| 2144 | if ((count == 1) || !I->getType()->isVectorType()) { |
| 2145 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 2146 | return; |
| 2147 | } |
| 2148 | count++; |
| 2149 | } |
| 2150 | |
| 2151 | D->addAttr(::new (S.Context) VecReturnAttr( |
| 2152 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 2153 | } |
| 2154 | |
| 2155 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 2156 | const ParsedAttr &AL) { |
| 2157 | if (isa<ParmVarDecl>(D)) { |
| 2158 | |
| 2159 | |
| 2160 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 2161 | S.Diag(AL.getLoc(), |
| 2162 | diag::err_carries_dependency_param_not_function_decl); |
| 2163 | return; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 2168 | AL.getRange(), S.Context, |
| 2169 | AL.getAttributeSpellingListIndex())); |
| 2170 | } |
| 2171 | |
| 2172 | static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2173 | bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName(); |
| 2174 | |
| 2175 | |
| 2176 | |
| 2177 | if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr) |
| 2178 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
| 2179 | |
| 2180 | D->addAttr(::new (S.Context) UnusedAttr( |
| 2181 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 2182 | } |
| 2183 | |
| 2184 | static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2185 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 2186 | if (AL.getNumArgs() && |
| 2187 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
| 2188 | return; |
| 2189 | |
| 2190 | D->addAttr(::new (S.Context) |
| 2191 | ConstructorAttr(AL.getRange(), S.Context, priority, |
| 2192 | AL.getAttributeSpellingListIndex())); |
| 2193 | } |
| 2194 | |
| 2195 | static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2196 | uint32_t priority = DestructorAttr::DefaultPriority; |
| 2197 | if (AL.getNumArgs() && |
| 2198 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
| 2199 | return; |
| 2200 | |
| 2201 | D->addAttr(::new (S.Context) |
| 2202 | DestructorAttr(AL.getRange(), S.Context, priority, |
| 2203 | AL.getAttributeSpellingListIndex())); |
| 2204 | } |
| 2205 | |
| 2206 | template <typename AttrTy> |
| 2207 | static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2208 | |
| 2209 | StringRef Str; |
| 2210 | if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
| 2211 | return; |
| 2212 | |
| 2213 | D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str, |
| 2214 | AL.getAttributeSpellingListIndex())); |
| 2215 | } |
| 2216 | |
| 2217 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 2218 | const ParsedAttr &AL) { |
| 2219 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
| 2220 | S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 2221 | << AL << AL.getRange(); |
| 2222 | return; |
| 2223 | } |
| 2224 | |
| 2225 | D->addAttr(::new (S.Context) |
| 2226 | ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context, |
| 2227 | AL.getAttributeSpellingListIndex())); |
| 2228 | } |
| 2229 | |
| 2230 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2231 | IdentifierInfo *Platform, |
| 2232 | VersionTuple Introduced, |
| 2233 | VersionTuple Deprecated, |
| 2234 | VersionTuple Obsoleted) { |
| 2235 | StringRef PlatformName |
| 2236 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2237 | if (PlatformName.empty()) |
| 2238 | PlatformName = Platform->getName(); |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2243 | !(Introduced <= Deprecated)) { |
| 2244 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2245 | << 1 << PlatformName << Deprecated.getAsString() |
| 2246 | << 0 << Introduced.getAsString(); |
| 2247 | return true; |
| 2248 | } |
| 2249 | |
| 2250 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2251 | !(Introduced <= Obsoleted)) { |
| 2252 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2253 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2254 | << 0 << Introduced.getAsString(); |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
| 2258 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2259 | !(Deprecated <= Obsoleted)) { |
| 2260 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2261 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2262 | << 1 << Deprecated.getAsString(); |
| 2263 | return true; |
| 2264 | } |
| 2265 | |
| 2266 | return false; |
| 2267 | } |
| 2268 | |
| 2269 | |
| 2270 | |
| 2271 | |
| 2272 | |
| 2273 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2274 | bool BeforeIsOkay) { |
| 2275 | if (X.empty() || Y.empty()) |
| 2276 | return true; |
| 2277 | |
| 2278 | if (X == Y) |
| 2279 | return true; |
| 2280 | |
| 2281 | if (BeforeIsOkay && X < Y) |
| 2282 | return true; |
| 2283 | |
| 2284 | return false; |
| 2285 | } |
| 2286 | |
| 2287 | AvailabilityAttr *Sema::mergeAvailabilityAttr( |
| 2288 | NamedDecl *D, SourceRange Range, IdentifierInfo *Platform, bool Implicit, |
| 2289 | VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted, |
| 2290 | bool IsUnavailable, StringRef Message, bool IsStrict, StringRef Replacement, |
| 2291 | AvailabilityMergeKind AMK, int Priority, unsigned AttrSpellingListIndex) { |
| 2292 | VersionTuple MergedIntroduced = Introduced; |
| 2293 | VersionTuple MergedDeprecated = Deprecated; |
| 2294 | VersionTuple MergedObsoleted = Obsoleted; |
| 2295 | bool FoundAny = false; |
| 2296 | bool OverrideOrImpl = false; |
| 2297 | switch (AMK) { |
| 2298 | case AMK_None: |
| 2299 | case AMK_Redeclaration: |
| 2300 | OverrideOrImpl = false; |
| 2301 | break; |
| 2302 | |
| 2303 | case AMK_Override: |
| 2304 | case AMK_ProtocolImplementation: |
| 2305 | OverrideOrImpl = true; |
| 2306 | break; |
| 2307 | } |
| 2308 | |
| 2309 | if (D->hasAttrs()) { |
| 2310 | AttrVec &Attrs = D->getAttrs(); |
| 2311 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2312 | const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2313 | if (!OldAA) { |
| 2314 | ++i; |
| 2315 | continue; |
| 2316 | } |
| 2317 | |
| 2318 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2319 | if (OldPlatform != Platform) { |
| 2320 | ++i; |
| 2321 | continue; |
| 2322 | } |
| 2323 | |
| 2324 | |
| 2325 | |
| 2326 | |
| 2327 | if (OldAA->getPriority() < Priority) |
| 2328 | return nullptr; |
| 2329 | |
| 2330 | |
| 2331 | |
| 2332 | |
| 2333 | if (OldAA->getPriority() > Priority) { |
| 2334 | Attrs.erase(Attrs.begin() + i); |
| 2335 | --e; |
| 2336 | continue; |
| 2337 | } |
| 2338 | |
| 2339 | FoundAny = true; |
| 2340 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2341 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2342 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2343 | bool OldIsUnavailable = OldAA->getUnavailable(); |
| 2344 | |
| 2345 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2346 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2347 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
| 2348 | !(OldIsUnavailable == IsUnavailable || |
| 2349 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2350 | if (OverrideOrImpl) { |
| 2351 | int Which = -1; |
| 2352 | VersionTuple FirstVersion; |
| 2353 | VersionTuple SecondVersion; |
| 2354 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
| 2355 | Which = 0; |
| 2356 | FirstVersion = OldIntroduced; |
| 2357 | SecondVersion = Introduced; |
| 2358 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
| 2359 | Which = 1; |
| 2360 | FirstVersion = Deprecated; |
| 2361 | SecondVersion = OldDeprecated; |
| 2362 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
| 2363 | Which = 2; |
| 2364 | FirstVersion = Obsoleted; |
| 2365 | SecondVersion = OldObsoleted; |
| 2366 | } |
| 2367 | |
| 2368 | if (Which == -1) { |
| 2369 | Diag(OldAA->getLocation(), |
| 2370 | diag::warn_mismatched_availability_override_unavail) |
| 2371 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2372 | << (AMK == AMK_Override); |
| 2373 | } else { |
| 2374 | Diag(OldAA->getLocation(), |
| 2375 | diag::warn_mismatched_availability_override) |
| 2376 | << Which |
| 2377 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2378 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2379 | << (AMK == AMK_Override); |
| 2380 | } |
| 2381 | if (AMK == AMK_Override) |
| 2382 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2383 | else |
| 2384 | Diag(Range.getBegin(), diag::note_protocol_method); |
| 2385 | } else { |
| 2386 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2387 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2388 | } |
| 2389 | |
| 2390 | Attrs.erase(Attrs.begin() + i); |
| 2391 | --e; |
| 2392 | continue; |
| 2393 | } |
| 2394 | |
| 2395 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2396 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2397 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2398 | |
| 2399 | if (MergedIntroduced2.empty()) |
| 2400 | MergedIntroduced2 = OldIntroduced; |
| 2401 | if (MergedDeprecated2.empty()) |
| 2402 | MergedDeprecated2 = OldDeprecated; |
| 2403 | if (MergedObsoleted2.empty()) |
| 2404 | MergedObsoleted2 = OldObsoleted; |
| 2405 | |
| 2406 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2407 | MergedIntroduced2, MergedDeprecated2, |
| 2408 | MergedObsoleted2)) { |
| 2409 | Attrs.erase(Attrs.begin() + i); |
| 2410 | --e; |
| 2411 | continue; |
| 2412 | } |
| 2413 | |
| 2414 | MergedIntroduced = MergedIntroduced2; |
| 2415 | MergedDeprecated = MergedDeprecated2; |
| 2416 | MergedObsoleted = MergedObsoleted2; |
| 2417 | ++i; |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | if (FoundAny && |
| 2422 | MergedIntroduced == Introduced && |
| 2423 | MergedDeprecated == Deprecated && |
| 2424 | MergedObsoleted == Obsoleted) |
| 2425 | return nullptr; |
| 2426 | |
| 2427 | |
| 2428 | |
| 2429 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
| 2430 | MergedDeprecated, MergedObsoleted) && |
| 2431 | !OverrideOrImpl) { |
| 2432 | auto *Avail = ::new (Context) |
| 2433 | AvailabilityAttr(Range, Context, Platform, Introduced, Deprecated, |
| 2434 | Obsoleted, IsUnavailable, Message, IsStrict, |
| 2435 | Replacement, Priority, AttrSpellingListIndex); |
| 2436 | Avail->setImplicit(Implicit); |
| 2437 | return Avail; |
| 2438 | } |
| 2439 | return nullptr; |
| 2440 | } |
| 2441 | |
| 2442 | static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2443 | if (!checkAttributeNumArgs(S, AL, 1)) |
| 2444 | return; |
| 2445 | IdentifierLoc *Platform = AL.getArgAsIdent(0); |
| 2446 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2447 | |
| 2448 | IdentifierInfo *II = Platform->Ident; |
| 2449 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2450 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2451 | << Platform->Ident; |
| 2452 | |
| 2453 | auto *ND = dyn_cast<NamedDecl>(D); |
| 2454 | if (!ND) |
| 2455 | return; |
| 2456 | |
| 2457 | AvailabilityChange Introduced = AL.getAvailabilityIntroduced(); |
| 2458 | AvailabilityChange Deprecated = AL.getAvailabilityDeprecated(); |
| 2459 | AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted(); |
| 2460 | bool IsUnavailable = AL.getUnavailableLoc().isValid(); |
| 2461 | bool IsStrict = AL.getStrictLoc().isValid(); |
| 2462 | StringRef Str; |
| 2463 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr())) |
| 2464 | Str = SE->getString(); |
| 2465 | StringRef Replacement; |
| 2466 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr())) |
| 2467 | Replacement = SE->getString(); |
| 2468 | |
| 2469 | if (II->isStr("swift")) { |
| 2470 | if (Introduced.isValid() || Obsoleted.isValid() || |
| 2471 | (!IsUnavailable && !Deprecated.isValid())) { |
| 2472 | S.Diag(AL.getLoc(), |
| 2473 | diag::warn_availability_swift_unavailable_deprecated_only); |
| 2474 | return; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | int PriorityModifier = AL.isPragmaClangAttribute() |
| 2479 | ? Sema::AP_PragmaClangAttribute |
| 2480 | : Sema::AP_Explicit; |
| 2481 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
| 2482 | ND, AL.getRange(), II, false , Introduced.Version, |
| 2483 | Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict, |
| 2484 | Replacement, Sema::AMK_None, PriorityModifier, Index); |
| 2485 | if (NewAttr) |
| 2486 | D->addAttr(NewAttr); |
| 2487 | |
| 2488 | |
| 2489 | |
| 2490 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2491 | IdentifierInfo *NewII = nullptr; |
| 2492 | if (II->getName() == "ios") |
| 2493 | NewII = &S.Context.Idents.get("watchos"); |
| 2494 | else if (II->getName() == "ios_app_extension") |
| 2495 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2496 | |
| 2497 | if (NewII) { |
| 2498 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2499 | if (Version.empty()) |
| 2500 | return Version; |
| 2501 | auto Major = Version.getMajor(); |
| 2502 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2503 | if (NewMajor >= 2) { |
| 2504 | if (Version.getMinor().hasValue()) { |
| 2505 | if (Version.getSubminor().hasValue()) |
| 2506 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2507 | Version.getSubminor().getValue()); |
| 2508 | else |
| 2509 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2510 | } |
| 2511 | return VersionTuple(NewMajor); |
| 2512 | } |
| 2513 | |
| 2514 | return VersionTuple(2, 0); |
| 2515 | }; |
| 2516 | |
| 2517 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2518 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2519 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2520 | |
| 2521 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
| 2522 | ND, AL.getRange(), NewII, true , NewIntroduced, |
| 2523 | NewDeprecated, NewObsoleted, IsUnavailable, Str, IsStrict, |
| 2524 | Replacement, Sema::AMK_None, |
| 2525 | PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index); |
| 2526 | if (NewAttr) |
| 2527 | D->addAttr(NewAttr); |
| 2528 | } |
| 2529 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2530 | |
| 2531 | |
| 2532 | IdentifierInfo *NewII = nullptr; |
| 2533 | if (II->getName() == "ios") |
| 2534 | NewII = &S.Context.Idents.get("tvos"); |
| 2535 | else if (II->getName() == "ios_app_extension") |
| 2536 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2537 | |
| 2538 | if (NewII) { |
| 2539 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
| 2540 | ND, AL.getRange(), NewII, true , Introduced.Version, |
| 2541 | Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict, |
| 2542 | Replacement, Sema::AMK_None, |
| 2543 | PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index); |
| 2544 | if (NewAttr) |
| 2545 | D->addAttr(NewAttr); |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
| 2551 | const ParsedAttr &AL) { |
| 2552 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 2553 | return; |
| 2554 | (0) . __assert_fail ("checkAttributeAtMostNumArgs(S, AL, 3) && \"Invalid number of arguments in an external_source_symbol attribute\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 2555, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(checkAttributeAtMostNumArgs(S, AL, 3) && |
| 2555 | (0) . __assert_fail ("checkAttributeAtMostNumArgs(S, AL, 3) && \"Invalid number of arguments in an external_source_symbol attribute\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 2555, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid number of arguments in an external_source_symbol attribute"); |
| 2556 | |
| 2557 | StringRef Language; |
| 2558 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0))) |
| 2559 | Language = SE->getString(); |
| 2560 | StringRef DefinedIn; |
| 2561 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1))) |
| 2562 | DefinedIn = SE->getString(); |
| 2563 | bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr; |
| 2564 | |
| 2565 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
| 2566 | AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration, |
| 2567 | AL.getAttributeSpellingListIndex())); |
| 2568 | } |
| 2569 | |
| 2570 | template <class T> |
| 2571 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2572 | typename T::VisibilityType value, |
| 2573 | unsigned attrSpellingListIndex) { |
| 2574 | T *existingAttr = D->getAttr<T>(); |
| 2575 | if (existingAttr) { |
| 2576 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2577 | if (existingValue == value) |
| 2578 | return nullptr; |
| 2579 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2580 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2581 | D->dropAttr<T>(); |
| 2582 | } |
| 2583 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2584 | } |
| 2585 | |
| 2586 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
| 2587 | VisibilityAttr::VisibilityType Vis, |
| 2588 | unsigned AttrSpellingListIndex) { |
| 2589 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2590 | AttrSpellingListIndex); |
| 2591 | } |
| 2592 | |
| 2593 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2594 | TypeVisibilityAttr::VisibilityType Vis, |
| 2595 | unsigned AttrSpellingListIndex) { |
| 2596 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2597 | AttrSpellingListIndex); |
| 2598 | } |
| 2599 | |
| 2600 | static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
| 2601 | bool isTypeVisibility) { |
| 2602 | |
| 2603 | if (isa<TypedefNameDecl>(D)) { |
| 2604 | S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL; |
| 2605 | return; |
| 2606 | } |
| 2607 | |
| 2608 | |
| 2609 | if (isTypeVisibility && |
| 2610 | !(isa<TagDecl>(D) || |
| 2611 | isa<ObjCInterfaceDecl>(D) || |
| 2612 | isa<NamespaceDecl>(D))) { |
| 2613 | S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2614 | << AL << ExpectedTypeOrNamespace; |
| 2615 | return; |
| 2616 | } |
| 2617 | |
| 2618 | |
| 2619 | StringRef TypeStr; |
| 2620 | SourceLocation LiteralLoc; |
| 2621 | if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc)) |
| 2622 | return; |
| 2623 | |
| 2624 | VisibilityAttr::VisibilityType type; |
| 2625 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
| 2626 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL |
| 2627 | << TypeStr; |
| 2628 | return; |
| 2629 | } |
| 2630 | |
| 2631 | |
| 2632 | |
| 2633 | if (type == VisibilityAttr::Protected && |
| 2634 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2635 | S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility); |
| 2636 | type = VisibilityAttr::Default; |
| 2637 | } |
| 2638 | |
| 2639 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2640 | Attr *newAttr; |
| 2641 | if (isTypeVisibility) { |
| 2642 | newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(), |
| 2643 | (TypeVisibilityAttr::VisibilityType) type, |
| 2644 | Index); |
| 2645 | } else { |
| 2646 | newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index); |
| 2647 | } |
| 2648 | if (newAttr) |
| 2649 | D->addAttr(newAttr); |
| 2650 | } |
| 2651 | |
| 2652 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2653 | const auto *M = cast<ObjCMethodDecl>(D); |
| 2654 | if (!AL.isArgIdent(0)) { |
| 2655 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 2656 | << AL << 1 << AANT_ArgumentIdentifier; |
| 2657 | return; |
| 2658 | } |
| 2659 | |
| 2660 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
| 2661 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2662 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2663 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident; |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2668 | !M->getReturnType()->isObjCObjectPointerType()) { |
| 2669 | S.Diag(M->getLocation(), diag::err_init_method_bad_return_type) |
| 2670 | << M->getReturnType(); |
| 2671 | |
| 2672 | return; |
| 2673 | } |
| 2674 | |
| 2675 | D->addAttr(new (S.Context) ObjCMethodFamilyAttr( |
| 2676 | AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex())); |
| 2677 | } |
| 2678 | |
| 2679 | static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2680 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2681 | QualType T = TD->getUnderlyingType(); |
| 2682 | if (!T->isCARCBridgableType()) { |
| 2683 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2684 | return; |
| 2685 | } |
| 2686 | } |
| 2687 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2688 | QualType T = PD->getType(); |
| 2689 | if (!T->isCARCBridgableType()) { |
| 2690 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2691 | return; |
| 2692 | } |
| 2693 | } |
| 2694 | else { |
| 2695 | |
| 2696 | |
| 2697 | |
| 2698 | |
| 2699 | |
| 2700 | |
| 2701 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
| 2702 | } |
| 2703 | D->addAttr(::new (S.Context) |
| 2704 | ObjCNSObjectAttr(AL.getRange(), S.Context, |
| 2705 | AL.getAttributeSpellingListIndex())); |
| 2706 | } |
| 2707 | |
| 2708 | static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2709 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2710 | QualType T = TD->getUnderlyingType(); |
| 2711 | if (!T->isObjCObjectPointerType()) { |
| 2712 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2713 | return; |
| 2714 | } |
| 2715 | } else { |
| 2716 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2717 | return; |
| 2718 | } |
| 2719 | D->addAttr(::new (S.Context) |
| 2720 | ObjCIndependentClassAttr(AL.getRange(), S.Context, |
| 2721 | AL.getAttributeSpellingListIndex())); |
| 2722 | } |
| 2723 | |
| 2724 | static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2725 | if (!AL.isArgIdent(0)) { |
| 2726 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 2727 | << AL << 1 << AANT_ArgumentIdentifier; |
| 2728 | return; |
| 2729 | } |
| 2730 | |
| 2731 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
| 2732 | BlocksAttr::BlockType type; |
| 2733 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2734 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
| 2735 | return; |
| 2736 | } |
| 2737 | |
| 2738 | D->addAttr(::new (S.Context) |
| 2739 | BlocksAttr(AL.getRange(), S.Context, type, |
| 2740 | AL.getAttributeSpellingListIndex())); |
| 2741 | } |
| 2742 | |
| 2743 | static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2744 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
| 2745 | if (AL.getNumArgs() > 0) { |
| 2746 | Expr *E = AL.getArgAsExpr(0); |
| 2747 | llvm::APSInt Idx(32); |
| 2748 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2749 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
| 2750 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 2751 | << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 2752 | return; |
| 2753 | } |
| 2754 | |
| 2755 | if (Idx.isSigned() && Idx.isNegative()) { |
| 2756 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2757 | << E->getSourceRange(); |
| 2758 | return; |
| 2759 | } |
| 2760 | |
| 2761 | sentinel = Idx.getZExtValue(); |
| 2762 | } |
| 2763 | |
| 2764 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
| 2765 | if (AL.getNumArgs() > 1) { |
| 2766 | Expr *E = AL.getArgAsExpr(1); |
| 2767 | llvm::APSInt Idx(32); |
| 2768 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2769 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
| 2770 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 2771 | << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 2772 | return; |
| 2773 | } |
| 2774 | nullPos = Idx.getZExtValue(); |
| 2775 | |
| 2776 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
| 2777 | |
| 2778 | |
| 2779 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2780 | << E->getSourceRange(); |
| 2781 | return; |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2786 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
| 2787 | if (isa<FunctionNoProtoType>(FT)) { |
| 2788 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2789 | return; |
| 2790 | } |
| 2791 | |
| 2792 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
| 2793 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
| 2794 | return; |
| 2795 | } |
| 2796 | } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 2797 | if (!MD->isVariadic()) { |
| 2798 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
| 2799 | return; |
| 2800 | } |
| 2801 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
| 2802 | if (!BD->isVariadic()) { |
| 2803 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2804 | return; |
| 2805 | } |
| 2806 | } else if (const auto *V = dyn_cast<VarDecl>(D)) { |
| 2807 | QualType Ty = V->getType(); |
| 2808 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| 2809 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2810 | ? D->getFunctionType() |
| 2811 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
| 2812 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
| 2813 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2814 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
| 2815 | return; |
| 2816 | } |
| 2817 | } else { |
| 2818 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2819 | << AL << ExpectedFunctionMethodOrBlock; |
| 2820 | return; |
| 2821 | } |
| 2822 | } else { |
| 2823 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2824 | << AL << ExpectedFunctionMethodOrBlock; |
| 2825 | return; |
| 2826 | } |
| 2827 | D->addAttr(::new (S.Context) |
| 2828 | SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos, |
| 2829 | AL.getAttributeSpellingListIndex())); |
| 2830 | } |
| 2831 | |
| 2832 | static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2833 | if (D->getFunctionType() && |
| 2834 | D->getFunctionType()->getReturnType()->isVoidType()) { |
| 2835 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0; |
| 2836 | return; |
| 2837 | } |
| 2838 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2839 | if (MD->getReturnType()->isVoidType()) { |
| 2840 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1; |
| 2841 | return; |
| 2842 | } |
| 2843 | |
| 2844 | |
| 2845 | |
| 2846 | if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() && |
| 2847 | !AL.getScopeName()) |
| 2848 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
| 2849 | |
| 2850 | D->addAttr(::new (S.Context) |
| 2851 | WarnUnusedResultAttr(AL.getRange(), S.Context, |
| 2852 | AL.getAttributeSpellingListIndex())); |
| 2853 | } |
| 2854 | |
| 2855 | static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2856 | |
| 2857 | bool isDef = false; |
| 2858 | if (!D->canBeWeakImported(isDef)) { |
| 2859 | if (isDef) |
| 2860 | S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2861 | << "weak_import"; |
| 2862 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
| 2863 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
| 2864 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
| 2865 | |
| 2866 | } else |
| 2867 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2868 | << AL << ExpectedVariableOrFunction; |
| 2869 | |
| 2870 | return; |
| 2871 | } |
| 2872 | |
| 2873 | D->addAttr(::new (S.Context) |
| 2874 | WeakImportAttr(AL.getRange(), S.Context, |
| 2875 | AL.getAttributeSpellingListIndex())); |
| 2876 | } |
| 2877 | |
| 2878 | |
| 2879 | template <typename WorkGroupAttr> |
| 2880 | static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2881 | uint32_t WGSize[3]; |
| 2882 | for (unsigned i = 0; i < 3; ++i) { |
| 2883 | const Expr *E = AL.getArgAsExpr(i); |
| 2884 | if (!checkUInt32Argument(S, AL, E, WGSize[i], i, |
| 2885 | )) |
| 2886 | return; |
| 2887 | if (WGSize[i] == 0) { |
| 2888 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
| 2889 | << AL << E->getSourceRange(); |
| 2890 | return; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2895 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2896 | Existing->getYDim() == WGSize[1] && |
| 2897 | Existing->getZDim() == WGSize[2])) |
| 2898 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
| 2899 | |
| 2900 | D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context, |
| 2901 | WGSize[0], WGSize[1], WGSize[2], |
| 2902 | AL.getAttributeSpellingListIndex())); |
| 2903 | } |
| 2904 | |
| 2905 | |
| 2906 | static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2907 | uint32_t SGSize; |
| 2908 | const Expr *E = AL.getArgAsExpr(0); |
| 2909 | if (!checkUInt32Argument(S, AL, E, SGSize)) |
| 2910 | return; |
| 2911 | if (SGSize == 0) { |
| 2912 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
| 2913 | << AL << E->getSourceRange(); |
| 2914 | return; |
| 2915 | } |
| 2916 | |
| 2917 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2918 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2919 | if (Existing && Existing->getSubGroupSize() != SGSize) |
| 2920 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
| 2921 | |
| 2922 | D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr( |
| 2923 | AL.getRange(), S.Context, SGSize, |
| 2924 | AL.getAttributeSpellingListIndex())); |
| 2925 | } |
| 2926 | |
| 2927 | static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2928 | if (!AL.hasParsedType()) { |
| 2929 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
| 2930 | return; |
| 2931 | } |
| 2932 | |
| 2933 | TypeSourceInfo *ParmTSI = nullptr; |
| 2934 | QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI); |
| 2935 | (0) . __assert_fail ("ParmTSI && \"no type source info for attribute argument\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 2935, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParmTSI && "no type source info for attribute argument"); |
| 2936 | |
| 2937 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2938 | (ParmType->isBooleanType() || |
| 2939 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2940 | S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2941 | << ParmType; |
| 2942 | return; |
| 2943 | } |
| 2944 | |
| 2945 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
| 2946 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
| 2947 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
| 2948 | return; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context, |
| 2953 | ParmTSI, |
| 2954 | AL.getAttributeSpellingListIndex())); |
| 2955 | } |
| 2956 | |
| 2957 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
| 2958 | StringRef Name, |
| 2959 | unsigned AttrSpellingListIndex) { |
| 2960 | |
| 2961 | |
| 2962 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2963 | if (AttrSpellingListIndex == SectionAttr::Declspec_allocate && |
| 2964 | FD->isFunctionTemplateSpecialization()) |
| 2965 | return nullptr; |
| 2966 | } |
| 2967 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2968 | if (ExistingAttr->getName() == Name) |
| 2969 | return nullptr; |
| 2970 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2971 | << 1 ; |
| 2972 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2973 | return nullptr; |
| 2974 | } |
| 2975 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2976 | AttrSpellingListIndex); |
| 2977 | } |
| 2978 | |
| 2979 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2980 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2981 | if (!Error.empty()) { |
| 2982 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
| 2983 | << 1 ; |
| 2984 | return false; |
| 2985 | } |
| 2986 | return true; |
| 2987 | } |
| 2988 | |
| 2989 | static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2990 | |
| 2991 | |
| 2992 | StringRef Str; |
| 2993 | SourceLocation LiteralLoc; |
| 2994 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
| 2995 | return; |
| 2996 | |
| 2997 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2998 | return; |
| 2999 | |
| 3000 | |
| 3001 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
| 3002 | if (!Error.empty()) { |
| 3003 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
| 3004 | << Error; |
| 3005 | return; |
| 3006 | } |
| 3007 | |
| 3008 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 3009 | SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index); |
| 3010 | if (NewAttr) |
| 3011 | D->addAttr(NewAttr); |
| 3012 | } |
| 3013 | |
| 3014 | static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) { |
| 3015 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName); |
| 3016 | if (!Error.empty()) { |
| 3017 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
| 3018 | << 0 ; |
| 3019 | return false; |
| 3020 | } |
| 3021 | return true; |
| 3022 | } |
| 3023 | |
| 3024 | CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range, |
| 3025 | StringRef Name, |
| 3026 | unsigned AttrSpellingListIndex) { |
| 3027 | |
| 3028 | |
| 3029 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 3030 | if (FD->isFunctionTemplateSpecialization()) |
| 3031 | return nullptr; |
| 3032 | } |
| 3033 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3034 | if (ExistingAttr->getName() == Name) |
| 3035 | return nullptr; |
| 3036 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 3037 | << 0 ; |
| 3038 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 3039 | return nullptr; |
| 3040 | } |
| 3041 | return ::new (Context) CodeSegAttr(Range, Context, Name, |
| 3042 | AttrSpellingListIndex); |
| 3043 | } |
| 3044 | |
| 3045 | static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3046 | StringRef Str; |
| 3047 | SourceLocation LiteralLoc; |
| 3048 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
| 3049 | return; |
| 3050 | if (!checkCodeSegName(S, LiteralLoc, Str)) |
| 3051 | return; |
| 3052 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3053 | if (!ExistingAttr->isImplicit()) { |
| 3054 | S.Diag(AL.getLoc(), |
| 3055 | ExistingAttr->getName() == Str |
| 3056 | ? diag::warn_duplicate_codeseg_attribute |
| 3057 | : diag::err_conflicting_codeseg_attribute); |
| 3058 | return; |
| 3059 | } |
| 3060 | D->dropAttr<CodeSegAttr>(); |
| 3061 | } |
| 3062 | if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str, |
| 3063 | AL.getAttributeSpellingListIndex())) |
| 3064 | D->addAttr(CSA); |
| 3065 | } |
| 3066 | |
| 3067 | |
| 3068 | |
| 3069 | bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 3070 | enum FirstParam { Unsupported, Duplicate }; |
| 3071 | enum SecondParam { None, Architecture }; |
| 3072 | for (auto Str : {"tune=", "fpmath="}) |
| 3073 | if (AttrStr.find(Str) != StringRef::npos) |
| 3074 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3075 | << Unsupported << None << Str; |
| 3076 | |
| 3077 | TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr); |
| 3078 | |
| 3079 | if (!ParsedAttrs.Architecture.empty() && |
| 3080 | !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture)) |
| 3081 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3082 | << Unsupported << Architecture << ParsedAttrs.Architecture; |
| 3083 | |
| 3084 | if (ParsedAttrs.DuplicateArchitecture) |
| 3085 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3086 | << Duplicate << None << "arch="; |
| 3087 | |
| 3088 | for (const auto &Feature : ParsedAttrs.Features) { |
| 3089 | auto CurFeature = StringRef(Feature).drop_front(); |
| 3090 | if (!Context.getTargetInfo().isValidFeatureName(CurFeature)) |
| 3091 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3092 | << Unsupported << None << CurFeature; |
| 3093 | } |
| 3094 | |
| 3095 | return false; |
| 3096 | } |
| 3097 | |
| 3098 | static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3099 | StringRef Str; |
| 3100 | SourceLocation LiteralLoc; |
| 3101 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) || |
| 3102 | S.checkTargetAttr(LiteralLoc, Str)) |
| 3103 | return; |
| 3104 | |
| 3105 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 3106 | TargetAttr *NewAttr = |
| 3107 | ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index); |
| 3108 | D->addAttr(NewAttr); |
| 3109 | } |
| 3110 | |
| 3111 | static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3112 | Expr *E = AL.getArgAsExpr(0); |
| 3113 | uint32_t VecWidth; |
| 3114 | if (!checkUInt32Argument(S, AL, E, VecWidth)) { |
| 3115 | AL.setInvalid(); |
| 3116 | return; |
| 3117 | } |
| 3118 | |
| 3119 | MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>(); |
| 3120 | if (Existing && Existing->getVectorWidth() != VecWidth) { |
| 3121 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
| 3122 | return; |
| 3123 | } |
| 3124 | |
| 3125 | D->addAttr(::new (S.Context) |
| 3126 | MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth, |
| 3127 | AL.getAttributeSpellingListIndex())); |
| 3128 | } |
| 3129 | |
| 3130 | static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3131 | Expr *E = AL.getArgAsExpr(0); |
| 3132 | SourceLocation Loc = E->getExprLoc(); |
| 3133 | FunctionDecl *FD = nullptr; |
| 3134 | DeclarationNameInfo NI; |
| 3135 | |
| 3136 | |
| 3137 | |
| 3138 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 3139 | if (DRE->hasQualifier()) |
| 3140 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3141 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3142 | NI = DRE->getNameInfo(); |
| 3143 | if (!FD) { |
| 3144 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3145 | << NI.getName(); |
| 3146 | return; |
| 3147 | } |
| 3148 | } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 3149 | if (ULE->hasExplicitTemplateArgs()) |
| 3150 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3151 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3152 | NI = ULE->getNameInfo(); |
| 3153 | if (!FD) { |
| 3154 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3155 | << NI.getName(); |
| 3156 | if (ULE->getType() == S.Context.OverloadTy) |
| 3157 | S.NoteAllOverloadCandidates(ULE); |
| 3158 | return; |
| 3159 | } |
| 3160 | } else { |
| 3161 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
| 3162 | return; |
| 3163 | } |
| 3164 | |
| 3165 | if (FD->getNumParams() != 1) { |
| 3166 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3167 | << NI.getName(); |
| 3168 | return; |
| 3169 | } |
| 3170 | |
| 3171 | |
| 3172 | |
| 3173 | QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType()); |
| 3174 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
| 3175 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3176 | ParamTy, Ty) != Sema::Compatible) { |
| 3177 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3178 | << NI.getName() << ParamTy << Ty; |
| 3179 | return; |
| 3180 | } |
| 3181 | |
| 3182 | D->addAttr(::new (S.Context) |
| 3183 | CleanupAttr(AL.getRange(), S.Context, FD, |
| 3184 | AL.getAttributeSpellingListIndex())); |
| 3185 | } |
| 3186 | |
| 3187 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
| 3188 | const ParsedAttr &AL) { |
| 3189 | if (!AL.isArgIdent(0)) { |
| 3190 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 3191 | << AL << 0 << AANT_ArgumentIdentifier; |
| 3192 | return; |
| 3193 | } |
| 3194 | |
| 3195 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
| 3196 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
| 3197 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3198 | ExtensibilityKind)) { |
| 3199 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
| 3200 | return; |
| 3201 | } |
| 3202 | |
| 3203 | D->addAttr(::new (S.Context) EnumExtensibilityAttr( |
| 3204 | AL.getRange(), S.Context, ExtensibilityKind, |
| 3205 | AL.getAttributeSpellingListIndex())); |
| 3206 | } |
| 3207 | |
| 3208 | |
| 3209 | |
| 3210 | static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3211 | Expr *IdxExpr = AL.getArgAsExpr(0); |
| 3212 | ParamIdx Idx; |
| 3213 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx)) |
| 3214 | return; |
| 3215 | |
| 3216 | |
| 3217 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
| 3218 | |
| 3219 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3220 | if (NotNSStringTy && |
| 3221 | !isCFStringType(Ty, S.Context) && |
| 3222 | (!Ty->isPointerType() || |
| 3223 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
| 3224 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
| 3225 | << "a string type" << IdxExpr->getSourceRange() |
| 3226 | << getFunctionOrMethodParamRange(D, 0); |
| 3227 | return; |
| 3228 | } |
| 3229 | Ty = getFunctionOrMethodResultType(D); |
| 3230 | if (!isNSStringType(Ty, S.Context) && |
| 3231 | !isCFStringType(Ty, S.Context) && |
| 3232 | (!Ty->isPointerType() || |
| 3233 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
| 3234 | S.Diag(AL.getLoc(), diag::err_format_attribute_result_not) |
| 3235 | << (NotNSStringTy ? "string type" : "NSString") |
| 3236 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
| 3237 | return; |
| 3238 | } |
| 3239 | |
| 3240 | D->addAttr(::new (S.Context) FormatArgAttr( |
| 3241 | AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex())); |
| 3242 | } |
| 3243 | |
| 3244 | enum FormatAttrKind { |
| 3245 | CFStringFormat, |
| 3246 | NSStringFormat, |
| 3247 | StrftimeFormat, |
| 3248 | SupportedFormat, |
| 3249 | IgnoredFormat, |
| 3250 | InvalidFormat |
| 3251 | }; |
| 3252 | |
| 3253 | |
| 3254 | |
| 3255 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
| 3256 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3257 | |
| 3258 | .Case("NSString", NSStringFormat) |
| 3259 | .Case("CFString", CFStringFormat) |
| 3260 | .Case("strftime", StrftimeFormat) |
| 3261 | |
| 3262 | |
| 3263 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3264 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3265 | .Case("kprintf", SupportedFormat) |
| 3266 | .Case("freebsd_kprintf", SupportedFormat) |
| 3267 | .Case("os_trace", SupportedFormat) |
| 3268 | .Case("os_log", SupportedFormat) |
| 3269 | |
| 3270 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3271 | .Default(InvalidFormat); |
| 3272 | } |
| 3273 | |
| 3274 | |
| 3275 | |
| 3276 | static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3277 | if (!S.getLangOpts().CPlusPlus) { |
| 3278 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
| 3279 | return; |
| 3280 | } |
| 3281 | |
| 3282 | if (S.getCurFunctionOrMethodDecl()) { |
| 3283 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3284 | AL.setInvalid(); |
| 3285 | return; |
| 3286 | } |
| 3287 | QualType T = cast<VarDecl>(D)->getType(); |
| 3288 | if (S.Context.getAsArrayType(T)) |
| 3289 | T = S.Context.getBaseElementType(T); |
| 3290 | if (!T->getAs<RecordType>()) { |
| 3291 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3292 | AL.setInvalid(); |
| 3293 | return; |
| 3294 | } |
| 3295 | |
| 3296 | Expr *E = AL.getArgAsExpr(0); |
| 3297 | uint32_t prioritynum; |
| 3298 | if (!checkUInt32Argument(S, AL, E, prioritynum)) { |
| 3299 | AL.setInvalid(); |
| 3300 | return; |
| 3301 | } |
| 3302 | |
| 3303 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3304 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range) |
| 3305 | << E->getSourceRange() << AL << 101 << 65535; |
| 3306 | AL.setInvalid(); |
| 3307 | return; |
| 3308 | } |
| 3309 | D->addAttr(::new (S.Context) |
| 3310 | InitPriorityAttr(AL.getRange(), S.Context, prioritynum, |
| 3311 | AL.getAttributeSpellingListIndex())); |
| 3312 | } |
| 3313 | |
| 3314 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3315 | IdentifierInfo *Format, int FormatIdx, |
| 3316 | int FirstArg, |
| 3317 | unsigned AttrSpellingListIndex) { |
| 3318 | |
| 3319 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3320 | if (F->getType() == Format && |
| 3321 | F->getFormatIdx() == FormatIdx && |
| 3322 | F->getFirstArg() == FirstArg) { |
| 3323 | |
| 3324 | |
| 3325 | if (F->getLocation().isInvalid()) |
| 3326 | F->setRange(Range); |
| 3327 | return nullptr; |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3332 | FirstArg, AttrSpellingListIndex); |
| 3333 | } |
| 3334 | |
| 3335 | |
| 3336 | |
| 3337 | static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3338 | if (!AL.isArgIdent(0)) { |
| 3339 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 3340 | << AL << 1 << AANT_ArgumentIdentifier; |
| 3341 | return; |
| 3342 | } |
| 3343 | |
| 3344 | |
| 3345 | |
| 3346 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3347 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
| 3348 | |
| 3349 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
| 3350 | StringRef Format = II->getName(); |
| 3351 | |
| 3352 | if (normalizeName(Format)) { |
| 3353 | |
| 3354 | II = &S.Context.Idents.get(Format); |
| 3355 | } |
| 3356 | |
| 3357 | |
| 3358 | FormatAttrKind Kind = getFormatAttrKind(Format); |
| 3359 | |
| 3360 | if (Kind == IgnoredFormat) |
| 3361 | return; |
| 3362 | |
| 3363 | if (Kind == InvalidFormat) { |
| 3364 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
| 3365 | << AL << II->getName(); |
| 3366 | return; |
| 3367 | } |
| 3368 | |
| 3369 | |
| 3370 | Expr *IdxExpr = AL.getArgAsExpr(1); |
| 3371 | uint32_t Idx; |
| 3372 | if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2)) |
| 3373 | return; |
| 3374 | |
| 3375 | if (Idx < 1 || Idx > NumArgs) { |
| 3376 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3377 | << AL << 2 << IdxExpr->getSourceRange(); |
| 3378 | return; |
| 3379 | } |
| 3380 | |
| 3381 | |
| 3382 | unsigned ArgIdx = Idx - 1; |
| 3383 | |
| 3384 | if (HasImplicitThisParam) { |
| 3385 | if (ArgIdx == 0) { |
| 3386 | S.Diag(AL.getLoc(), |
| 3387 | diag::err_format_attribute_implicit_this_format_string) |
| 3388 | << IdxExpr->getSourceRange(); |
| 3389 | return; |
| 3390 | } |
| 3391 | ArgIdx--; |
| 3392 | } |
| 3393 | |
| 3394 | |
| 3395 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
| 3396 | |
| 3397 | if (Kind == CFStringFormat) { |
| 3398 | if (!isCFStringType(Ty, S.Context)) { |
| 3399 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
| 3400 | << "a CFString" << IdxExpr->getSourceRange() |
| 3401 | << getFunctionOrMethodParamRange(D, ArgIdx); |
| 3402 | return; |
| 3403 | } |
| 3404 | } else if (Kind == NSStringFormat) { |
| 3405 | |
| 3406 | |
| 3407 | if (!isNSStringType(Ty, S.Context)) { |
| 3408 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
| 3409 | << "an NSString" << IdxExpr->getSourceRange() |
| 3410 | << getFunctionOrMethodParamRange(D, ArgIdx); |
| 3411 | return; |
| 3412 | } |
| 3413 | } else if (!Ty->isPointerType() || |
| 3414 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
| 3415 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
| 3416 | << "a string type" << IdxExpr->getSourceRange() |
| 3417 | << getFunctionOrMethodParamRange(D, ArgIdx); |
| 3418 | return; |
| 3419 | } |
| 3420 | |
| 3421 | |
| 3422 | Expr *FirstArgExpr = AL.getArgAsExpr(2); |
| 3423 | uint32_t FirstArg; |
| 3424 | if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3)) |
| 3425 | return; |
| 3426 | |
| 3427 | |
| 3428 | if (FirstArg != 0) { |
| 3429 | if (isFunctionOrMethodVariadic(D)) { |
| 3430 | ++NumArgs; |
| 3431 | } else { |
| 3432 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
| 3433 | return; |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | |
| 3438 | |
| 3439 | if (Kind == StrftimeFormat) { |
| 3440 | if (FirstArg != 0) { |
| 3441 | S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter) |
| 3442 | << FirstArgExpr->getSourceRange(); |
| 3443 | return; |
| 3444 | } |
| 3445 | |
| 3446 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
| 3447 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3448 | << AL << 3 << FirstArgExpr->getSourceRange(); |
| 3449 | return; |
| 3450 | } |
| 3451 | |
| 3452 | FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II, |
| 3453 | Idx, FirstArg, |
| 3454 | AL.getAttributeSpellingListIndex()); |
| 3455 | if (NewAttr) |
| 3456 | D->addAttr(NewAttr); |
| 3457 | } |
| 3458 | |
| 3459 | |
| 3460 | static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3461 | |
| 3462 | if (AL.getNumArgs() == 0) { |
| 3463 | S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee) |
| 3464 | << AL.getRange(); |
| 3465 | return; |
| 3466 | } |
| 3467 | |
| 3468 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3469 | int32_t NumArgs = getFunctionOrMethodNumParams(D); |
| 3470 | |
| 3471 | FunctionDecl *FD = D->getAsFunction(); |
| 3472 | (0) . __assert_fail ("FD && \"Expected a function declaration!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 3472, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FD && "Expected a function declaration!"); |
| 3473 | |
| 3474 | llvm::StringMap<int> NameIdxMapping; |
| 3475 | NameIdxMapping["__"] = -1; |
| 3476 | |
| 3477 | NameIdxMapping["this"] = 0; |
| 3478 | |
| 3479 | int Idx = 1; |
| 3480 | for (const ParmVarDecl *PVD : FD->parameters()) |
| 3481 | NameIdxMapping[PVD->getName()] = Idx++; |
| 3482 | |
| 3483 | auto UnknownName = NameIdxMapping.end(); |
| 3484 | |
| 3485 | SmallVector<int, 8> EncodingIndices; |
| 3486 | for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) { |
| 3487 | SourceRange SR; |
| 3488 | int32_t ArgIdx; |
| 3489 | |
| 3490 | if (AL.isArgIdent(I)) { |
| 3491 | IdentifierLoc *IdLoc = AL.getArgAsIdent(I); |
| 3492 | auto It = NameIdxMapping.find(IdLoc->Ident->getName()); |
| 3493 | if (It == UnknownName) { |
| 3494 | S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown) |
| 3495 | << IdLoc->Ident << IdLoc->Loc; |
| 3496 | return; |
| 3497 | } |
| 3498 | |
| 3499 | SR = SourceRange(IdLoc->Loc); |
| 3500 | ArgIdx = It->second; |
| 3501 | } else if (AL.isArgExpr(I)) { |
| 3502 | Expr *IdxExpr = AL.getArgAsExpr(I); |
| 3503 | |
| 3504 | |
| 3505 | if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1, |
| 3506 | false)) { |
| 3507 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3508 | << AL << (I + 1) << IdxExpr->getSourceRange(); |
| 3509 | return; |
| 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | if (ArgIdx < -1 || ArgIdx > NumArgs) { |
| 3514 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3515 | << AL << (I + 1) << IdxExpr->getSourceRange(); |
| 3516 | return; |
| 3517 | } |
| 3518 | |
| 3519 | SR = IdxExpr->getSourceRange(); |
| 3520 | } else { |
| 3521 | llvm_unreachable("Unexpected ParsedAttr argument type!"); |
| 3522 | } |
| 3523 | |
| 3524 | if (ArgIdx == 0 && !HasImplicitThisParam) { |
| 3525 | S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available) |
| 3526 | << (I + 1) << SR; |
| 3527 | return; |
| 3528 | } |
| 3529 | |
| 3530 | |
| 3531 | |
| 3532 | if (!HasImplicitThisParam && ArgIdx > 0) |
| 3533 | ArgIdx -= 1; |
| 3534 | |
| 3535 | EncodingIndices.push_back(ArgIdx); |
| 3536 | } |
| 3537 | |
| 3538 | int CalleeIdx = EncodingIndices.front(); |
| 3539 | |
| 3540 | |
| 3541 | |
| 3542 | if (CalleeIdx < (int)HasImplicitThisParam) { |
| 3543 | S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee) |
| 3544 | << AL.getRange(); |
| 3545 | return; |
| 3546 | } |
| 3547 | |
| 3548 | |
| 3549 | |
| 3550 | const Type *CalleeType = |
| 3551 | getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam) |
| 3552 | .getTypePtr(); |
| 3553 | if (!CalleeType || !CalleeType->isFunctionPointerType()) { |
| 3554 | S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type) |
| 3555 | << AL.getRange(); |
| 3556 | return; |
| 3557 | } |
| 3558 | |
| 3559 | const Type *CalleeFnType = |
| 3560 | CalleeType->getPointeeType()->getUnqualifiedDesugaredType(); |
| 3561 | |
| 3562 | |
| 3563 | |
| 3564 | const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType); |
| 3565 | if (!CalleeFnProtoType) { |
| 3566 | S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type) |
| 3567 | << AL.getRange(); |
| 3568 | return; |
| 3569 | } |
| 3570 | |
| 3571 | if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) { |
| 3572 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3573 | << AL << (unsigned)(EncodingIndices.size() - 1); |
| 3574 | return; |
| 3575 | } |
| 3576 | |
| 3577 | if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) { |
| 3578 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3579 | << AL << (unsigned)(EncodingIndices.size() - 1); |
| 3580 | return; |
| 3581 | } |
| 3582 | |
| 3583 | if (CalleeFnProtoType->isVariadic()) { |
| 3584 | S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange(); |
| 3585 | return; |
| 3586 | } |
| 3587 | |
| 3588 | |
| 3589 | if (D->hasAttr<CallbackAttr>()) { |
| 3590 | S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange(); |
| 3591 | return; |
| 3592 | } |
| 3593 | |
| 3594 | D->addAttr(::new (S.Context) CallbackAttr( |
| 3595 | AL.getRange(), S.Context, EncodingIndices.data(), EncodingIndices.size(), |
| 3596 | AL.getAttributeSpellingListIndex())); |
| 3597 | } |
| 3598 | |
| 3599 | static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3600 | |
| 3601 | RecordDecl *RD = nullptr; |
| 3602 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
| 3603 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3604 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3605 | else |
| 3606 | RD = dyn_cast<RecordDecl>(D); |
| 3607 | |
| 3608 | if (!RD || !RD->isUnion()) { |
| 3609 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL |
| 3610 | << ExpectedUnion; |
| 3611 | return; |
| 3612 | } |
| 3613 | |
| 3614 | if (!RD->isCompleteDefinition()) { |
| 3615 | if (!RD->isBeingDefined()) |
| 3616 | S.Diag(AL.getLoc(), |
| 3617 | diag::warn_transparent_union_attribute_not_definition); |
| 3618 | return; |
| 3619 | } |
| 3620 | |
| 3621 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3622 | FieldEnd = RD->field_end(); |
| 3623 | if (Field == FieldEnd) { |
| 3624 | S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3625 | return; |
| 3626 | } |
| 3627 | |
| 3628 | FieldDecl *FirstField = *Field; |
| 3629 | QualType FirstType = FirstField->getType(); |
| 3630 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
| 3631 | S.Diag(FirstField->getLocation(), |
| 3632 | diag::warn_transparent_union_attribute_floating) |
| 3633 | << FirstType->isVectorType() << FirstType; |
| 3634 | return; |
| 3635 | } |
| 3636 | |
| 3637 | if (FirstType->isIncompleteType()) |
| 3638 | return; |
| 3639 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3640 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3641 | for (; Field != FieldEnd; ++Field) { |
| 3642 | QualType FieldType = Field->getType(); |
| 3643 | if (FieldType->isIncompleteType()) |
| 3644 | return; |
| 3645 | |
| 3646 | |
| 3647 | |
| 3648 | |
| 3649 | |
| 3650 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3651 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
| 3652 | |
| 3653 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
| 3654 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
| 3655 | : S.Context.getTypeAlign(FieldType); |
| 3656 | S.Diag(Field->getLocation(), |
| 3657 | diag::warn_transparent_union_attribute_field_size_align) |
| 3658 | << isSize << Field->getDeclName() << FieldBits; |
| 3659 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
| 3660 | S.Diag(FirstField->getLocation(), |
| 3661 | diag::note_transparent_union_first_field_size_align) |
| 3662 | << isSize << FirstBits; |
| 3663 | return; |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | RD->addAttr(::new (S.Context) |
| 3668 | TransparentUnionAttr(AL.getRange(), S.Context, |
| 3669 | AL.getAttributeSpellingListIndex())); |
| 3670 | } |
| 3671 | |
| 3672 | static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3673 | |
| 3674 | |
| 3675 | StringRef Str; |
| 3676 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
| 3677 | return; |
| 3678 | |
| 3679 | |
| 3680 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3681 | if (I->getAnnotation() == Str) |
| 3682 | return; |
| 3683 | } |
| 3684 | |
| 3685 | D->addAttr(::new (S.Context) |
| 3686 | AnnotateAttr(AL.getRange(), S.Context, Str, |
| 3687 | AL.getAttributeSpellingListIndex())); |
| 3688 | } |
| 3689 | |
| 3690 | static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3691 | S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 3692 | AL.getAttributeSpellingListIndex()); |
| 3693 | } |
| 3694 | |
| 3695 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3696 | unsigned SpellingListIndex) { |
| 3697 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3698 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3699 | |
| 3700 | QualType T; |
| 3701 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3702 | T = TD->getUnderlyingType(); |
| 3703 | else if (const auto *VD = dyn_cast<ValueDecl>(D)) |
| 3704 | T = VD->getType(); |
| 3705 | else |
| 3706 | llvm_unreachable("Unknown decl type for align_value"); |
| 3707 | |
| 3708 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3709 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3710 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3711 | << &TmpAttr << T << D->getSourceRange(); |
| 3712 | return; |
| 3713 | } |
| 3714 | |
| 3715 | if (!E->isValueDependent()) { |
| 3716 | llvm::APSInt Alignment; |
| 3717 | ExprResult ICE |
| 3718 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3719 | diag::err_align_value_attribute_argument_not_int, |
| 3720 | false); |
| 3721 | if (ICE.isInvalid()) |
| 3722 | return; |
| 3723 | |
| 3724 | if (!Alignment.isPowerOf2()) { |
| 3725 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3726 | << E->getSourceRange(); |
| 3727 | return; |
| 3728 | } |
| 3729 | |
| 3730 | D->addAttr(::new (Context) |
| 3731 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3732 | SpellingListIndex)); |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | |
| 3737 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
| 3738 | } |
| 3739 | |
| 3740 | static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3741 | |
| 3742 | if (AL.getNumArgs() > 1) { |
| 3743 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
| 3744 | return; |
| 3745 | } |
| 3746 | |
| 3747 | if (AL.getNumArgs() == 0) { |
| 3748 | D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context, |
| 3749 | true, nullptr, AL.getAttributeSpellingListIndex())); |
| 3750 | return; |
| 3751 | } |
| 3752 | |
| 3753 | Expr *E = AL.getArgAsExpr(0); |
| 3754 | if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3755 | S.Diag(AL.getEllipsisLoc(), |
| 3756 | diag::err_pack_expansion_without_parameter_packs); |
| 3757 | return; |
| 3758 | } |
| 3759 | |
| 3760 | if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3761 | return; |
| 3762 | |
| 3763 | S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(), |
| 3764 | AL.isPackExpansion()); |
| 3765 | } |
| 3766 | |
| 3767 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3768 | unsigned SpellingListIndex, bool IsPackExpansion) { |
| 3769 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3770 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3771 | |
| 3772 | |
| 3773 | if (TmpAttr.isAlignas()) { |
| 3774 | |
| 3775 | |
| 3776 | |
| 3777 | |
| 3778 | |
| 3779 | |
| 3780 | |
| 3781 | |
| 3782 | |
| 3783 | |
| 3784 | |
| 3785 | int DiagKind = -1; |
| 3786 | if (isa<ParmVarDecl>(D)) { |
| 3787 | DiagKind = 0; |
| 3788 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 3789 | if (VD->getStorageClass() == SC_Register) |
| 3790 | DiagKind = 1; |
| 3791 | if (VD->isExceptionVariable()) |
| 3792 | DiagKind = 2; |
| 3793 | } else if (const auto *FD = dyn_cast<FieldDecl>(D)) { |
| 3794 | if (FD->isBitField()) |
| 3795 | DiagKind = 3; |
| 3796 | } else if (!isa<TagDecl>(D)) { |
| 3797 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
| 3798 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3799 | : ExpectedVariableFieldOrTag); |
| 3800 | return; |
| 3801 | } |
| 3802 | if (DiagKind != -1) { |
| 3803 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
| 3804 | << &TmpAttr << DiagKind; |
| 3805 | return; |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | if (E->isValueDependent()) { |
| 3810 | |
| 3811 | |
| 3812 | |
| 3813 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3814 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3815 | Diag(AttrLoc, diag::err_alignment_dependent_typedef_name) |
| 3816 | << E->getSourceRange(); |
| 3817 | return; |
| 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | |
| 3822 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3823 | AA->setPackExpansion(IsPackExpansion); |
| 3824 | D->addAttr(AA); |
| 3825 | return; |
| 3826 | } |
| 3827 | |
| 3828 | |
| 3829 | llvm::APSInt Alignment; |
| 3830 | ExprResult ICE |
| 3831 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3832 | diag::err_aligned_attribute_argument_not_int, |
| 3833 | false); |
| 3834 | if (ICE.isInvalid()) |
| 3835 | return; |
| 3836 | |
| 3837 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3838 | |
| 3839 | |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | |
| 3844 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
| 3845 | if (!llvm::isPowerOf2_64(AlignVal)) { |
| 3846 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3847 | << E->getSourceRange(); |
| 3848 | return; |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | |
| 3853 | unsigned MaxValidAlignment = |
| 3854 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3855 | : 268435456; |
| 3856 | if (AlignVal > MaxValidAlignment) { |
| 3857 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3858 | << E->getSourceRange(); |
| 3859 | return; |
| 3860 | } |
| 3861 | |
| 3862 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3863 | unsigned MaxTLSAlign = |
| 3864 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3865 | .getQuantity(); |
| 3866 | const auto *VD = dyn_cast<VarDecl>(D); |
| 3867 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3868 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3869 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3870 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3871 | return; |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3876 | ICE.get(), SpellingListIndex); |
| 3877 | AA->setPackExpansion(IsPackExpansion); |
| 3878 | D->addAttr(AA); |
| 3879 | } |
| 3880 | |
| 3881 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
| 3882 | unsigned SpellingListIndex, bool IsPackExpansion) { |
| 3883 | |
| 3884 | |
| 3885 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3886 | SpellingListIndex); |
| 3887 | AA->setPackExpansion(IsPackExpansion); |
| 3888 | D->addAttr(AA); |
| 3889 | } |
| 3890 | |
| 3891 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3892 | (0) . __assert_fail ("D->hasAttrs() && \"no attributes on decl\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 3892, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->hasAttrs() && "no attributes on decl"); |
| 3893 | |
| 3894 | QualType UnderlyingTy, DiagTy; |
| 3895 | if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
| 3896 | UnderlyingTy = DiagTy = VD->getType(); |
| 3897 | } else { |
| 3898 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3899 | if (const auto *ED = dyn_cast<EnumDecl>(D)) |
| 3900 | UnderlyingTy = ED->getIntegerType(); |
| 3901 | } |
| 3902 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
| 3903 | return; |
| 3904 | |
| 3905 | |
| 3906 | |
| 3907 | |
| 3908 | |
| 3909 | AlignedAttr *AlignasAttr = nullptr; |
| 3910 | unsigned Align = 0; |
| 3911 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
| 3912 | if (I->isAlignmentDependent()) |
| 3913 | return; |
| 3914 | if (I->isAlignas()) |
| 3915 | AlignasAttr = I; |
| 3916 | Align = std::max(Align, I->getAlignment(Context)); |
| 3917 | } |
| 3918 | |
| 3919 | if (AlignasAttr && Align) { |
| 3920 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3921 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
| 3922 | if (NaturalAlign > RequestedAlign) |
| 3923 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3924 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | bool Sema::checkMSInheritanceAttrOnDefinition( |
| 3929 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
| 3930 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3931 | (0) . __assert_fail ("RD->hasDefinition() && \"RD has no definition!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 3931, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RD->hasDefinition() && "RD has no definition!"); |
| 3932 | |
| 3933 | |
| 3934 | |
| 3935 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3936 | return false; |
| 3937 | |
| 3938 | |
| 3939 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3940 | return false; |
| 3941 | |
| 3942 | if (BestCase) { |
| 3943 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3944 | return false; |
| 3945 | } else { |
| 3946 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3947 | return false; |
| 3948 | } |
| 3949 | |
| 3950 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3951 | << 0 ; |
| 3952 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3953 | << RD->getNameAsString(); |
| 3954 | return true; |
| 3955 | } |
| 3956 | |
| 3957 | |
| 3958 | |
| 3959 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3960 | bool &IntegerMode, bool &ComplexMode) { |
| 3961 | IntegerMode = true; |
| 3962 | ComplexMode = false; |
| 3963 | switch (Str.size()) { |
| 3964 | case 2: |
| 3965 | switch (Str[0]) { |
| 3966 | case 'Q': |
| 3967 | DestWidth = 8; |
| 3968 | break; |
| 3969 | case 'H': |
| 3970 | DestWidth = 16; |
| 3971 | break; |
| 3972 | case 'S': |
| 3973 | DestWidth = 32; |
| 3974 | break; |
| 3975 | case 'D': |
| 3976 | DestWidth = 64; |
| 3977 | break; |
| 3978 | case 'X': |
| 3979 | DestWidth = 96; |
| 3980 | break; |
| 3981 | case 'T': |
| 3982 | DestWidth = 128; |
| 3983 | break; |
| 3984 | } |
| 3985 | if (Str[1] == 'F') { |
| 3986 | IntegerMode = false; |
| 3987 | } else if (Str[1] == 'C') { |
| 3988 | IntegerMode = false; |
| 3989 | ComplexMode = true; |
| 3990 | } else if (Str[1] != 'I') { |
| 3991 | DestWidth = 0; |
| 3992 | } |
| 3993 | break; |
| 3994 | case 4: |
| 3995 | |
| 3996 | |
| 3997 | if (Str == "word") |
| 3998 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
| 3999 | else if (Str == "byte") |
| 4000 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
| 4001 | break; |
| 4002 | case 7: |
| 4003 | if (Str == "pointer") |
| 4004 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
| 4005 | break; |
| 4006 | case 11: |
| 4007 | if (Str == "unwind_word") |
| 4008 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
| 4009 | break; |
| 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | |
| 4014 | |
| 4015 | |
| 4016 | |
| 4017 | |
| 4018 | |
| 4019 | static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4020 | |
| 4021 | |
| 4022 | if (!AL.isArgIdent(0)) { |
| 4023 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 4024 | << AL << AANT_ArgumentIdentifier; |
| 4025 | return; |
| 4026 | } |
| 4027 | |
| 4028 | IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident; |
| 4029 | |
| 4030 | S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex()); |
| 4031 | } |
| 4032 | |
| 4033 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 4034 | unsigned SpellingListIndex, bool InInstantiation) { |
| 4035 | StringRef Str = Name->getName(); |
| 4036 | normalizeName(Str); |
| 4037 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 4038 | |
| 4039 | unsigned DestWidth = 0; |
| 4040 | bool IntegerMode = true; |
| 4041 | bool ComplexMode = false; |
| 4042 | llvm::APInt VectorSize(64, 0); |
| 4043 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 4044 | |
| 4045 | size_t StrSize = Str.size(); |
| 4046 | size_t VectorStringLength = 0; |
| 4047 | while ((VectorStringLength + 1) < StrSize && |
| 4048 | isdigit(Str[VectorStringLength + 1])) |
| 4049 | ++VectorStringLength; |
| 4050 | if (VectorStringLength && |
| 4051 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 4052 | VectorSize.isPowerOf2()) { |
| 4053 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
| 4054 | IntegerMode, ComplexMode); |
| 4055 | |
| 4056 | if (!InInstantiation) |
| 4057 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
| 4058 | } else { |
| 4059 | VectorSize = 0; |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | if (!VectorSize) |
| 4064 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 4065 | |
| 4066 | |
| 4067 | |
| 4068 | |
| 4069 | |
| 4070 | if (!DestWidth) { |
| 4071 | Diag(AttrLoc, diag::err_machine_mode) << 0 << Name; |
| 4072 | return; |
| 4073 | } |
| 4074 | |
| 4075 | QualType OldTy; |
| 4076 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
| 4077 | OldTy = TD->getUnderlyingType(); |
| 4078 | else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
| 4079 | |
| 4080 | |
| 4081 | OldTy = ED->getIntegerType(); |
| 4082 | if (OldTy.isNull()) |
| 4083 | OldTy = Context.IntTy; |
| 4084 | } else |
| 4085 | OldTy = cast<ValueDecl>(D)->getType(); |
| 4086 | |
| 4087 | if (OldTy->isDependentType()) { |
| 4088 | D->addAttr(::new (Context) |
| 4089 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 4090 | return; |
| 4091 | } |
| 4092 | |
| 4093 | |
| 4094 | |
| 4095 | QualType OldElemTy = OldTy; |
| 4096 | if (const auto *VT = OldTy->getAs<VectorType>()) |
| 4097 | OldElemTy = VT->getElementType(); |
| 4098 | |
| 4099 | |
| 4100 | |
| 4101 | |
| 4102 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 4103 | VectorSize.getBoolValue()) { |
| 4104 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 4105 | return; |
| 4106 | } |
| 4107 | bool IntegralOrAnyEnumType = |
| 4108 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 4109 | |
| 4110 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 4111 | !IntegralOrAnyEnumType) |
| 4112 | Diag(AttrLoc, diag::err_mode_not_primitive); |
| 4113 | else if (IntegerMode) { |
| 4114 | if (!IntegralOrAnyEnumType) |
| 4115 | Diag(AttrLoc, diag::err_mode_wrong_type); |
| 4116 | } else if (ComplexMode) { |
| 4117 | if (!OldElemTy->isComplexType()) |
| 4118 | Diag(AttrLoc, diag::err_mode_wrong_type); |
| 4119 | } else { |
| 4120 | if (!OldElemTy->isFloatingType()) |
| 4121 | Diag(AttrLoc, diag::err_mode_wrong_type); |
| 4122 | } |
| 4123 | |
| 4124 | QualType NewElemTy; |
| 4125 | |
| 4126 | if (IntegerMode) |
| 4127 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 4128 | OldElemTy->isSignedIntegerType()); |
| 4129 | else |
| 4130 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
| 4131 | |
| 4132 | if (NewElemTy.isNull()) { |
| 4133 | Diag(AttrLoc, diag::err_machine_mode) << 1 << Name; |
| 4134 | return; |
| 4135 | } |
| 4136 | |
| 4137 | if (ComplexMode) { |
| 4138 | NewElemTy = Context.getComplexType(NewElemTy); |
| 4139 | } |
| 4140 | |
| 4141 | QualType NewTy = NewElemTy; |
| 4142 | if (VectorSize.getBoolValue()) { |
| 4143 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 4144 | VectorType::GenericVector); |
| 4145 | } else if (const auto *OldVT = OldTy->getAs<VectorType>()) { |
| 4146 | |
| 4147 | if (ComplexMode) { |
| 4148 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
| 4149 | return; |
| 4150 | } |
| 4151 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
| 4152 | OldVT->getNumElements() / |
| 4153 | Context.getTypeSize(NewElemTy); |
| 4154 | NewTy = |
| 4155 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
| 4156 | } |
| 4157 | |
| 4158 | if (NewTy.isNull()) { |
| 4159 | Diag(AttrLoc, diag::err_mode_wrong_type); |
| 4160 | return; |
| 4161 | } |
| 4162 | |
| 4163 | |
| 4164 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) |
| 4165 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 4166 | else if (auto *ED = dyn_cast<EnumDecl>(D)) |
| 4167 | ED->setIntegerType(NewTy); |
| 4168 | else |
| 4169 | cast<ValueDecl>(D)->setType(NewTy); |
| 4170 | |
| 4171 | D->addAttr(::new (Context) |
| 4172 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 4173 | } |
| 4174 | |
| 4175 | static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4176 | D->addAttr(::new (S.Context) |
| 4177 | NoDebugAttr(AL.getRange(), S.Context, |
| 4178 | AL.getAttributeSpellingListIndex())); |
| 4179 | } |
| 4180 | |
| 4181 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
| 4182 | IdentifierInfo *Ident, |
| 4183 | unsigned AttrSpellingListIndex) { |
| 4184 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4185 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
| 4186 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4187 | return nullptr; |
| 4188 | } |
| 4189 | |
| 4190 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 4191 | return nullptr; |
| 4192 | |
| 4193 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 4194 | AttrSpellingListIndex); |
| 4195 | } |
| 4196 | |
| 4197 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) { |
| 4198 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
| 4199 | return nullptr; |
| 4200 | |
| 4201 | return ::new (Context) |
| 4202 | CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
| 4203 | } |
| 4204 | |
| 4205 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) { |
| 4206 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
| 4207 | return nullptr; |
| 4208 | |
| 4209 | return ::new (Context) |
| 4210 | CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4211 | } |
| 4212 | |
| 4213 | InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D, |
| 4214 | const ParsedAttr &AL) { |
| 4215 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 4216 | |
| 4217 | |
| 4218 | if (VD->getKind() != Decl::Var) { |
| 4219 | Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4220 | << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4221 | : ExpectedVariableOrFunction); |
| 4222 | return nullptr; |
| 4223 | } |
| 4224 | |
| 4225 | if (VD->hasLocalStorage()) { |
| 4226 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4227 | return nullptr; |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
| 4232 | return nullptr; |
| 4233 | |
| 4234 | return ::new (Context) InternalLinkageAttr( |
| 4235 | AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
| 4236 | } |
| 4237 | InternalLinkageAttr * |
| 4238 | Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) { |
| 4239 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 4240 | |
| 4241 | |
| 4242 | if (VD->getKind() != Decl::Var) { |
| 4243 | Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4244 | << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4245 | : ExpectedVariableOrFunction); |
| 4246 | return nullptr; |
| 4247 | } |
| 4248 | |
| 4249 | if (VD->hasLocalStorage()) { |
| 4250 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4251 | return nullptr; |
| 4252 | } |
| 4253 | } |
| 4254 | |
| 4255 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
| 4256 | return nullptr; |
| 4257 | |
| 4258 | return ::new (Context) |
| 4259 | InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4260 | } |
| 4261 | |
| 4262 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 4263 | unsigned AttrSpellingListIndex) { |
| 4264 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4265 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 4266 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4267 | return nullptr; |
| 4268 | } |
| 4269 | |
| 4270 | if (D->hasAttr<MinSizeAttr>()) |
| 4271 | return nullptr; |
| 4272 | |
| 4273 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 4274 | } |
| 4275 | |
| 4276 | NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr( |
| 4277 | Decl *D, const NoSpeculativeLoadHardeningAttr &AL) { |
| 4278 | if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL)) |
| 4279 | return nullptr; |
| 4280 | |
| 4281 | return ::new (Context) NoSpeculativeLoadHardeningAttr( |
| 4282 | AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4283 | } |
| 4284 | |
| 4285 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 4286 | unsigned AttrSpellingListIndex) { |
| 4287 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4288 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 4289 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4290 | D->dropAttr<AlwaysInlineAttr>(); |
| 4291 | } |
| 4292 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4293 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 4294 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4295 | D->dropAttr<MinSizeAttr>(); |
| 4296 | } |
| 4297 | |
| 4298 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4299 | return nullptr; |
| 4300 | |
| 4301 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 4302 | AttrSpellingListIndex); |
| 4303 | } |
| 4304 | |
| 4305 | SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr( |
| 4306 | Decl *D, const SpeculativeLoadHardeningAttr &AL) { |
| 4307 | if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL)) |
| 4308 | return nullptr; |
| 4309 | |
| 4310 | return ::new (Context) SpeculativeLoadHardeningAttr( |
| 4311 | AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4312 | } |
| 4313 | |
| 4314 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4315 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL)) |
| 4316 | return; |
| 4317 | |
| 4318 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 4319 | D, AL.getRange(), AL.getName(), |
| 4320 | AL.getAttributeSpellingListIndex())) |
| 4321 | D->addAttr(Inline); |
| 4322 | } |
| 4323 | |
| 4324 | static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4325 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 4326 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
| 4327 | D->addAttr(MinSize); |
| 4328 | } |
| 4329 | |
| 4330 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4331 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 4332 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
| 4333 | D->addAttr(Optnone); |
| 4334 | } |
| 4335 | |
| 4336 | static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4337 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL)) |
| 4338 | return; |
| 4339 | const auto *VD = cast<VarDecl>(D); |
| 4340 | if (!VD->hasGlobalStorage()) { |
| 4341 | S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant); |
| 4342 | return; |
| 4343 | } |
| 4344 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
| 4345 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 4346 | } |
| 4347 | |
| 4348 | static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4349 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL)) |
| 4350 | return; |
| 4351 | const auto *VD = cast<VarDecl>(D); |
| 4352 | |
| 4353 | |
| 4354 | if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() && |
| 4355 | !isa<IncompleteArrayType>(VD->getType())) { |
| 4356 | S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD; |
| 4357 | return; |
| 4358 | } |
| 4359 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
| 4360 | S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared) |
| 4361 | << S.CurrentCUDATarget()) |
| 4362 | return; |
| 4363 | D->addAttr(::new (S.Context) CUDASharedAttr( |
| 4364 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 4365 | } |
| 4366 | |
| 4367 | static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4368 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) || |
| 4369 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) { |
| 4370 | return; |
| 4371 | } |
| 4372 | const auto *FD = cast<FunctionDecl>(D); |
| 4373 | if (!FD->getReturnType()->isVoidType()) { |
| 4374 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4375 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 4376 | << FD->getType() |
| 4377 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4378 | : FixItHint()); |
| 4379 | return; |
| 4380 | } |
| 4381 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4382 | if (Method->isInstance()) { |
| 4383 | S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method) |
| 4384 | << Method; |
| 4385 | return; |
| 4386 | } |
| 4387 | S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method; |
| 4388 | } |
| 4389 | |
| 4390 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
| 4391 | S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD; |
| 4392 | |
| 4393 | D->addAttr(::new (S.Context) |
| 4394 | CUDAGlobalAttr(AL.getRange(), S.Context, |
| 4395 | AL.getAttributeSpellingListIndex())); |
| 4396 | } |
| 4397 | |
| 4398 | static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4399 | const auto *Fn = cast<FunctionDecl>(D); |
| 4400 | if (!Fn->isInlineSpecified()) { |
| 4401 | S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
| 4402 | return; |
| 4403 | } |
| 4404 | |
| 4405 | D->addAttr(::new (S.Context) |
| 4406 | GNUInlineAttr(AL.getRange(), S.Context, |
| 4407 | AL.getAttributeSpellingListIndex())); |
| 4408 | } |
| 4409 | |
| 4410 | static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4411 | if (hasDeclarator(D)) return; |
| 4412 | |
| 4413 | |
| 4414 | |
| 4415 | CallingConv CC; |
| 4416 | if (S.CheckCallingConvAttr(AL, CC, )) |
| 4417 | return; |
| 4418 | |
| 4419 | if (!isa<ObjCMethodDecl>(D)) { |
| 4420 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4421 | << AL << ExpectedFunctionOrMethod; |
| 4422 | return; |
| 4423 | } |
| 4424 | |
| 4425 | switch (AL.getKind()) { |
| 4426 | case ParsedAttr::AT_FastCall: |
| 4427 | D->addAttr(::new (S.Context) |
| 4428 | FastCallAttr(AL.getRange(), S.Context, |
| 4429 | AL.getAttributeSpellingListIndex())); |
| 4430 | return; |
| 4431 | case ParsedAttr::AT_StdCall: |
| 4432 | D->addAttr(::new (S.Context) |
| 4433 | StdCallAttr(AL.getRange(), S.Context, |
| 4434 | AL.getAttributeSpellingListIndex())); |
| 4435 | return; |
| 4436 | case ParsedAttr::AT_ThisCall: |
| 4437 | D->addAttr(::new (S.Context) |
| 4438 | ThisCallAttr(AL.getRange(), S.Context, |
| 4439 | AL.getAttributeSpellingListIndex())); |
| 4440 | return; |
| 4441 | case ParsedAttr::AT_CDecl: |
| 4442 | D->addAttr(::new (S.Context) |
| 4443 | CDeclAttr(AL.getRange(), S.Context, |
| 4444 | AL.getAttributeSpellingListIndex())); |
| 4445 | return; |
| 4446 | case ParsedAttr::AT_Pascal: |
| 4447 | D->addAttr(::new (S.Context) |
| 4448 | PascalAttr(AL.getRange(), S.Context, |
| 4449 | AL.getAttributeSpellingListIndex())); |
| 4450 | return; |
| 4451 | case ParsedAttr::AT_SwiftCall: |
| 4452 | D->addAttr(::new (S.Context) |
| 4453 | SwiftCallAttr(AL.getRange(), S.Context, |
| 4454 | AL.getAttributeSpellingListIndex())); |
| 4455 | return; |
| 4456 | case ParsedAttr::AT_VectorCall: |
| 4457 | D->addAttr(::new (S.Context) |
| 4458 | VectorCallAttr(AL.getRange(), S.Context, |
| 4459 | AL.getAttributeSpellingListIndex())); |
| 4460 | return; |
| 4461 | case ParsedAttr::AT_MSABI: |
| 4462 | D->addAttr(::new (S.Context) |
| 4463 | MSABIAttr(AL.getRange(), S.Context, |
| 4464 | AL.getAttributeSpellingListIndex())); |
| 4465 | return; |
| 4466 | case ParsedAttr::AT_SysVABI: |
| 4467 | D->addAttr(::new (S.Context) |
| 4468 | SysVABIAttr(AL.getRange(), S.Context, |
| 4469 | AL.getAttributeSpellingListIndex())); |
| 4470 | return; |
| 4471 | case ParsedAttr::AT_RegCall: |
| 4472 | D->addAttr(::new (S.Context) RegCallAttr( |
| 4473 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 4474 | return; |
| 4475 | case ParsedAttr::AT_Pcs: { |
| 4476 | PcsAttr::PCSType PCS; |
| 4477 | switch (CC) { |
| 4478 | case CC_AAPCS: |
| 4479 | PCS = PcsAttr::AAPCS; |
| 4480 | break; |
| 4481 | case CC_AAPCS_VFP: |
| 4482 | PCS = PcsAttr::AAPCS_VFP; |
| 4483 | break; |
| 4484 | default: |
| 4485 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
| 4486 | } |
| 4487 | |
| 4488 | D->addAttr(::new (S.Context) |
| 4489 | PcsAttr(AL.getRange(), S.Context, PCS, |
| 4490 | AL.getAttributeSpellingListIndex())); |
| 4491 | return; |
| 4492 | } |
| 4493 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4494 | D->addAttr(::new(S.Context) |
| 4495 | AArch64VectorPcsAttr(AL.getRange(), S.Context, |
| 4496 | AL.getAttributeSpellingListIndex())); |
| 4497 | return; |
| 4498 | case ParsedAttr::AT_IntelOclBicc: |
| 4499 | D->addAttr(::new (S.Context) |
| 4500 | IntelOclBiccAttr(AL.getRange(), S.Context, |
| 4501 | AL.getAttributeSpellingListIndex())); |
| 4502 | return; |
| 4503 | case ParsedAttr::AT_PreserveMost: |
| 4504 | D->addAttr(::new (S.Context) PreserveMostAttr( |
| 4505 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 4506 | return; |
| 4507 | case ParsedAttr::AT_PreserveAll: |
| 4508 | D->addAttr(::new (S.Context) PreserveAllAttr( |
| 4509 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 4510 | return; |
| 4511 | default: |
| 4512 | llvm_unreachable("unexpected attribute kind"); |
| 4513 | } |
| 4514 | } |
| 4515 | |
| 4516 | static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4517 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 4518 | return; |
| 4519 | |
| 4520 | std::vector<StringRef> DiagnosticIdentifiers; |
| 4521 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
| 4522 | StringRef RuleName; |
| 4523 | |
| 4524 | if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr)) |
| 4525 | return; |
| 4526 | |
| 4527 | |
| 4528 | |
| 4529 | DiagnosticIdentifiers.push_back(RuleName); |
| 4530 | } |
| 4531 | D->addAttr(::new (S.Context) SuppressAttr( |
| 4532 | AL.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 4533 | DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex())); |
| 4534 | } |
| 4535 | |
| 4536 | bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC, |
| 4537 | const FunctionDecl *FD) { |
| 4538 | if (Attrs.isInvalid()) |
| 4539 | return true; |
| 4540 | |
| 4541 | if (Attrs.hasProcessingCache()) { |
| 4542 | CC = (CallingConv) Attrs.getProcessingCache(); |
| 4543 | return false; |
| 4544 | } |
| 4545 | |
| 4546 | unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0; |
| 4547 | if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) { |
| 4548 | Attrs.setInvalid(); |
| 4549 | return true; |
| 4550 | } |
| 4551 | |
| 4552 | |
| 4553 | switch (Attrs.getKind()) { |
| 4554 | case ParsedAttr::AT_CDecl: |
| 4555 | CC = CC_C; |
| 4556 | break; |
| 4557 | case ParsedAttr::AT_FastCall: |
| 4558 | CC = CC_X86FastCall; |
| 4559 | break; |
| 4560 | case ParsedAttr::AT_StdCall: |
| 4561 | CC = CC_X86StdCall; |
| 4562 | break; |
| 4563 | case ParsedAttr::AT_ThisCall: |
| 4564 | CC = CC_X86ThisCall; |
| 4565 | break; |
| 4566 | case ParsedAttr::AT_Pascal: |
| 4567 | CC = CC_X86Pascal; |
| 4568 | break; |
| 4569 | case ParsedAttr::AT_SwiftCall: |
| 4570 | CC = CC_Swift; |
| 4571 | break; |
| 4572 | case ParsedAttr::AT_VectorCall: |
| 4573 | CC = CC_X86VectorCall; |
| 4574 | break; |
| 4575 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4576 | CC = CC_AArch64VectorCall; |
| 4577 | break; |
| 4578 | case ParsedAttr::AT_RegCall: |
| 4579 | CC = CC_X86RegCall; |
| 4580 | break; |
| 4581 | case ParsedAttr::AT_MSABI: |
| 4582 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 4583 | CC_Win64; |
| 4584 | break; |
| 4585 | case ParsedAttr::AT_SysVABI: |
| 4586 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4587 | CC_C; |
| 4588 | break; |
| 4589 | case ParsedAttr::AT_Pcs: { |
| 4590 | StringRef StrRef; |
| 4591 | if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) { |
| 4592 | Attrs.setInvalid(); |
| 4593 | return true; |
| 4594 | } |
| 4595 | if (StrRef == "aapcs") { |
| 4596 | CC = CC_AAPCS; |
| 4597 | break; |
| 4598 | } else if (StrRef == "aapcs-vfp") { |
| 4599 | CC = CC_AAPCS_VFP; |
| 4600 | break; |
| 4601 | } |
| 4602 | |
| 4603 | Attrs.setInvalid(); |
| 4604 | Diag(Attrs.getLoc(), diag::err_invalid_pcs); |
| 4605 | return true; |
| 4606 | } |
| 4607 | case ParsedAttr::AT_IntelOclBicc: |
| 4608 | CC = CC_IntelOclBicc; |
| 4609 | break; |
| 4610 | case ParsedAttr::AT_PreserveMost: |
| 4611 | CC = CC_PreserveMost; |
| 4612 | break; |
| 4613 | case ParsedAttr::AT_PreserveAll: |
| 4614 | CC = CC_PreserveAll; |
| 4615 | break; |
| 4616 | default: llvm_unreachable("unexpected attribute kind"); |
| 4617 | } |
| 4618 | |
| 4619 | TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK; |
| 4620 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4621 | |
| 4622 | |
| 4623 | |
| 4624 | |
| 4625 | if (LangOpts.CUDA) { |
| 4626 | auto *Aux = Context.getAuxTargetInfo(); |
| 4627 | auto CudaTarget = IdentifyCUDATarget(FD); |
| 4628 | bool CheckHost = false, CheckDevice = false; |
| 4629 | switch (CudaTarget) { |
| 4630 | case CFT_HostDevice: |
| 4631 | CheckHost = true; |
| 4632 | CheckDevice = true; |
| 4633 | break; |
| 4634 | case CFT_Host: |
| 4635 | CheckHost = true; |
| 4636 | break; |
| 4637 | case CFT_Device: |
| 4638 | case CFT_Global: |
| 4639 | CheckDevice = true; |
| 4640 | break; |
| 4641 | case CFT_InvalidTarget: |
| 4642 | llvm_unreachable("unexpected cuda target"); |
| 4643 | } |
| 4644 | auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI; |
| 4645 | auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux; |
| 4646 | if (CheckHost && HostTI) |
| 4647 | A = HostTI->checkCallingConvention(CC); |
| 4648 | if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI) |
| 4649 | A = DeviceTI->checkCallingConvention(CC); |
| 4650 | } else { |
| 4651 | A = TI.checkCallingConvention(CC); |
| 4652 | } |
| 4653 | if (A != TargetInfo::CCCR_OK) { |
| 4654 | if (A == TargetInfo::CCCR_Warning) |
| 4655 | Diag(Attrs.getLoc(), diag::warn_cconv_ignored) |
| 4656 | << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget; |
| 4657 | |
| 4658 | |
| 4659 | |
| 4660 | bool IsCXXMethod = false, IsVariadic = false; |
| 4661 | if (FD) { |
| 4662 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4663 | IsVariadic = FD->isVariadic(); |
| 4664 | } |
| 4665 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
| 4666 | } |
| 4667 | |
| 4668 | Attrs.setProcessingCache((unsigned) CC); |
| 4669 | return false; |
| 4670 | } |
| 4671 | |
| 4672 | |
| 4673 | static bool isValidSwiftContextType(QualType Ty) { |
| 4674 | if (!Ty->hasPointerRepresentation()) |
| 4675 | return Ty->isDependentType(); |
| 4676 | return Ty->getPointeeType().getAddressSpace() == LangAS::Default; |
| 4677 | } |
| 4678 | |
| 4679 | |
| 4680 | static bool isValidSwiftIndirectResultType(QualType Ty) { |
| 4681 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4682 | Ty = PtrType->getPointeeType(); |
| 4683 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4684 | Ty = RefType->getPointeeType(); |
| 4685 | } else { |
| 4686 | return Ty->isDependentType(); |
| 4687 | } |
| 4688 | return Ty.getAddressSpace() == LangAS::Default; |
| 4689 | } |
| 4690 | |
| 4691 | |
| 4692 | static bool isValidSwiftErrorResultType(QualType Ty) { |
| 4693 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4694 | Ty = PtrType->getPointeeType(); |
| 4695 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4696 | Ty = RefType->getPointeeType(); |
| 4697 | } else { |
| 4698 | return Ty->isDependentType(); |
| 4699 | } |
| 4700 | if (!Ty.getQualifiers().empty()) |
| 4701 | return false; |
| 4702 | return isValidSwiftContextType(Ty); |
| 4703 | } |
| 4704 | |
| 4705 | static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs, |
| 4706 | ParameterABI Abi) { |
| 4707 | S.AddParameterABIAttr(Attrs.getRange(), D, Abi, |
| 4708 | Attrs.getAttributeSpellingListIndex()); |
| 4709 | } |
| 4710 | |
| 4711 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4712 | unsigned spellingIndex) { |
| 4713 | |
| 4714 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4715 | |
| 4716 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4717 | if (existingAttr->getABI() != abi) { |
| 4718 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4719 | << getParameterABISpelling(abi) << existingAttr; |
| 4720 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4721 | return; |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | switch (abi) { |
| 4726 | case ParameterABI::Ordinary: |
| 4727 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4728 | |
| 4729 | case ParameterABI::SwiftContext: |
| 4730 | if (!isValidSwiftContextType(type)) { |
| 4731 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4732 | << getParameterABISpelling(abi) |
| 4733 | << 0 << type; |
| 4734 | } |
| 4735 | D->addAttr(::new (Context) |
| 4736 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4737 | return; |
| 4738 | |
| 4739 | case ParameterABI::SwiftErrorResult: |
| 4740 | if (!isValidSwiftErrorResultType(type)) { |
| 4741 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4742 | << getParameterABISpelling(abi) |
| 4743 | << 1 << type; |
| 4744 | } |
| 4745 | D->addAttr(::new (Context) |
| 4746 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4747 | return; |
| 4748 | |
| 4749 | case ParameterABI::SwiftIndirectResult: |
| 4750 | if (!isValidSwiftIndirectResultType(type)) { |
| 4751 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4752 | << getParameterABISpelling(abi) |
| 4753 | << 0 << type; |
| 4754 | } |
| 4755 | D->addAttr(::new (Context) |
| 4756 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4757 | return; |
| 4758 | } |
| 4759 | llvm_unreachable("bad parameter ABI attribute"); |
| 4760 | } |
| 4761 | |
| 4762 | |
| 4763 | |
| 4764 | bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) { |
| 4765 | if (AL.isInvalid()) |
| 4766 | return true; |
| 4767 | |
| 4768 | if (!checkAttributeNumArgs(*this, AL, 1)) { |
| 4769 | AL.setInvalid(); |
| 4770 | return true; |
| 4771 | } |
| 4772 | |
| 4773 | uint32_t NP; |
| 4774 | Expr *NumParamsExpr = AL.getArgAsExpr(0); |
| 4775 | if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) { |
| 4776 | AL.setInvalid(); |
| 4777 | return true; |
| 4778 | } |
| 4779 | |
| 4780 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
| 4781 | Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform) |
| 4782 | << NumParamsExpr->getSourceRange(); |
| 4783 | AL.setInvalid(); |
| 4784 | return true; |
| 4785 | } |
| 4786 | |
| 4787 | numParams = NP; |
| 4788 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
| 4789 | Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 4790 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
| 4791 | AL.setInvalid(); |
| 4792 | return true; |
| 4793 | } |
| 4794 | |
| 4795 | return false; |
| 4796 | } |
| 4797 | |
| 4798 | |
| 4799 | |
| 4800 | |
| 4801 | |
| 4802 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
| 4803 | const CUDALaunchBoundsAttr &AL, |
| 4804 | const unsigned Idx) { |
| 4805 | if (S.DiagnoseUnexpandedParameterPack(E)) |
| 4806 | return nullptr; |
| 4807 | |
| 4808 | |
| 4809 | |
| 4810 | if (E->isValueDependent()) |
| 4811 | return E; |
| 4812 | |
| 4813 | llvm::APSInt I(64); |
| 4814 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4815 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 4816 | << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 4817 | return nullptr; |
| 4818 | } |
| 4819 | |
| 4820 | if (!I.isIntN(32)) { |
| 4821 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4822 | << 32 << 1; |
| 4823 | return nullptr; |
| 4824 | } |
| 4825 | if (I < 0) |
| 4826 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 4827 | << &AL << Idx << E->getSourceRange(); |
| 4828 | |
| 4829 | |
| 4830 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4831 | S.Context, S.Context.getConstType(S.Context.IntTy), false); |
| 4832 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4833 | (0) . __assert_fail ("!ValArg.isInvalid() && \"Unexpected PerformCopyInitialization() failure.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 4834, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ValArg.isInvalid() && |
| 4834 | (0) . __assert_fail ("!ValArg.isInvalid() && \"Unexpected PerformCopyInitialization() failure.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 4834, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unexpected PerformCopyInitialization() failure."); |
| 4835 | |
| 4836 | return ValArg.getAs<Expr>(); |
| 4837 | } |
| 4838 | |
| 4839 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4840 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4841 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4842 | SpellingListIndex); |
| 4843 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4844 | if (MaxThreads == nullptr) |
| 4845 | return; |
| 4846 | |
| 4847 | if (MinBlocks) { |
| 4848 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4849 | if (MinBlocks == nullptr) |
| 4850 | return; |
| 4851 | } |
| 4852 | |
| 4853 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4854 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4855 | } |
| 4856 | |
| 4857 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4858 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 4859 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
| 4860 | return; |
| 4861 | |
| 4862 | S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 4863 | AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr, |
| 4864 | AL.getAttributeSpellingListIndex()); |
| 4865 | } |
| 4866 | |
| 4867 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4868 | const ParsedAttr &AL) { |
| 4869 | if (!AL.isArgIdent(0)) { |
| 4870 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 4871 | << AL << 1 << AANT_ArgumentIdentifier; |
| 4872 | return; |
| 4873 | } |
| 4874 | |
| 4875 | ParamIdx ArgumentIdx; |
| 4876 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1), |
| 4877 | ArgumentIdx)) |
| 4878 | return; |
| 4879 | |
| 4880 | ParamIdx TypeTagIdx; |
| 4881 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2), |
| 4882 | TypeTagIdx)) |
| 4883 | return; |
| 4884 | |
| 4885 | bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag"; |
| 4886 | if (IsPointer) { |
| 4887 | |
| 4888 | unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex(); |
| 4889 | if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) || |
| 4890 | !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType()) |
| 4891 | S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0; |
| 4892 | } |
| 4893 | |
| 4894 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr( |
| 4895 | AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx, |
| 4896 | TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex())); |
| 4897 | } |
| 4898 | |
| 4899 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4900 | const ParsedAttr &AL) { |
| 4901 | if (!AL.isArgIdent(0)) { |
| 4902 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 4903 | << AL << 1 << AANT_ArgumentIdentifier; |
| 4904 | return; |
| 4905 | } |
| 4906 | |
| 4907 | if (!checkAttributeNumArgs(S, AL, 1)) |
| 4908 | return; |
| 4909 | |
| 4910 | if (!isa<VarDecl>(D)) { |
| 4911 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4912 | << AL << ExpectedVariable; |
| 4913 | return; |
| 4914 | } |
| 4915 | |
| 4916 | IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident; |
| 4917 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
| 4918 | S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc); |
| 4919 | (0) . __assert_fail ("MatchingCTypeLoc && \"no type source info for attribute argument\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 4919, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
| 4920 | |
| 4921 | D->addAttr(::new (S.Context) |
| 4922 | TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind, |
| 4923 | MatchingCTypeLoc, |
| 4924 | AL.getLayoutCompatible(), |
| 4925 | AL.getMustBeNull(), |
| 4926 | AL.getAttributeSpellingListIndex())); |
| 4927 | } |
| 4928 | |
| 4929 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4930 | ParamIdx ArgCount; |
| 4931 | |
| 4932 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0), |
| 4933 | ArgCount, |
| 4934 | true )) |
| 4935 | return; |
| 4936 | |
| 4937 | |
| 4938 | D->addAttr(::new (S.Context) XRayLogArgsAttr( |
| 4939 | AL.getRange(), S.Context, ArgCount.getSourceIndex(), |
| 4940 | AL.getAttributeSpellingListIndex())); |
| 4941 | } |
| 4942 | |
| 4943 | |
| 4944 | |
| 4945 | |
| 4946 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) { |
| 4947 | return QT->isDependentType() || QT->isObjCRetainableType(); |
| 4948 | } |
| 4949 | |
| 4950 | static bool isValidSubjectOfNSAttribute(QualType QT) { |
| 4951 | return QT->isDependentType() || QT->isObjCObjectPointerType() || |
| 4952 | QT->isObjCNSObjectType(); |
| 4953 | } |
| 4954 | |
| 4955 | static bool isValidSubjectOfCFAttribute(QualType QT) { |
| 4956 | return QT->isDependentType() || QT->isPointerType() || |
| 4957 | isValidSubjectOfNSAttribute(QT); |
| 4958 | } |
| 4959 | |
| 4960 | static bool isValidSubjectOfOSAttribute(QualType QT) { |
| 4961 | if (QT->isDependentType()) |
| 4962 | return true; |
| 4963 | QualType PT = QT->getPointeeType(); |
| 4964 | return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr; |
| 4965 | } |
| 4966 | |
| 4967 | void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex, |
| 4968 | RetainOwnershipKind K, |
| 4969 | bool IsTemplateInstantiation) { |
| 4970 | ValueDecl *VD = cast<ValueDecl>(D); |
| 4971 | switch (K) { |
| 4972 | case RetainOwnershipKind::OS: |
| 4973 | handleSimpleAttributeOrDiagnose<OSConsumedAttr>( |
| 4974 | *this, VD, SR, SpellingIndex, isValidSubjectOfOSAttribute(VD->getType()), |
| 4975 | diag::warn_ns_attribute_wrong_parameter_type, |
| 4976 | SR, "os_consumed", 1); |
| 4977 | return; |
| 4978 | case RetainOwnershipKind::NS: |
| 4979 | handleSimpleAttributeOrDiagnose<NSConsumedAttr>( |
| 4980 | *this, VD, SR, SpellingIndex, isValidSubjectOfNSAttribute(VD->getType()), |
| 4981 | |
| 4982 | |
| 4983 | |
| 4984 | |
| 4985 | |
| 4986 | ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount) |
| 4987 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4988 | : diag::warn_ns_attribute_wrong_parameter_type), |
| 4989 | SR, "ns_consumed", 0); |
| 4990 | return; |
| 4991 | case RetainOwnershipKind::CF: |
| 4992 | handleSimpleAttributeOrDiagnose<CFConsumedAttr>( |
| 4993 | *this, VD, SR, SpellingIndex, |
| 4994 | isValidSubjectOfCFAttribute(VD->getType()), |
| 4995 | diag::warn_ns_attribute_wrong_parameter_type, |
| 4996 | SR, "cf_consumed", ); |
| 4997 | return; |
| 4998 | } |
| 4999 | } |
| 5000 | |
| 5001 | static Sema::RetainOwnershipKind |
| 5002 | parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) { |
| 5003 | switch (AL.getKind()) { |
| 5004 | case ParsedAttr::AT_CFConsumed: |
| 5005 | case ParsedAttr::AT_CFReturnsRetained: |
| 5006 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 5007 | return Sema::RetainOwnershipKind::CF; |
| 5008 | case ParsedAttr::AT_OSConsumesThis: |
| 5009 | case ParsedAttr::AT_OSConsumed: |
| 5010 | case ParsedAttr::AT_OSReturnsRetained: |
| 5011 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5012 | case ParsedAttr::AT_OSReturnsRetainedOnZero: |
| 5013 | case ParsedAttr::AT_OSReturnsRetainedOnNonZero: |
| 5014 | return Sema::RetainOwnershipKind::OS; |
| 5015 | case ParsedAttr::AT_NSConsumesSelf: |
| 5016 | case ParsedAttr::AT_NSConsumed: |
| 5017 | case ParsedAttr::AT_NSReturnsRetained: |
| 5018 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 5019 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5020 | return Sema::RetainOwnershipKind::NS; |
| 5021 | default: |
| 5022 | llvm_unreachable("Wrong argument supplied"); |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) { |
| 5027 | if (isValidSubjectOfNSReturnsRetainedAttribute(QT)) |
| 5028 | return false; |
| 5029 | |
| 5030 | Diag(Loc, diag::warn_ns_attribute_wrong_return_type) |
| 5031 | << "'ns_returns_retained'" << 0 << 0; |
| 5032 | return true; |
| 5033 | } |
| 5034 | |
| 5035 | |
| 5036 | static bool isValidOSObjectOutParameter(const Decl *D) { |
| 5037 | const auto *PVD = dyn_cast<ParmVarDecl>(D); |
| 5038 | if (!PVD) |
| 5039 | return false; |
| 5040 | QualType QT = PVD->getType(); |
| 5041 | QualType PT = QT->getPointeeType(); |
| 5042 | return !PT.isNull() && isValidSubjectOfOSAttribute(PT); |
| 5043 | } |
| 5044 | |
| 5045 | static void handleXReturnsXRetainedAttr(Sema &S, Decl *D, |
| 5046 | const ParsedAttr &AL) { |
| 5047 | QualType ReturnType; |
| 5048 | Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL); |
| 5049 | |
| 5050 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 5051 | ReturnType = MD->getReturnType(); |
| 5052 | } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
| 5053 | (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) { |
| 5054 | return; |
| 5055 | } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 5056 | ReturnType = PD->getType(); |
| 5057 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 5058 | ReturnType = FD->getReturnType(); |
| 5059 | } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 5060 | |
| 5061 | |
| 5062 | unsigned DiagID = K == Sema::RetainOwnershipKind::CF |
| 5063 | ? |
| 5064 | : ; |
| 5065 | ReturnType = Param->getType()->getPointeeType(); |
| 5066 | if (ReturnType.isNull()) { |
| 5067 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
| 5068 | << AL << DiagID << AL.getRange(); |
| 5069 | return; |
| 5070 | } |
| 5071 | } else if (AL.isUsedAsTypeAttr()) { |
| 5072 | return; |
| 5073 | } else { |
| 5074 | AttributeDeclKind ExpectedDeclKind; |
| 5075 | switch (AL.getKind()) { |
| 5076 | default: llvm_unreachable("invalid ownership attribute"); |
| 5077 | case ParsedAttr::AT_NSReturnsRetained: |
| 5078 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5079 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 5080 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 5081 | break; |
| 5082 | |
| 5083 | case ParsedAttr::AT_OSReturnsRetained: |
| 5084 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5085 | case ParsedAttr::AT_CFReturnsRetained: |
| 5086 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 5087 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 5088 | break; |
| 5089 | } |
| 5090 | S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type) |
| 5091 | << AL.getRange() << AL << ExpectedDeclKind; |
| 5092 | return; |
| 5093 | } |
| 5094 | |
| 5095 | bool TypeOK; |
| 5096 | bool Cf; |
| 5097 | unsigned ParmDiagID = 2; |
| 5098 | switch (AL.getKind()) { |
| 5099 | default: llvm_unreachable("invalid ownership attribute"); |
| 5100 | case ParsedAttr::AT_NSReturnsRetained: |
| 5101 | TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType); |
| 5102 | Cf = false; |
| 5103 | break; |
| 5104 | |
| 5105 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5106 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 5107 | TypeOK = isValidSubjectOfNSAttribute(ReturnType); |
| 5108 | Cf = false; |
| 5109 | break; |
| 5110 | |
| 5111 | case ParsedAttr::AT_CFReturnsRetained: |
| 5112 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 5113 | TypeOK = isValidSubjectOfCFAttribute(ReturnType); |
| 5114 | Cf = true; |
| 5115 | break; |
| 5116 | |
| 5117 | case ParsedAttr::AT_OSReturnsRetained: |
| 5118 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5119 | TypeOK = isValidSubjectOfOSAttribute(ReturnType); |
| 5120 | Cf = true; |
| 5121 | ParmDiagID = 3; |
| 5122 | break; |
| 5123 | } |
| 5124 | |
| 5125 | if (!TypeOK) { |
| 5126 | if (AL.isUsedAsTypeAttr()) |
| 5127 | return; |
| 5128 | |
| 5129 | if (isa<ParmVarDecl>(D)) { |
| 5130 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
| 5131 | << AL << ParmDiagID << AL.getRange(); |
| 5132 | } else { |
| 5133 | |
| 5134 | enum : unsigned { |
| 5135 | Function, |
| 5136 | Method, |
| 5137 | Property |
| 5138 | } SubjectKind = Function; |
| 5139 | if (isa<ObjCMethodDecl>(D)) |
| 5140 | SubjectKind = Method; |
| 5141 | else if (isa<ObjCPropertyDecl>(D)) |
| 5142 | SubjectKind = Property; |
| 5143 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 5144 | << AL << SubjectKind << Cf << AL.getRange(); |
| 5145 | } |
| 5146 | return; |
| 5147 | } |
| 5148 | |
| 5149 | switch (AL.getKind()) { |
| 5150 | default: |
| 5151 | llvm_unreachable("invalid ownership attribute"); |
| 5152 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5153 | handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL); |
| 5154 | return; |
| 5155 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 5156 | handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL); |
| 5157 | return; |
| 5158 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 5159 | handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL); |
| 5160 | return; |
| 5161 | case ParsedAttr::AT_CFReturnsRetained: |
| 5162 | handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL); |
| 5163 | return; |
| 5164 | case ParsedAttr::AT_NSReturnsRetained: |
| 5165 | handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL); |
| 5166 | return; |
| 5167 | case ParsedAttr::AT_OSReturnsRetained: |
| 5168 | handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL); |
| 5169 | return; |
| 5170 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5171 | handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL); |
| 5172 | return; |
| 5173 | }; |
| 5174 | } |
| 5175 | |
| 5176 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 5177 | const ParsedAttr &Attrs) { |
| 5178 | const int EP_ObjCMethod = 1; |
| 5179 | const int EP_ObjCProperty = 2; |
| 5180 | |
| 5181 | SourceLocation loc = Attrs.getLoc(); |
| 5182 | QualType resultType; |
| 5183 | if (isa<ObjCMethodDecl>(D)) |
| 5184 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
| 5185 | else |
| 5186 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
| 5187 | |
| 5188 | if (!resultType->isReferenceType() && |
| 5189 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
| 5190 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 5191 | << SourceRange(loc) << Attrs |
| 5192 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
| 5193 | << 2; |
| 5194 | |
| 5195 | |
| 5196 | return; |
| 5197 | } |
| 5198 | |
| 5199 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 5200 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
| 5201 | } |
| 5202 | |
| 5203 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 5204 | const ParsedAttr &Attrs) { |
| 5205 | const auto *Method = cast<ObjCMethodDecl>(D); |
| 5206 | |
| 5207 | const DeclContext *DC = Method->getDeclContext(); |
| 5208 | if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 5209 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
| 5210 | << 0; |
| 5211 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 5212 | return; |
| 5213 | } |
| 5214 | if (Method->getMethodFamily() == OMF_dealloc) { |
| 5215 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
| 5216 | << 1; |
| 5217 | return; |
| 5218 | } |
| 5219 | |
| 5220 | D->addAttr(::new (S.Context) ObjCRequiresSuperAttr( |
| 5221 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
| 5222 | } |
| 5223 | |
| 5224 | static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5225 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
| 5226 | |
| 5227 | if (!Parm) { |
| 5228 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
| 5229 | return; |
| 5230 | } |
| 5231 | |
| 5232 | |
| 5233 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5234 | if (!Parm->Ident->isStr("id")) { |
| 5235 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL; |
| 5236 | return; |
| 5237 | } |
| 5238 | |
| 5239 | |
| 5240 | QualType T = TD->getUnderlyingType(); |
| 5241 | if (!T->isVoidPointerType()) { |
| 5242 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 5243 | return; |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | D->addAttr(::new (S.Context) |
| 5248 | ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident, |
| 5249 | AL.getAttributeSpellingListIndex())); |
| 5250 | } |
| 5251 | |
| 5252 | static void handleObjCBridgeMutableAttr(Sema &S, Decl *D, |
| 5253 | const ParsedAttr &AL) { |
| 5254 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
| 5255 | |
| 5256 | if (!Parm) { |
| 5257 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
| 5258 | return; |
| 5259 | } |
| 5260 | |
| 5261 | D->addAttr(::new (S.Context) |
| 5262 | ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident, |
| 5263 | AL.getAttributeSpellingListIndex())); |
| 5264 | } |
| 5265 | |
| 5266 | static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D, |
| 5267 | const ParsedAttr &AL) { |
| 5268 | IdentifierInfo *RelatedClass = |
| 5269 | AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr; |
| 5270 | if (!RelatedClass) { |
| 5271 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
| 5272 | return; |
| 5273 | } |
| 5274 | IdentifierInfo *ClassMethod = |
| 5275 | AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr; |
| 5276 | IdentifierInfo *InstanceMethod = |
| 5277 | AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr; |
| 5278 | D->addAttr(::new (S.Context) |
| 5279 | ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass, |
| 5280 | ClassMethod, InstanceMethod, |
| 5281 | AL.getAttributeSpellingListIndex())); |
| 5282 | } |
| 5283 | |
| 5284 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 5285 | const ParsedAttr &AL) { |
| 5286 | DeclContext *Ctx = D->getDeclContext(); |
| 5287 | |
| 5288 | |
| 5289 | |
| 5290 | if (!isa<ObjCInterfaceDecl>(Ctx) && |
| 5291 | !(isa<ObjCCategoryDecl>(Ctx) && |
| 5292 | cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) { |
| 5293 | S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init); |
| 5294 | return; |
| 5295 | } |
| 5296 | |
| 5297 | ObjCInterfaceDecl *IFace; |
| 5298 | if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx)) |
| 5299 | IFace = CatDecl->getClassInterface(); |
| 5300 | else |
| 5301 | IFace = cast<ObjCInterfaceDecl>(Ctx); |
| 5302 | |
| 5303 | if (!IFace) |
| 5304 | return; |
| 5305 | |
| 5306 | IFace->setHasDesignatedInitializers(); |
| 5307 | D->addAttr(::new (S.Context) |
| 5308 | ObjCDesignatedInitializerAttr(AL.getRange(), S.Context, |
| 5309 | AL.getAttributeSpellingListIndex())); |
| 5310 | } |
| 5311 | |
| 5312 | static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5313 | StringRef MetaDataName; |
| 5314 | if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName)) |
| 5315 | return; |
| 5316 | D->addAttr(::new (S.Context) |
| 5317 | ObjCRuntimeNameAttr(AL.getRange(), S.Context, |
| 5318 | MetaDataName, |
| 5319 | AL.getAttributeSpellingListIndex())); |
| 5320 | } |
| 5321 | |
| 5322 | |
| 5323 | |
| 5324 | |
| 5325 | |
| 5326 | static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5327 | bool notify = false; |
| 5328 | |
| 5329 | auto *RD = dyn_cast<RecordDecl>(D); |
| 5330 | if (RD && RD->getDefinition()) { |
| 5331 | RD = RD->getDefinition(); |
| 5332 | notify = true; |
| 5333 | } |
| 5334 | |
| 5335 | if (RD) { |
| 5336 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 5337 | ObjCBoxableAttr(AL.getRange(), S.Context, |
| 5338 | AL.getAttributeSpellingListIndex()); |
| 5339 | RD->addAttr(BoxableAttr); |
| 5340 | if (notify) { |
| 5341 | |
| 5342 | |
| 5343 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 5344 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 5345 | } |
| 5346 | } |
| 5347 | } |
| 5348 | |
| 5349 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5350 | if (hasDeclarator(D)) return; |
| 5351 | |
| 5352 | S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type) |
| 5353 | << AL.getRange() << AL << ExpectedVariable; |
| 5354 | } |
| 5355 | |
| 5356 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 5357 | const ParsedAttr &AL) { |
| 5358 | const auto *VD = cast<ValueDecl>(D); |
| 5359 | QualType QT = VD->getType(); |
| 5360 | |
| 5361 | if (!QT->isDependentType() && |
| 5362 | !QT->isObjCLifetimeType()) { |
| 5363 | S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
| 5364 | << QT; |
| 5365 | return; |
| 5366 | } |
| 5367 | |
| 5368 | Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime(); |
| 5369 | |
| 5370 | |
| 5371 | |
| 5372 | if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType()) |
| 5373 | Lifetime = QT->getObjCARCImplicitLifetime(); |
| 5374 | |
| 5375 | switch (Lifetime) { |
| 5376 | case Qualifiers::OCL_None: |
| 5377 | (0) . __assert_fail ("QT->isDependentType() && \"didn't infer lifetime for non-dependent type?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 5378, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(QT->isDependentType() && |
| 5378 | (0) . __assert_fail ("QT->isDependentType() && \"didn't infer lifetime for non-dependent type?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 5378, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "didn't infer lifetime for non-dependent type?"); |
| 5379 | break; |
| 5380 | |
| 5381 | case Qualifiers::OCL_Weak: |
| 5382 | case Qualifiers::OCL_Strong: |
| 5383 | break; |
| 5384 | |
| 5385 | case Qualifiers::OCL_ExplicitNone: |
| 5386 | case Qualifiers::OCL_Autoreleasing: |
| 5387 | S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
| 5388 | << (Lifetime == Qualifiers::OCL_Autoreleasing); |
| 5389 | break; |
| 5390 | } |
| 5391 | |
| 5392 | D->addAttr(::new (S.Context) |
| 5393 | ObjCPreciseLifetimeAttr(AL.getRange(), S.Context, |
| 5394 | AL.getAttributeSpellingListIndex())); |
| 5395 | } |
| 5396 | |
| 5397 | |
| 5398 | |
| 5399 | |
| 5400 | |
| 5401 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 5402 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 5403 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
| 5404 | if (UA->getGuid().equals_lower(Uuid)) |
| 5405 | return nullptr; |
| 5406 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5407 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 5408 | D->dropAttr<UuidAttr>(); |
| 5409 | } |
| 5410 | |
| 5411 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 5412 | } |
| 5413 | |
| 5414 | static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5415 | if (!S.LangOpts.CPlusPlus) { |
| 5416 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 5417 | << AL << AttributeLangSupport::C; |
| 5418 | return; |
| 5419 | } |
| 5420 | |
| 5421 | StringRef StrRef; |
| 5422 | SourceLocation LiteralLoc; |
| 5423 | if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc)) |
| 5424 | return; |
| 5425 | |
| 5426 | |
| 5427 | |
| 5428 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5429 | StrRef = StrRef.drop_front().drop_back(); |
| 5430 | |
| 5431 | |
| 5432 | if (StrRef.size() != 36) { |
| 5433 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
| 5434 | return; |
| 5435 | } |
| 5436 | |
| 5437 | for (unsigned i = 0; i < 36; ++i) { |
| 5438 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 5439 | if (StrRef[i] != '-') { |
| 5440 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
| 5441 | return; |
| 5442 | } |
| 5443 | } else if (!isHexDigit(StrRef[i])) { |
| 5444 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
| 5445 | return; |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | |
| 5450 | |
| 5451 | |
| 5452 | |
| 5453 | |
| 5454 | |
| 5455 | if (AL.isMicrosoftAttribute()) |
| 5456 | S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated); |
| 5457 | |
| 5458 | UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(), |
| 5459 | AL.getAttributeSpellingListIndex(), StrRef); |
| 5460 | if (UA) |
| 5461 | D->addAttr(UA); |
| 5462 | } |
| 5463 | |
| 5464 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5465 | if (!S.LangOpts.CPlusPlus) { |
| 5466 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 5467 | << AL << AttributeLangSupport::C; |
| 5468 | return; |
| 5469 | } |
| 5470 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
| 5471 | D, AL.getRange(), , |
| 5472 | AL.getAttributeSpellingListIndex(), |
| 5473 | (MSInheritanceAttr::Spelling)AL.getSemanticSpelling()); |
| 5474 | if (IA) { |
| 5475 | D->addAttr(IA); |
| 5476 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5477 | } |
| 5478 | } |
| 5479 | |
| 5480 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5481 | const auto *VD = cast<VarDecl>(D); |
| 5482 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 5483 | S.Diag(AL.getLoc(), diag::err_thread_unsupported); |
| 5484 | return; |
| 5485 | } |
| 5486 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 5487 | S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 5488 | return; |
| 5489 | } |
| 5490 | if (VD->hasLocalStorage()) { |
| 5491 | S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 5492 | return; |
| 5493 | } |
| 5494 | D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context, |
| 5495 | AL.getAttributeSpellingListIndex())); |
| 5496 | } |
| 5497 | |
| 5498 | static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5499 | SmallVector<StringRef, 4> Tags; |
| 5500 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
| 5501 | StringRef Tag; |
| 5502 | if (!S.checkStringLiteralArgumentAttr(AL, I, Tag)) |
| 5503 | return; |
| 5504 | Tags.push_back(Tag); |
| 5505 | } |
| 5506 | |
| 5507 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5508 | if (!NS->isInline()) { |
| 5509 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
| 5510 | return; |
| 5511 | } |
| 5512 | if (NS->isAnonymousNamespace()) { |
| 5513 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
| 5514 | return; |
| 5515 | } |
| 5516 | if (AL.getNumArgs() == 0) |
| 5517 | Tags.push_back(NS->getName()); |
| 5518 | } else if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 5519 | return; |
| 5520 | |
| 5521 | |
| 5522 | llvm::sort(Tags); |
| 5523 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5524 | |
| 5525 | D->addAttr(::new (S.Context) |
| 5526 | AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(), |
| 5527 | AL.getAttributeSpellingListIndex())); |
| 5528 | } |
| 5529 | |
| 5530 | static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5531 | |
| 5532 | if (AL.getNumArgs() > 1) { |
| 5533 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
| 5534 | return; |
| 5535 | } |
| 5536 | |
| 5537 | StringRef Str; |
| 5538 | SourceLocation ArgLoc; |
| 5539 | |
| 5540 | if (AL.getNumArgs() == 0) |
| 5541 | Str = ""; |
| 5542 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5543 | return; |
| 5544 | |
| 5545 | ARMInterruptAttr::InterruptType Kind; |
| 5546 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5547 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5548 | << ArgLoc; |
| 5549 | return; |
| 5550 | } |
| 5551 | |
| 5552 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 5553 | D->addAttr(::new (S.Context) |
| 5554 | ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index)); |
| 5555 | } |
| 5556 | |
| 5557 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5558 | |
| 5559 | |
| 5560 | if (!isFunctionOrMethod(D)) { |
| 5561 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5562 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5563 | return; |
| 5564 | } |
| 5565 | |
| 5566 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5567 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5568 | << 1 << 0; |
| 5569 | return; |
| 5570 | } |
| 5571 | |
| 5572 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5573 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5574 | << 1 << 1; |
| 5575 | return; |
| 5576 | } |
| 5577 | |
| 5578 | |
| 5579 | if (!checkAttributeNumArgs(S, AL, 1)) |
| 5580 | return; |
| 5581 | |
| 5582 | if (!AL.isArgExpr(0)) { |
| 5583 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 5584 | << AL << AANT_ArgumentIntegerConstant; |
| 5585 | return; |
| 5586 | } |
| 5587 | |
| 5588 | Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
| 5589 | llvm::APSInt NumParams(32); |
| 5590 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 5591 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 5592 | << AL << AANT_ArgumentIntegerConstant |
| 5593 | << NumParamsExpr->getSourceRange(); |
| 5594 | return; |
| 5595 | } |
| 5596 | |
| 5597 | unsigned Num = NumParams.getLimitedValue(255); |
| 5598 | if (Num > 63) { |
| 5599 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5600 | << AL << (int)NumParams.getSExtValue() |
| 5601 | << NumParamsExpr->getSourceRange(); |
| 5602 | return; |
| 5603 | } |
| 5604 | |
| 5605 | D->addAttr(::new (S.Context) |
| 5606 | MSP430InterruptAttr(AL.getLoc(), S.Context, Num, |
| 5607 | AL.getAttributeSpellingListIndex())); |
| 5608 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5609 | } |
| 5610 | |
| 5611 | static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5612 | |
| 5613 | if (AL.getNumArgs() > 1) { |
| 5614 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
| 5615 | return; |
| 5616 | } |
| 5617 | |
| 5618 | StringRef Str; |
| 5619 | SourceLocation ArgLoc; |
| 5620 | |
| 5621 | if (AL.getNumArgs() == 0) |
| 5622 | Str = ""; |
| 5623 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5624 | return; |
| 5625 | |
| 5626 | |
| 5627 | |
| 5628 | |
| 5629 | |
| 5630 | |
| 5631 | |
| 5632 | |
| 5633 | |
| 5634 | |
| 5635 | if (!isFunctionOrMethod(D)) { |
| 5636 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5637 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5638 | return; |
| 5639 | } |
| 5640 | |
| 5641 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5642 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5643 | << 0 << 0; |
| 5644 | return; |
| 5645 | } |
| 5646 | |
| 5647 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5648 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5649 | << 0 << 1; |
| 5650 | return; |
| 5651 | } |
| 5652 | |
| 5653 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL)) |
| 5654 | return; |
| 5655 | |
| 5656 | MipsInterruptAttr::InterruptType Kind; |
| 5657 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5658 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
| 5659 | << AL << "'" + std::string(Str) + "'"; |
| 5660 | return; |
| 5661 | } |
| 5662 | |
| 5663 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 5664 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
| 5665 | } |
| 5666 | |
| 5667 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5668 | |
| 5669 | |
| 5670 | |
| 5671 | |
| 5672 | |
| 5673 | |
| 5674 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5675 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5676 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
| 5677 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5678 | << AL << ExpectedFunctionWithProtoType; |
| 5679 | return; |
| 5680 | } |
| 5681 | |
| 5682 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5683 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5684 | diag::err_anyx86_interrupt_attribute) |
| 5685 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5686 | ? 0 |
| 5687 | : 1) |
| 5688 | << 0; |
| 5689 | return; |
| 5690 | } |
| 5691 | |
| 5692 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5693 | if (NumParams < 1 || NumParams > 2) { |
| 5694 | S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute) |
| 5695 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5696 | ? 0 |
| 5697 | : 1) |
| 5698 | << 1; |
| 5699 | return; |
| 5700 | } |
| 5701 | |
| 5702 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5703 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5704 | diag::err_anyx86_interrupt_attribute) |
| 5705 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5706 | ? 0 |
| 5707 | : 1) |
| 5708 | << 2; |
| 5709 | return; |
| 5710 | } |
| 5711 | |
| 5712 | unsigned TypeSize = |
| 5713 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5714 | ? 64 |
| 5715 | : 32; |
| 5716 | if (NumParams == 2 && |
| 5717 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5718 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5719 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5720 | diag::err_anyx86_interrupt_attribute) |
| 5721 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5722 | ? 0 |
| 5723 | : 1) |
| 5724 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, ); |
| 5725 | return; |
| 5726 | } |
| 5727 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
| 5728 | AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex())); |
| 5729 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5730 | } |
| 5731 | |
| 5732 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5733 | if (!isFunctionOrMethod(D)) { |
| 5734 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5735 | << "'interrupt'" << ExpectedFunction; |
| 5736 | return; |
| 5737 | } |
| 5738 | |
| 5739 | if (!checkAttributeNumArgs(S, AL, 0)) |
| 5740 | return; |
| 5741 | |
| 5742 | handleSimpleAttribute<AVRInterruptAttr>(S, D, AL); |
| 5743 | } |
| 5744 | |
| 5745 | static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5746 | if (!isFunctionOrMethod(D)) { |
| 5747 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5748 | << "'signal'" << ExpectedFunction; |
| 5749 | return; |
| 5750 | } |
| 5751 | |
| 5752 | if (!checkAttributeNumArgs(S, AL, 0)) |
| 5753 | return; |
| 5754 | |
| 5755 | handleSimpleAttribute<AVRSignalAttr>(S, D, AL); |
| 5756 | } |
| 5757 | |
| 5758 | static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5759 | if (!isFunctionOrMethod(D)) { |
| 5760 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5761 | << "'import_module'" << ExpectedFunction; |
| 5762 | return; |
| 5763 | } |
| 5764 | |
| 5765 | auto *FD = cast<FunctionDecl>(D); |
| 5766 | if (FD->isThisDeclarationADefinition()) { |
| 5767 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5768 | return; |
| 5769 | } |
| 5770 | |
| 5771 | StringRef Str; |
| 5772 | SourceLocation ArgLoc; |
| 5773 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5774 | return; |
| 5775 | |
| 5776 | FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr( |
| 5777 | AL.getRange(), S.Context, Str, |
| 5778 | AL.getAttributeSpellingListIndex())); |
| 5779 | } |
| 5780 | |
| 5781 | static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5782 | if (!isFunctionOrMethod(D)) { |
| 5783 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5784 | << "'import_name'" << ExpectedFunction; |
| 5785 | return; |
| 5786 | } |
| 5787 | |
| 5788 | auto *FD = cast<FunctionDecl>(D); |
| 5789 | if (FD->isThisDeclarationADefinition()) { |
| 5790 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5791 | return; |
| 5792 | } |
| 5793 | |
| 5794 | StringRef Str; |
| 5795 | SourceLocation ArgLoc; |
| 5796 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5797 | return; |
| 5798 | |
| 5799 | FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr( |
| 5800 | AL.getRange(), S.Context, Str, |
| 5801 | AL.getAttributeSpellingListIndex())); |
| 5802 | } |
| 5803 | |
| 5804 | static void handleRISCVInterruptAttr(Sema &S, Decl *D, |
| 5805 | const ParsedAttr &AL) { |
| 5806 | |
| 5807 | if (const auto *A = D->getAttr<RISCVInterruptAttr>()) { |
| 5808 | S.Diag(AL.getRange().getBegin(), |
| 5809 | diag::warn_riscv_repeated_interrupt_attribute); |
| 5810 | S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute); |
| 5811 | return; |
| 5812 | } |
| 5813 | |
| 5814 | |
| 5815 | if (!checkAttributeAtMostNumArgs(S, AL, 1)) |
| 5816 | return; |
| 5817 | |
| 5818 | StringRef Str; |
| 5819 | SourceLocation ArgLoc; |
| 5820 | |
| 5821 | |
| 5822 | if (AL.getNumArgs() == 0) |
| 5823 | Str = "machine"; |
| 5824 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5825 | return; |
| 5826 | |
| 5827 | |
| 5828 | |
| 5829 | |
| 5830 | |
| 5831 | |
| 5832 | |
| 5833 | |
| 5834 | if (D->getFunctionType() == nullptr) { |
| 5835 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5836 | << "'interrupt'" << ExpectedFunction; |
| 5837 | return; |
| 5838 | } |
| 5839 | |
| 5840 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5841 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5842 | << 2 << 0; |
| 5843 | return; |
| 5844 | } |
| 5845 | |
| 5846 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5847 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5848 | << 2 << 1; |
| 5849 | return; |
| 5850 | } |
| 5851 | |
| 5852 | RISCVInterruptAttr::InterruptType Kind; |
| 5853 | if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5854 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5855 | << ArgLoc; |
| 5856 | return; |
| 5857 | } |
| 5858 | |
| 5859 | D->addAttr(::new (S.Context) RISCVInterruptAttr( |
| 5860 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
| 5861 | } |
| 5862 | |
| 5863 | static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5864 | |
| 5865 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5866 | case llvm::Triple::msp430: |
| 5867 | handleMSP430InterruptAttr(S, D, AL); |
| 5868 | break; |
| 5869 | case llvm::Triple::mipsel: |
| 5870 | case llvm::Triple::mips: |
| 5871 | handleMipsInterruptAttr(S, D, AL); |
| 5872 | break; |
| 5873 | case llvm::Triple::x86: |
| 5874 | case llvm::Triple::x86_64: |
| 5875 | handleAnyX86InterruptAttr(S, D, AL); |
| 5876 | break; |
| 5877 | case llvm::Triple::avr: |
| 5878 | handleAVRInterruptAttr(S, D, AL); |
| 5879 | break; |
| 5880 | case llvm::Triple::riscv32: |
| 5881 | case llvm::Triple::riscv64: |
| 5882 | handleRISCVInterruptAttr(S, D, AL); |
| 5883 | break; |
| 5884 | default: |
| 5885 | handleARMInterruptAttr(S, D, AL); |
| 5886 | break; |
| 5887 | } |
| 5888 | } |
| 5889 | |
| 5890 | static bool |
| 5891 | checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, |
| 5892 | const AMDGPUFlatWorkGroupSizeAttr &Attr) { |
| 5893 | |
| 5894 | |
| 5895 | if (MinExpr->isValueDependent() || MaxExpr->isValueDependent()) |
| 5896 | return false; |
| 5897 | |
| 5898 | uint32_t Min = 0; |
| 5899 | if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0)) |
| 5900 | return true; |
| 5901 | |
| 5902 | uint32_t Max = 0; |
| 5903 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1)) |
| 5904 | return true; |
| 5905 | |
| 5906 | if (Min == 0 && Max != 0) { |
| 5907 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5908 | << &Attr << 0; |
| 5909 | return true; |
| 5910 | } |
| 5911 | if (Min > Max) { |
| 5912 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5913 | << &Attr << 1; |
| 5914 | return true; |
| 5915 | } |
| 5916 | |
| 5917 | return false; |
| 5918 | } |
| 5919 | |
| 5920 | void Sema::addAMDGPUFlatWorkGroupSizeAttr(SourceRange AttrRange, Decl *D, |
| 5921 | Expr *MinExpr, Expr *MaxExpr, |
| 5922 | unsigned SpellingListIndex) { |
| 5923 | AMDGPUFlatWorkGroupSizeAttr TmpAttr(AttrRange, Context, MinExpr, MaxExpr, |
| 5924 | SpellingListIndex); |
| 5925 | |
| 5926 | if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr)) |
| 5927 | return; |
| 5928 | |
| 5929 | D->addAttr(::new (Context) AMDGPUFlatWorkGroupSizeAttr( |
| 5930 | AttrRange, Context, MinExpr, MaxExpr, SpellingListIndex)); |
| 5931 | } |
| 5932 | |
| 5933 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 5934 | const ParsedAttr &AL) { |
| 5935 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5936 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 5937 | |
| 5938 | S.addAMDGPUFlatWorkGroupSizeAttr(AL.getRange(), D, MinExpr, MaxExpr, |
| 5939 | AL.getAttributeSpellingListIndex()); |
| 5940 | } |
| 5941 | |
| 5942 | static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr, |
| 5943 | Expr *MaxExpr, |
| 5944 | const AMDGPUWavesPerEUAttr &Attr) { |
| 5945 | if (S.DiagnoseUnexpandedParameterPack(MinExpr) || |
| 5946 | (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr))) |
| 5947 | return true; |
| 5948 | |
| 5949 | |
| 5950 | |
| 5951 | if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent())) |
| 5952 | return false; |
| 5953 | |
| 5954 | uint32_t Min = 0; |
| 5955 | if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0)) |
| 5956 | return true; |
| 5957 | |
| 5958 | uint32_t Max = 0; |
| 5959 | if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1)) |
| 5960 | return true; |
| 5961 | |
| 5962 | if (Min == 0 && Max != 0) { |
| 5963 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5964 | << &Attr << 0; |
| 5965 | return true; |
| 5966 | } |
| 5967 | if (Max != 0 && Min > Max) { |
| 5968 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5969 | << &Attr << 1; |
| 5970 | return true; |
| 5971 | } |
| 5972 | |
| 5973 | return false; |
| 5974 | } |
| 5975 | |
| 5976 | void Sema::addAMDGPUWavesPerEUAttr(SourceRange AttrRange, Decl *D, |
| 5977 | Expr *MinExpr, Expr *MaxExpr, |
| 5978 | unsigned SpellingListIndex) { |
| 5979 | AMDGPUWavesPerEUAttr TmpAttr(AttrRange, Context, MinExpr, MaxExpr, |
| 5980 | SpellingListIndex); |
| 5981 | |
| 5982 | if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr)) |
| 5983 | return; |
| 5984 | |
| 5985 | D->addAttr(::new (Context) AMDGPUWavesPerEUAttr(AttrRange, Context, MinExpr, |
| 5986 | MaxExpr, SpellingListIndex)); |
| 5987 | } |
| 5988 | |
| 5989 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5990 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 5991 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
| 5992 | return; |
| 5993 | |
| 5994 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5995 | Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr; |
| 5996 | |
| 5997 | S.addAMDGPUWavesPerEUAttr(AL.getRange(), D, MinExpr, MaxExpr, |
| 5998 | AL.getAttributeSpellingListIndex()); |
| 5999 | } |
| 6000 | |
| 6001 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6002 | uint32_t NumSGPR = 0; |
| 6003 | Expr *NumSGPRExpr = AL.getArgAsExpr(0); |
| 6004 | if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR)) |
| 6005 | return; |
| 6006 | |
| 6007 | D->addAttr(::new (S.Context) |
| 6008 | AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR, |
| 6009 | AL.getAttributeSpellingListIndex())); |
| 6010 | } |
| 6011 | |
| 6012 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6013 | uint32_t NumVGPR = 0; |
| 6014 | Expr *NumVGPRExpr = AL.getArgAsExpr(0); |
| 6015 | if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR)) |
| 6016 | return; |
| 6017 | |
| 6018 | D->addAttr(::new (S.Context) |
| 6019 | AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR, |
| 6020 | AL.getAttributeSpellingListIndex())); |
| 6021 | } |
| 6022 | |
| 6023 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 6024 | const ParsedAttr &AL) { |
| 6025 | |
| 6026 | |
| 6027 | |
| 6028 | const auto *VD = dyn_cast<ValueDecl>(D); |
| 6029 | if (VD && VD->getType()->isFunctionPointerType()) |
| 6030 | return; |
| 6031 | |
| 6032 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
| 6033 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 6034 | TD->getUnderlyingType()->isFunctionType())) |
| 6035 | return; |
| 6036 | |
| 6037 | if (!isa<FunctionDecl>(D)) { |
| 6038 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 6039 | << AL << ExpectedFunction; |
| 6040 | return; |
| 6041 | } |
| 6042 | |
| 6043 | D->addAttr(::new (S.Context) |
| 6044 | X86ForceAlignArgPointerAttr(AL.getRange(), S.Context, |
| 6045 | AL.getAttributeSpellingListIndex())); |
| 6046 | } |
| 6047 | |
| 6048 | static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6049 | uint32_t Version; |
| 6050 | Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
| 6051 | if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version)) |
| 6052 | return; |
| 6053 | |
| 6054 | |
| 6055 | if (Version != LangOptions::MSVC2015 / 100) { |
| 6056 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 6057 | << AL << Version << VersionExpr->getSourceRange(); |
| 6058 | return; |
| 6059 | } |
| 6060 | |
| 6061 | |
| 6062 | |
| 6063 | |
| 6064 | Version *= 100; |
| 6065 | |
| 6066 | D->addAttr(::new (S.Context) |
| 6067 | LayoutVersionAttr(AL.getRange(), S.Context, Version, |
| 6068 | AL.getAttributeSpellingListIndex())); |
| 6069 | } |
| 6070 | |
| 6071 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 6072 | unsigned AttrSpellingListIndex) { |
| 6073 | if (D->hasAttr<DLLExportAttr>()) { |
| 6074 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
| 6075 | return nullptr; |
| 6076 | } |
| 6077 | |
| 6078 | if (D->hasAttr<DLLImportAttr>()) |
| 6079 | return nullptr; |
| 6080 | |
| 6081 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
| 6082 | } |
| 6083 | |
| 6084 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 6085 | unsigned AttrSpellingListIndex) { |
| 6086 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
| 6087 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
| 6088 | D->dropAttr<DLLImportAttr>(); |
| 6089 | } |
| 6090 | |
| 6091 | if (D->hasAttr<DLLExportAttr>()) |
| 6092 | return nullptr; |
| 6093 | |
| 6094 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
| 6095 | } |
| 6096 | |
| 6097 | static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
| 6098 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 6099 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 6100 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A; |
| 6101 | return; |
| 6102 | } |
| 6103 | |
| 6104 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 6105 | if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport && |
| 6106 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 6107 | |
| 6108 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 6109 | << A; |
| 6110 | return; |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 6115 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 6116 | MD->getParent()->isLambda()) { |
| 6117 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A; |
| 6118 | return; |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 6123 | Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport |
| 6124 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 6125 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
| 6126 | if (NewAttr) |
| 6127 | D->addAttr(NewAttr); |
| 6128 | } |
| 6129 | |
| 6130 | MSInheritanceAttr * |
| 6131 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
| 6132 | unsigned AttrSpellingListIndex, |
| 6133 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 6134 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 6135 | if (IA->getSemanticSpelling() == SemanticSpelling) |
| 6136 | return nullptr; |
| 6137 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 6138 | << 1 ; |
| 6139 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 6140 | D->dropAttr<MSInheritanceAttr>(); |
| 6141 | } |
| 6142 | |
| 6143 | auto *RD = cast<CXXRecordDecl>(D); |
| 6144 | if (RD->hasDefinition()) { |
| 6145 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 6146 | SemanticSpelling)) { |
| 6147 | return nullptr; |
| 6148 | } |
| 6149 | } else { |
| 6150 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 6151 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 6152 | << 1 ; |
| 6153 | return nullptr; |
| 6154 | } |
| 6155 | if (RD->getDescribedClassTemplate()) { |
| 6156 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 6157 | << 0 ; |
| 6158 | return nullptr; |
| 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | return ::new (Context) |
| 6163 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
| 6164 | } |
| 6165 | |
| 6166 | static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6167 | |
| 6168 | |
| 6169 | |
| 6170 | |
| 6171 | |
| 6172 | |
| 6173 | |
| 6174 | |
| 6175 | StringRef N("mutex"); |
| 6176 | SourceLocation LiteralLoc; |
| 6177 | if (AL.getKind() == ParsedAttr::AT_Capability && |
| 6178 | !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc)) |
| 6179 | return; |
| 6180 | |
| 6181 | |
| 6182 | |
| 6183 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 6184 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 6185 | |
| 6186 | D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N, |
| 6187 | AL.getAttributeSpellingListIndex())); |
| 6188 | } |
| 6189 | |
| 6190 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6191 | SmallVector<Expr*, 1> Args; |
| 6192 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
| 6193 | return; |
| 6194 | |
| 6195 | D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context, |
| 6196 | Args.data(), Args.size(), |
| 6197 | AL.getAttributeSpellingListIndex())); |
| 6198 | } |
| 6199 | |
| 6200 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 6201 | const ParsedAttr &AL) { |
| 6202 | SmallVector<Expr*, 1> Args; |
| 6203 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
| 6204 | return; |
| 6205 | |
| 6206 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(), |
| 6207 | S.Context, |
| 6208 | Args.data(), Args.size(), |
| 6209 | AL.getAttributeSpellingListIndex())); |
| 6210 | } |
| 6211 | |
| 6212 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 6213 | const ParsedAttr &AL) { |
| 6214 | SmallVector<Expr*, 2> Args; |
| 6215 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
| 6216 | return; |
| 6217 | |
| 6218 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(), |
| 6219 | S.Context, |
| 6220 | AL.getArgAsExpr(0), |
| 6221 | Args.data(), |
| 6222 | Args.size(), |
| 6223 | AL.getAttributeSpellingListIndex())); |
| 6224 | } |
| 6225 | |
| 6226 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 6227 | const ParsedAttr &AL) { |
| 6228 | |
| 6229 | SmallVector<Expr *, 1> Args; |
| 6230 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true); |
| 6231 | |
| 6232 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 6233 | AL.getRange(), S.Context, Args.data(), Args.size(), |
| 6234 | AL.getAttributeSpellingListIndex())); |
| 6235 | } |
| 6236 | |
| 6237 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 6238 | const ParsedAttr &AL) { |
| 6239 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 6240 | return; |
| 6241 | |
| 6242 | |
| 6243 | SmallVector<Expr*, 1> Args; |
| 6244 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
| 6245 | if (Args.empty()) |
| 6246 | return; |
| 6247 | |
| 6248 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 6249 | RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(), |
| 6250 | Args.size(), AL.getAttributeSpellingListIndex()); |
| 6251 | |
| 6252 | D->addAttr(RCA); |
| 6253 | } |
| 6254 | |
| 6255 | static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6256 | if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 6257 | if (NSD->isAnonymousNamespace()) { |
| 6258 | S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 6259 | |
| 6260 | |
| 6261 | |
| 6262 | return; |
| 6263 | } |
| 6264 | } |
| 6265 | |
| 6266 | |
| 6267 | StringRef Str, Replacement; |
| 6268 | if (AL.isArgExpr(0) && AL.getArgAsExpr(0) && |
| 6269 | !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
| 6270 | return; |
| 6271 | |
| 6272 | |
| 6273 | if (AL.isDeclspecAttribute() || AL.isCXX11Attribute()) |
| 6274 | checkAttributeAtMostNumArgs(S, AL, 1); |
| 6275 | else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) && |
| 6276 | !S.checkStringLiteralArgumentAttr(AL, 1, Replacement)) |
| 6277 | return; |
| 6278 | |
| 6279 | if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope()) |
| 6280 | S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL; |
| 6281 | |
| 6282 | D->addAttr(::new (S.Context) |
| 6283 | DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement, |
| 6284 | AL.getAttributeSpellingListIndex())); |
| 6285 | } |
| 6286 | |
| 6287 | static bool isGlobalVar(const Decl *D) { |
| 6288 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 6289 | return S->hasGlobalStorage(); |
| 6290 | return false; |
| 6291 | } |
| 6292 | |
| 6293 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6294 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 6295 | return; |
| 6296 | |
| 6297 | std::vector<StringRef> Sanitizers; |
| 6298 | |
| 6299 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
| 6300 | StringRef SanitizerName; |
| 6301 | SourceLocation LiteralLoc; |
| 6302 | |
| 6303 | if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc)) |
| 6304 | return; |
| 6305 | |
| 6306 | if (parseSanitizerValue(SanitizerName, ) == |
| 6307 | SanitizerMask()) |
| 6308 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
| 6309 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 6310 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6311 | << AL << ExpectedFunctionOrMethod; |
| 6312 | Sanitizers.push_back(SanitizerName); |
| 6313 | } |
| 6314 | |
| 6315 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 6316 | AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 6317 | AL.getAttributeSpellingListIndex())); |
| 6318 | } |
| 6319 | |
| 6320 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 6321 | const ParsedAttr &AL) { |
| 6322 | StringRef AttrName = AL.getName()->getName(); |
| 6323 | normalizeName(AttrName); |
| 6324 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 6325 | .Case("no_address_safety_analysis", "address") |
| 6326 | .Case("no_sanitize_address", "address") |
| 6327 | .Case("no_sanitize_thread", "thread") |
| 6328 | .Case("no_sanitize_memory", "memory"); |
| 6329 | if (isGlobalVar(D) && SanitizerName != "address") |
| 6330 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6331 | << AL << ExpectedFunction; |
| 6332 | D->addAttr(::new (S.Context) |
| 6333 | NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1, |
| 6334 | AL.getAttributeSpellingListIndex())); |
| 6335 | } |
| 6336 | |
| 6337 | static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6338 | if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL)) |
| 6339 | D->addAttr(Internal); |
| 6340 | } |
| 6341 | |
| 6342 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6343 | if (S.LangOpts.OpenCLVersion != 200) |
| 6344 | S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version) |
| 6345 | << AL << "2.0" << 0; |
| 6346 | else |
| 6347 | S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL |
| 6348 | << "2.0"; |
| 6349 | } |
| 6350 | |
| 6351 | |
| 6352 | |
| 6353 | |
| 6354 | static bool handleCommonAttributeFeatures(Sema &S, Decl *D, |
| 6355 | const ParsedAttr &AL) { |
| 6356 | |
| 6357 | |
| 6358 | |
| 6359 | |
| 6360 | |
| 6361 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
| 6362 | return false; |
| 6363 | |
| 6364 | |
| 6365 | if (!AL.diagnoseLangOpts(S)) |
| 6366 | return true; |
| 6367 | |
| 6368 | if (!AL.diagnoseAppertainsTo(S, D)) |
| 6369 | return true; |
| 6370 | if (AL.hasCustomParsing()) |
| 6371 | return false; |
| 6372 | |
| 6373 | if (AL.getMinArgs() == AL.getMaxArgs()) { |
| 6374 | |
| 6375 | |
| 6376 | if (!checkAttributeNumArgs(S, AL, AL.getMinArgs())) |
| 6377 | return true; |
| 6378 | } else { |
| 6379 | |
| 6380 | if (AL.getMinArgs() && |
| 6381 | !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs())) |
| 6382 | return true; |
| 6383 | else if (!AL.hasVariadicArg() && AL.getMaxArgs() && |
| 6384 | !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs())) |
| 6385 | return true; |
| 6386 | } |
| 6387 | |
| 6388 | if (S.CheckAttrTarget(AL)) |
| 6389 | return true; |
| 6390 | |
| 6391 | return false; |
| 6392 | } |
| 6393 | |
| 6394 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6395 | if (D->isInvalidDecl()) |
| 6396 | return; |
| 6397 | |
| 6398 | |
| 6399 | if (D->hasAttr<OpenCLAccessAttr>()) { |
| 6400 | if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() == |
| 6401 | AL.getSemanticSpelling()) { |
| 6402 | S.Diag(AL.getLoc(), diag::warn_duplicate_declspec) |
| 6403 | << AL.getName()->getName() << AL.getRange(); |
| 6404 | } else { |
| 6405 | S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 6406 | << D->getSourceRange(); |
| 6407 | D->setInvalidDecl(true); |
| 6408 | return; |
| 6409 | } |
| 6410 | } |
| 6411 | |
| 6412 | |
| 6413 | |
| 6414 | |
| 6415 | |
| 6416 | |
| 6417 | if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) { |
| 6418 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
| 6419 | if (AL.getName()->getName().find("read_write") != StringRef::npos) { |
| 6420 | if ((!S.getLangOpts().OpenCLCPlusPlus && |
| 6421 | S.getLangOpts().OpenCLVersion < 200) || |
| 6422 | DeclTy->isPipeType()) { |
| 6423 | S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write) |
| 6424 | << AL << PDecl->getType() << DeclTy->isImageType(); |
| 6425 | D->setInvalidDecl(true); |
| 6426 | return; |
| 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | |
| 6431 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
| 6432 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
| 6433 | } |
| 6434 | |
| 6435 | static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
| 6436 | if (!cast<VarDecl>(D)->hasGlobalStorage()) { |
| 6437 | S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var) |
| 6438 | << (A.getKind() == ParsedAttr::AT_AlwaysDestroy); |
| 6439 | return; |
| 6440 | } |
| 6441 | |
| 6442 | if (A.getKind() == ParsedAttr::AT_AlwaysDestroy) |
| 6443 | handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A); |
| 6444 | else |
| 6445 | handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A); |
| 6446 | } |
| 6447 | |
| 6448 | static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6449 | (0) . __assert_fail ("cast(D)->getStorageDuration() == SD_Automatic && \"uninitialized is only valid on automatic duration variables\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 6450, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic && |
| 6450 | (0) . __assert_fail ("cast(D)->getStorageDuration() == SD_Automatic && \"uninitialized is only valid on automatic duration variables\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 6450, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "uninitialized is only valid on automatic duration variables"); |
| 6451 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 6452 | D->addAttr(::new (S.Context) |
| 6453 | UninitializedAttr(AL.getLoc(), S.Context, Index)); |
| 6454 | } |
| 6455 | |
| 6456 | static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD, |
| 6457 | bool DiagnoseFailure) { |
| 6458 | QualType Ty = VD->getType(); |
| 6459 | if (!Ty->isObjCRetainableType()) { |
| 6460 | if (DiagnoseFailure) { |
| 6461 | S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6462 | << 0; |
| 6463 | } |
| 6464 | return false; |
| 6465 | } |
| 6466 | |
| 6467 | Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime(); |
| 6468 | |
| 6469 | |
| 6470 | |
| 6471 | |
| 6472 | if (LifetimeQual == Qualifiers::OCL_None) |
| 6473 | LifetimeQual = Ty->getObjCARCImplicitLifetime(); |
| 6474 | |
| 6475 | |
| 6476 | |
| 6477 | if (LifetimeQual != Qualifiers::OCL_Strong) { |
| 6478 | if (DiagnoseFailure) { |
| 6479 | S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6480 | << 1; |
| 6481 | } |
| 6482 | return false; |
| 6483 | } |
| 6484 | |
| 6485 | |
| 6486 | |
| 6487 | |
| 6488 | VD->setType(Ty.withConst()); |
| 6489 | VD->setARCPseudoStrong(true); |
| 6490 | return true; |
| 6491 | } |
| 6492 | |
| 6493 | static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D, |
| 6494 | const ParsedAttr &AL) { |
| 6495 | if (auto *VD = dyn_cast<VarDecl>(D)) { |
| 6496 | (0) . __assert_fail ("!isa(VD) && \"should be diagnosed automatically\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 6496, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically"); |
| 6497 | if (!VD->hasLocalStorage()) { |
| 6498 | S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6499 | << 0; |
| 6500 | return; |
| 6501 | } |
| 6502 | |
| 6503 | if (!tryMakeVariablePseudoStrong(S, VD, )) |
| 6504 | return; |
| 6505 | |
| 6506 | handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL); |
| 6507 | return; |
| 6508 | } |
| 6509 | |
| 6510 | |
| 6511 | |
| 6512 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) { |
| 6513 | auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I)); |
| 6514 | QualType Ty = PVD->getType(); |
| 6515 | |
| 6516 | |
| 6517 | |
| 6518 | |
| 6519 | |
| 6520 | if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() == |
| 6521 | Qualifiers::OCL_Strong) |
| 6522 | continue; |
| 6523 | |
| 6524 | tryMakeVariablePseudoStrong(S, PVD, ); |
| 6525 | } |
| 6526 | handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL); |
| 6527 | } |
| 6528 | |
| 6529 | static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6530 | |
| 6531 | |
| 6532 | |
| 6533 | |
| 6534 | if (!isa<BlockDecl>(D)) { |
| 6535 | QualType T = getFunctionOrMethodResultType(D); |
| 6536 | bool IsKernReturnT = false; |
| 6537 | while (const auto *TT = T->getAs<TypedefType>()) { |
| 6538 | IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t"); |
| 6539 | T = TT->desugar(); |
| 6540 | } |
| 6541 | if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) { |
| 6542 | S.Diag(D->getBeginLoc(), |
| 6543 | diag::warn_mig_server_routine_does_not_return_kern_return_t); |
| 6544 | return; |
| 6545 | } |
| 6546 | } |
| 6547 | |
| 6548 | handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL); |
| 6549 | } |
| 6550 | |
| 6551 | static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6552 | |
| 6553 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 6554 | QualType RetTy = FD->getReturnType(); |
| 6555 | if (!RetTy->isPointerType() && !RetTy->isReferenceType()) { |
| 6556 | S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer) |
| 6557 | << AL.getRange() << RetTy; |
| 6558 | return; |
| 6559 | } |
| 6560 | } |
| 6561 | |
| 6562 | handleSimpleAttribute<MSAllocatorAttr>(S, D, AL); |
| 6563 | } |
| 6564 | |
| 6565 | |
| 6566 | |
| 6567 | |
| 6568 | |
| 6569 | |
| 6570 | |
| 6571 | |
| 6572 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 6573 | const ParsedAttr &AL, |
| 6574 | bool IncludeCXX11Attributes) { |
| 6575 | if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute) |
| 6576 | return; |
| 6577 | |
| 6578 | |
| 6579 | |
| 6580 | if (AL.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 6581 | return; |
| 6582 | |
| 6583 | |
| 6584 | |
| 6585 | |
| 6586 | if (AL.getKind() == ParsedAttr::UnknownAttribute || |
| 6587 | !AL.existsInTarget(S.Context.getTargetInfo())) { |
| 6588 | S.Diag(AL.getLoc(), |
| 6589 | AL.isDeclspecAttribute() |
| 6590 | ? (unsigned)diag::warn_unhandled_ms_attribute_ignored |
| 6591 | : (unsigned)diag::warn_unknown_attribute_ignored) |
| 6592 | << AL; |
| 6593 | return; |
| 6594 | } |
| 6595 | |
| 6596 | if (handleCommonAttributeFeatures(S, D, AL)) |
| 6597 | return; |
| 6598 | |
| 6599 | switch (AL.getKind()) { |
| 6600 | default: |
| 6601 | if (!AL.isStmtAttr()) { |
| 6602 | |
| 6603 | (0) . __assert_fail ("AL.isTypeAttr() && \"Non-type attribute not handled\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 6603, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AL.isTypeAttr() && "Non-type attribute not handled"); |
| 6604 | break; |
| 6605 | } |
| 6606 | S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
| 6607 | << AL << D->getLocation(); |
| 6608 | break; |
| 6609 | case ParsedAttr::AT_Interrupt: |
| 6610 | handleInterruptAttr(S, D, AL); |
| 6611 | break; |
| 6612 | case ParsedAttr::AT_X86ForceAlignArgPointer: |
| 6613 | handleX86ForceAlignArgPointerAttr(S, D, AL); |
| 6614 | break; |
| 6615 | case ParsedAttr::AT_DLLExport: |
| 6616 | case ParsedAttr::AT_DLLImport: |
| 6617 | handleDLLAttr(S, D, AL); |
| 6618 | break; |
| 6619 | case ParsedAttr::AT_Mips16: |
| 6620 | handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr, |
| 6621 | MipsInterruptAttr>(S, D, AL); |
| 6622 | break; |
| 6623 | case ParsedAttr::AT_NoMips16: |
| 6624 | handleSimpleAttribute<NoMips16Attr>(S, D, AL); |
| 6625 | break; |
| 6626 | case ParsedAttr::AT_MicroMips: |
| 6627 | handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL); |
| 6628 | break; |
| 6629 | case ParsedAttr::AT_NoMicroMips: |
| 6630 | handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL); |
| 6631 | break; |
| 6632 | case ParsedAttr::AT_MipsLongCall: |
| 6633 | handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>( |
| 6634 | S, D, AL); |
| 6635 | break; |
| 6636 | case ParsedAttr::AT_MipsShortCall: |
| 6637 | handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>( |
| 6638 | S, D, AL); |
| 6639 | break; |
| 6640 | case ParsedAttr::AT_AMDGPUFlatWorkGroupSize: |
| 6641 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL); |
| 6642 | break; |
| 6643 | case ParsedAttr::AT_AMDGPUWavesPerEU: |
| 6644 | handleAMDGPUWavesPerEUAttr(S, D, AL); |
| 6645 | break; |
| 6646 | case ParsedAttr::AT_AMDGPUNumSGPR: |
| 6647 | handleAMDGPUNumSGPRAttr(S, D, AL); |
| 6648 | break; |
| 6649 | case ParsedAttr::AT_AMDGPUNumVGPR: |
| 6650 | handleAMDGPUNumVGPRAttr(S, D, AL); |
| 6651 | break; |
| 6652 | case ParsedAttr::AT_AVRSignal: |
| 6653 | handleAVRSignalAttr(S, D, AL); |
| 6654 | break; |
| 6655 | case ParsedAttr::AT_WebAssemblyImportModule: |
| 6656 | handleWebAssemblyImportModuleAttr(S, D, AL); |
| 6657 | break; |
| 6658 | case ParsedAttr::AT_WebAssemblyImportName: |
| 6659 | handleWebAssemblyImportNameAttr(S, D, AL); |
| 6660 | break; |
| 6661 | case ParsedAttr::AT_IBAction: |
| 6662 | handleSimpleAttribute<IBActionAttr>(S, D, AL); |
| 6663 | break; |
| 6664 | case ParsedAttr::AT_IBOutlet: |
| 6665 | handleIBOutlet(S, D, AL); |
| 6666 | break; |
| 6667 | case ParsedAttr::AT_IBOutletCollection: |
| 6668 | handleIBOutletCollection(S, D, AL); |
| 6669 | break; |
| 6670 | case ParsedAttr::AT_IFunc: |
| 6671 | handleIFuncAttr(S, D, AL); |
| 6672 | break; |
| 6673 | case ParsedAttr::AT_Alias: |
| 6674 | handleAliasAttr(S, D, AL); |
| 6675 | break; |
| 6676 | case ParsedAttr::AT_Aligned: |
| 6677 | handleAlignedAttr(S, D, AL); |
| 6678 | break; |
| 6679 | case ParsedAttr::AT_AlignValue: |
| 6680 | handleAlignValueAttr(S, D, AL); |
| 6681 | break; |
| 6682 | case ParsedAttr::AT_AllocSize: |
| 6683 | handleAllocSizeAttr(S, D, AL); |
| 6684 | break; |
| 6685 | case ParsedAttr::AT_AlwaysInline: |
| 6686 | handleAlwaysInlineAttr(S, D, AL); |
| 6687 | break; |
| 6688 | case ParsedAttr::AT_Artificial: |
| 6689 | handleSimpleAttribute<ArtificialAttr>(S, D, AL); |
| 6690 | break; |
| 6691 | case ParsedAttr::AT_AnalyzerNoReturn: |
| 6692 | handleAnalyzerNoReturnAttr(S, D, AL); |
| 6693 | break; |
| 6694 | case ParsedAttr::AT_TLSModel: |
| 6695 | handleTLSModelAttr(S, D, AL); |
| 6696 | break; |
| 6697 | case ParsedAttr::AT_Annotate: |
| 6698 | handleAnnotateAttr(S, D, AL); |
| 6699 | break; |
| 6700 | case ParsedAttr::AT_Availability: |
| 6701 | handleAvailabilityAttr(S, D, AL); |
| 6702 | break; |
| 6703 | case ParsedAttr::AT_CarriesDependency: |
| 6704 | handleDependencyAttr(S, scope, D, AL); |
| 6705 | break; |
| 6706 | case ParsedAttr::AT_CPUDispatch: |
| 6707 | case ParsedAttr::AT_CPUSpecific: |
| 6708 | handleCPUSpecificAttr(S, D, AL); |
| 6709 | break; |
| 6710 | case ParsedAttr::AT_Common: |
| 6711 | handleCommonAttr(S, D, AL); |
| 6712 | break; |
| 6713 | case ParsedAttr::AT_CUDAConstant: |
| 6714 | handleConstantAttr(S, D, AL); |
| 6715 | break; |
| 6716 | case ParsedAttr::AT_PassObjectSize: |
| 6717 | handlePassObjectSizeAttr(S, D, AL); |
| 6718 | break; |
| 6719 | case ParsedAttr::AT_Constructor: |
| 6720 | handleConstructorAttr(S, D, AL); |
| 6721 | break; |
| 6722 | case ParsedAttr::AT_CXX11NoReturn: |
| 6723 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL); |
| 6724 | break; |
| 6725 | case ParsedAttr::AT_Deprecated: |
| 6726 | handleDeprecatedAttr(S, D, AL); |
| 6727 | break; |
| 6728 | case ParsedAttr::AT_Destructor: |
| 6729 | handleDestructorAttr(S, D, AL); |
| 6730 | break; |
| 6731 | case ParsedAttr::AT_EnableIf: |
| 6732 | handleEnableIfAttr(S, D, AL); |
| 6733 | break; |
| 6734 | case ParsedAttr::AT_DiagnoseIf: |
| 6735 | handleDiagnoseIfAttr(S, D, AL); |
| 6736 | break; |
| 6737 | case ParsedAttr::AT_ExtVectorType: |
| 6738 | handleExtVectorTypeAttr(S, D, AL); |
| 6739 | break; |
| 6740 | case ParsedAttr::AT_ExternalSourceSymbol: |
| 6741 | handleExternalSourceSymbolAttr(S, D, AL); |
| 6742 | break; |
| 6743 | case ParsedAttr::AT_MinSize: |
| 6744 | handleMinSizeAttr(S, D, AL); |
| 6745 | break; |
| 6746 | case ParsedAttr::AT_OptimizeNone: |
| 6747 | handleOptimizeNoneAttr(S, D, AL); |
| 6748 | break; |
| 6749 | case ParsedAttr::AT_FlagEnum: |
| 6750 | handleSimpleAttribute<FlagEnumAttr>(S, D, AL); |
| 6751 | break; |
| 6752 | case ParsedAttr::AT_EnumExtensibility: |
| 6753 | handleEnumExtensibilityAttr(S, D, AL); |
| 6754 | break; |
| 6755 | case ParsedAttr::AT_Flatten: |
| 6756 | handleSimpleAttribute<FlattenAttr>(S, D, AL); |
| 6757 | break; |
| 6758 | case ParsedAttr::AT_Format: |
| 6759 | handleFormatAttr(S, D, AL); |
| 6760 | break; |
| 6761 | case ParsedAttr::AT_FormatArg: |
| 6762 | handleFormatArgAttr(S, D, AL); |
| 6763 | break; |
| 6764 | case ParsedAttr::AT_Callback: |
| 6765 | handleCallbackAttr(S, D, AL); |
| 6766 | break; |
| 6767 | case ParsedAttr::AT_CUDAGlobal: |
| 6768 | handleGlobalAttr(S, D, AL); |
| 6769 | break; |
| 6770 | case ParsedAttr::AT_CUDADevice: |
| 6771 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
| 6772 | AL); |
| 6773 | break; |
| 6774 | case ParsedAttr::AT_CUDAHost: |
| 6775 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL); |
| 6776 | break; |
| 6777 | case ParsedAttr::AT_GNUInline: |
| 6778 | handleGNUInlineAttr(S, D, AL); |
| 6779 | break; |
| 6780 | case ParsedAttr::AT_CUDALaunchBounds: |
| 6781 | handleLaunchBoundsAttr(S, D, AL); |
| 6782 | break; |
| 6783 | case ParsedAttr::AT_Restrict: |
| 6784 | handleRestrictAttr(S, D, AL); |
| 6785 | break; |
| 6786 | case ParsedAttr::AT_LifetimeBound: |
| 6787 | handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL); |
| 6788 | break; |
| 6789 | case ParsedAttr::AT_MayAlias: |
| 6790 | handleSimpleAttribute<MayAliasAttr>(S, D, AL); |
| 6791 | break; |
| 6792 | case ParsedAttr::AT_Mode: |
| 6793 | handleModeAttr(S, D, AL); |
| 6794 | break; |
| 6795 | case ParsedAttr::AT_NoAlias: |
| 6796 | handleSimpleAttribute<NoAliasAttr>(S, D, AL); |
| 6797 | break; |
| 6798 | case ParsedAttr::AT_NoCommon: |
| 6799 | handleSimpleAttribute<NoCommonAttr>(S, D, AL); |
| 6800 | break; |
| 6801 | case ParsedAttr::AT_NoSplitStack: |
| 6802 | handleSimpleAttribute<NoSplitStackAttr>(S, D, AL); |
| 6803 | break; |
| 6804 | case ParsedAttr::AT_NonNull: |
| 6805 | if (auto *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6806 | handleNonNullAttrParameter(S, PVD, AL); |
| 6807 | else |
| 6808 | handleNonNullAttr(S, D, AL); |
| 6809 | break; |
| 6810 | case ParsedAttr::AT_ReturnsNonNull: |
| 6811 | handleReturnsNonNullAttr(S, D, AL); |
| 6812 | break; |
| 6813 | case ParsedAttr::AT_NoEscape: |
| 6814 | handleNoEscapeAttr(S, D, AL); |
| 6815 | break; |
| 6816 | case ParsedAttr::AT_AssumeAligned: |
| 6817 | handleAssumeAlignedAttr(S, D, AL); |
| 6818 | break; |
| 6819 | case ParsedAttr::AT_AllocAlign: |
| 6820 | handleAllocAlignAttr(S, D, AL); |
| 6821 | break; |
| 6822 | case ParsedAttr::AT_Overloadable: |
| 6823 | handleSimpleAttribute<OverloadableAttr>(S, D, AL); |
| 6824 | break; |
| 6825 | case ParsedAttr::AT_Ownership: |
| 6826 | handleOwnershipAttr(S, D, AL); |
| 6827 | break; |
| 6828 | case ParsedAttr::AT_Cold: |
| 6829 | handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL); |
| 6830 | break; |
| 6831 | case ParsedAttr::AT_Hot: |
| 6832 | handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL); |
| 6833 | break; |
| 6834 | case ParsedAttr::AT_Naked: |
| 6835 | handleNakedAttr(S, D, AL); |
| 6836 | break; |
| 6837 | case ParsedAttr::AT_NoReturn: |
| 6838 | handleNoReturnAttr(S, D, AL); |
| 6839 | break; |
| 6840 | case ParsedAttr::AT_AnyX86NoCfCheck: |
| 6841 | handleNoCfCheckAttr(S, D, AL); |
| 6842 | break; |
| 6843 | case ParsedAttr::AT_NoThrow: |
| 6844 | handleSimpleAttribute<NoThrowAttr>(S, D, AL); |
| 6845 | break; |
| 6846 | case ParsedAttr::AT_CUDAShared: |
| 6847 | handleSharedAttr(S, D, AL); |
| 6848 | break; |
| 6849 | case ParsedAttr::AT_VecReturn: |
| 6850 | handleVecReturnAttr(S, D, AL); |
| 6851 | break; |
| 6852 | case ParsedAttr::AT_ObjCOwnership: |
| 6853 | handleObjCOwnershipAttr(S, D, AL); |
| 6854 | break; |
| 6855 | case ParsedAttr::AT_ObjCPreciseLifetime: |
| 6856 | handleObjCPreciseLifetimeAttr(S, D, AL); |
| 6857 | break; |
| 6858 | case ParsedAttr::AT_ObjCReturnsInnerPointer: |
| 6859 | handleObjCReturnsInnerPointerAttr(S, D, AL); |
| 6860 | break; |
| 6861 | case ParsedAttr::AT_ObjCRequiresSuper: |
| 6862 | handleObjCRequiresSuperAttr(S, D, AL); |
| 6863 | break; |
| 6864 | case ParsedAttr::AT_ObjCBridge: |
| 6865 | handleObjCBridgeAttr(S, D, AL); |
| 6866 | break; |
| 6867 | case ParsedAttr::AT_ObjCBridgeMutable: |
| 6868 | handleObjCBridgeMutableAttr(S, D, AL); |
| 6869 | break; |
| 6870 | case ParsedAttr::AT_ObjCBridgeRelated: |
| 6871 | handleObjCBridgeRelatedAttr(S, D, AL); |
| 6872 | break; |
| 6873 | case ParsedAttr::AT_ObjCDesignatedInitializer: |
| 6874 | handleObjCDesignatedInitializer(S, D, AL); |
| 6875 | break; |
| 6876 | case ParsedAttr::AT_ObjCRuntimeName: |
| 6877 | handleObjCRuntimeName(S, D, AL); |
| 6878 | break; |
| 6879 | case ParsedAttr::AT_ObjCRuntimeVisible: |
| 6880 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL); |
| 6881 | break; |
| 6882 | case ParsedAttr::AT_ObjCBoxable: |
| 6883 | handleObjCBoxable(S, D, AL); |
| 6884 | break; |
| 6885 | case ParsedAttr::AT_CFAuditedTransfer: |
| 6886 | handleSimpleAttributeWithExclusions<CFAuditedTransferAttr, |
| 6887 | CFUnknownTransferAttr>(S, D, AL); |
| 6888 | break; |
| 6889 | case ParsedAttr::AT_CFUnknownTransfer: |
| 6890 | handleSimpleAttributeWithExclusions<CFUnknownTransferAttr, |
| 6891 | CFAuditedTransferAttr>(S, D, AL); |
| 6892 | break; |
| 6893 | case ParsedAttr::AT_CFConsumed: |
| 6894 | case ParsedAttr::AT_NSConsumed: |
| 6895 | case ParsedAttr::AT_OSConsumed: |
| 6896 | S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(), |
| 6897 | parsedAttrToRetainOwnershipKind(AL), |
| 6898 | ); |
| 6899 | break; |
| 6900 | case ParsedAttr::AT_NSConsumesSelf: |
| 6901 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL); |
| 6902 | break; |
| 6903 | case ParsedAttr::AT_OSConsumesThis: |
| 6904 | handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL); |
| 6905 | break; |
| 6906 | case ParsedAttr::AT_OSReturnsRetainedOnZero: |
| 6907 | handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>( |
| 6908 | S, D, AL, isValidOSObjectOutParameter(D), |
| 6909 | diag::warn_ns_attribute_wrong_parameter_type, |
| 6910 | AL, 3, AL.getRange()); |
| 6911 | break; |
| 6912 | case ParsedAttr::AT_OSReturnsRetainedOnNonZero: |
| 6913 | handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>( |
| 6914 | S, D, AL, isValidOSObjectOutParameter(D), |
| 6915 | diag::warn_ns_attribute_wrong_parameter_type, |
| 6916 | AL, 3, AL.getRange()); |
| 6917 | break; |
| 6918 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 6919 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 6920 | case ParsedAttr::AT_NSReturnsRetained: |
| 6921 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 6922 | case ParsedAttr::AT_CFReturnsRetained: |
| 6923 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 6924 | case ParsedAttr::AT_OSReturnsRetained: |
| 6925 | handleXReturnsXRetainedAttr(S, D, AL); |
| 6926 | break; |
| 6927 | case ParsedAttr::AT_WorkGroupSizeHint: |
| 6928 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL); |
| 6929 | break; |
| 6930 | case ParsedAttr::AT_ReqdWorkGroupSize: |
| 6931 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL); |
| 6932 | break; |
| 6933 | case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize: |
| 6934 | handleSubGroupSize(S, D, AL); |
| 6935 | break; |
| 6936 | case ParsedAttr::AT_VecTypeHint: |
| 6937 | handleVecTypeHint(S, D, AL); |
| 6938 | break; |
| 6939 | case ParsedAttr::AT_RequireConstantInit: |
| 6940 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL); |
| 6941 | break; |
| 6942 | case ParsedAttr::AT_InitPriority: |
| 6943 | handleInitPriorityAttr(S, D, AL); |
| 6944 | break; |
| 6945 | case ParsedAttr::AT_Packed: |
| 6946 | handlePackedAttr(S, D, AL); |
| 6947 | break; |
| 6948 | case ParsedAttr::AT_Section: |
| 6949 | handleSectionAttr(S, D, AL); |
| 6950 | break; |
| 6951 | case ParsedAttr::AT_SpeculativeLoadHardening: |
| 6952 | handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr, |
| 6953 | NoSpeculativeLoadHardeningAttr>(S, D, |
| 6954 | AL); |
| 6955 | break; |
| 6956 | case ParsedAttr::AT_NoSpeculativeLoadHardening: |
| 6957 | handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr, |
| 6958 | SpeculativeLoadHardeningAttr>(S, D, AL); |
| 6959 | break; |
| 6960 | case ParsedAttr::AT_CodeSeg: |
| 6961 | handleCodeSegAttr(S, D, AL); |
| 6962 | break; |
| 6963 | case ParsedAttr::AT_Target: |
| 6964 | handleTargetAttr(S, D, AL); |
| 6965 | break; |
| 6966 | case ParsedAttr::AT_MinVectorWidth: |
| 6967 | handleMinVectorWidthAttr(S, D, AL); |
| 6968 | break; |
| 6969 | case ParsedAttr::AT_Unavailable: |
| 6970 | handleAttrWithMessage<UnavailableAttr>(S, D, AL); |
| 6971 | break; |
| 6972 | case ParsedAttr::AT_ArcWeakrefUnavailable: |
| 6973 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL); |
| 6974 | break; |
| 6975 | case ParsedAttr::AT_ObjCRootClass: |
| 6976 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL); |
| 6977 | break; |
| 6978 | case ParsedAttr::AT_ObjCNonLazyClass: |
| 6979 | handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL); |
| 6980 | break; |
| 6981 | case ParsedAttr::AT_ObjCSubclassingRestricted: |
| 6982 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL); |
| 6983 | break; |
| 6984 | case ParsedAttr::AT_ObjCExplicitProtocolImpl: |
| 6985 | handleObjCSuppresProtocolAttr(S, D, AL); |
| 6986 | break; |
| 6987 | case ParsedAttr::AT_ObjCRequiresPropertyDefs: |
| 6988 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL); |
| 6989 | break; |
| 6990 | case ParsedAttr::AT_Unused: |
| 6991 | handleUnusedAttr(S, D, AL); |
| 6992 | break; |
| 6993 | case ParsedAttr::AT_ReturnsTwice: |
| 6994 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL); |
| 6995 | break; |
| 6996 | case ParsedAttr::AT_NotTailCalled: |
| 6997 | handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>( |
| 6998 | S, D, AL); |
| 6999 | break; |
| 7000 | case ParsedAttr::AT_DisableTailCalls: |
| 7001 | handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D, |
| 7002 | AL); |
| 7003 | break; |
| 7004 | case ParsedAttr::AT_Used: |
| 7005 | handleSimpleAttribute<UsedAttr>(S, D, AL); |
| 7006 | break; |
| 7007 | case ParsedAttr::AT_Visibility: |
| 7008 | handleVisibilityAttr(S, D, AL, false); |
| 7009 | break; |
| 7010 | case ParsedAttr::AT_TypeVisibility: |
| 7011 | handleVisibilityAttr(S, D, AL, true); |
| 7012 | break; |
| 7013 | case ParsedAttr::AT_WarnUnused: |
| 7014 | handleSimpleAttribute<WarnUnusedAttr>(S, D, AL); |
| 7015 | break; |
| 7016 | case ParsedAttr::AT_WarnUnusedResult: |
| 7017 | handleWarnUnusedResult(S, D, AL); |
| 7018 | break; |
| 7019 | case ParsedAttr::AT_Weak: |
| 7020 | handleSimpleAttribute<WeakAttr>(S, D, AL); |
| 7021 | break; |
| 7022 | case ParsedAttr::AT_WeakRef: |
| 7023 | handleWeakRefAttr(S, D, AL); |
| 7024 | break; |
| 7025 | case ParsedAttr::AT_WeakImport: |
| 7026 | handleWeakImportAttr(S, D, AL); |
| 7027 | break; |
| 7028 | case ParsedAttr::AT_TransparentUnion: |
| 7029 | handleTransparentUnionAttr(S, D, AL); |
| 7030 | break; |
| 7031 | case ParsedAttr::AT_ObjCException: |
| 7032 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL); |
| 7033 | break; |
| 7034 | case ParsedAttr::AT_ObjCMethodFamily: |
| 7035 | handleObjCMethodFamilyAttr(S, D, AL); |
| 7036 | break; |
| 7037 | case ParsedAttr::AT_ObjCNSObject: |
| 7038 | handleObjCNSObject(S, D, AL); |
| 7039 | break; |
| 7040 | case ParsedAttr::AT_ObjCIndependentClass: |
| 7041 | handleObjCIndependentClass(S, D, AL); |
| 7042 | break; |
| 7043 | case ParsedAttr::AT_Blocks: |
| 7044 | handleBlocksAttr(S, D, AL); |
| 7045 | break; |
| 7046 | case ParsedAttr::AT_Sentinel: |
| 7047 | handleSentinelAttr(S, D, AL); |
| 7048 | break; |
| 7049 | case ParsedAttr::AT_Const: |
| 7050 | handleSimpleAttribute<ConstAttr>(S, D, AL); |
| 7051 | break; |
| 7052 | case ParsedAttr::AT_Pure: |
| 7053 | handleSimpleAttribute<PureAttr>(S, D, AL); |
| 7054 | break; |
| 7055 | case ParsedAttr::AT_Cleanup: |
| 7056 | handleCleanupAttr(S, D, AL); |
| 7057 | break; |
| 7058 | case ParsedAttr::AT_NoDebug: |
| 7059 | handleNoDebugAttr(S, D, AL); |
| 7060 | break; |
| 7061 | case ParsedAttr::AT_NoDuplicate: |
| 7062 | handleSimpleAttribute<NoDuplicateAttr>(S, D, AL); |
| 7063 | break; |
| 7064 | case ParsedAttr::AT_Convergent: |
| 7065 | handleSimpleAttribute<ConvergentAttr>(S, D, AL); |
| 7066 | break; |
| 7067 | case ParsedAttr::AT_NoInline: |
| 7068 | handleSimpleAttribute<NoInlineAttr>(S, D, AL); |
| 7069 | break; |
| 7070 | case ParsedAttr::AT_NoInstrumentFunction: |
| 7071 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL); |
| 7072 | break; |
| 7073 | case ParsedAttr::AT_NoStackProtector: |
| 7074 | |
| 7075 | handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL); |
| 7076 | break; |
| 7077 | case ParsedAttr::AT_StdCall: |
| 7078 | case ParsedAttr::AT_CDecl: |
| 7079 | case ParsedAttr::AT_FastCall: |
| 7080 | case ParsedAttr::AT_ThisCall: |
| 7081 | case ParsedAttr::AT_Pascal: |
| 7082 | case ParsedAttr::AT_RegCall: |
| 7083 | case ParsedAttr::AT_SwiftCall: |
| 7084 | case ParsedAttr::AT_VectorCall: |
| 7085 | case ParsedAttr::AT_MSABI: |
| 7086 | case ParsedAttr::AT_SysVABI: |
| 7087 | case ParsedAttr::AT_Pcs: |
| 7088 | case ParsedAttr::AT_IntelOclBicc: |
| 7089 | case ParsedAttr::AT_PreserveMost: |
| 7090 | case ParsedAttr::AT_PreserveAll: |
| 7091 | case ParsedAttr::AT_AArch64VectorPcs: |
| 7092 | handleCallConvAttr(S, D, AL); |
| 7093 | break; |
| 7094 | case ParsedAttr::AT_Suppress: |
| 7095 | handleSuppressAttr(S, D, AL); |
| 7096 | break; |
| 7097 | case ParsedAttr::AT_OpenCLKernel: |
| 7098 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL); |
| 7099 | break; |
| 7100 | case ParsedAttr::AT_OpenCLAccess: |
| 7101 | handleOpenCLAccessAttr(S, D, AL); |
| 7102 | break; |
| 7103 | case ParsedAttr::AT_OpenCLNoSVM: |
| 7104 | handleOpenCLNoSVMAttr(S, D, AL); |
| 7105 | break; |
| 7106 | case ParsedAttr::AT_SwiftContext: |
| 7107 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext); |
| 7108 | break; |
| 7109 | case ParsedAttr::AT_SwiftErrorResult: |
| 7110 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult); |
| 7111 | break; |
| 7112 | case ParsedAttr::AT_SwiftIndirectResult: |
| 7113 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult); |
| 7114 | break; |
| 7115 | case ParsedAttr::AT_InternalLinkage: |
| 7116 | handleInternalLinkageAttr(S, D, AL); |
| 7117 | break; |
| 7118 | case ParsedAttr::AT_ExcludeFromExplicitInstantiation: |
| 7119 | handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL); |
| 7120 | break; |
| 7121 | case ParsedAttr::AT_LTOVisibilityPublic: |
| 7122 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL); |
| 7123 | break; |
| 7124 | |
| 7125 | |
| 7126 | case ParsedAttr::AT_EmptyBases: |
| 7127 | handleSimpleAttribute<EmptyBasesAttr>(S, D, AL); |
| 7128 | break; |
| 7129 | case ParsedAttr::AT_LayoutVersion: |
| 7130 | handleLayoutVersion(S, D, AL); |
| 7131 | break; |
| 7132 | case ParsedAttr::AT_TrivialABI: |
| 7133 | handleSimpleAttribute<TrivialABIAttr>(S, D, AL); |
| 7134 | break; |
| 7135 | case ParsedAttr::AT_MSNoVTable: |
| 7136 | handleSimpleAttribute<MSNoVTableAttr>(S, D, AL); |
| 7137 | break; |
| 7138 | case ParsedAttr::AT_MSStruct: |
| 7139 | handleSimpleAttribute<MSStructAttr>(S, D, AL); |
| 7140 | break; |
| 7141 | case ParsedAttr::AT_Uuid: |
| 7142 | handleUuidAttr(S, D, AL); |
| 7143 | break; |
| 7144 | case ParsedAttr::AT_MSInheritance: |
| 7145 | handleMSInheritanceAttr(S, D, AL); |
| 7146 | break; |
| 7147 | case ParsedAttr::AT_SelectAny: |
| 7148 | handleSimpleAttribute<SelectAnyAttr>(S, D, AL); |
| 7149 | break; |
| 7150 | case ParsedAttr::AT_Thread: |
| 7151 | handleDeclspecThreadAttr(S, D, AL); |
| 7152 | break; |
| 7153 | |
| 7154 | case ParsedAttr::AT_AbiTag: |
| 7155 | handleAbiTagAttr(S, D, AL); |
| 7156 | break; |
| 7157 | |
| 7158 | |
| 7159 | case ParsedAttr::AT_AssertExclusiveLock: |
| 7160 | handleAssertExclusiveLockAttr(S, D, AL); |
| 7161 | break; |
| 7162 | case ParsedAttr::AT_AssertSharedLock: |
| 7163 | handleAssertSharedLockAttr(S, D, AL); |
| 7164 | break; |
| 7165 | case ParsedAttr::AT_GuardedVar: |
| 7166 | handleSimpleAttribute<GuardedVarAttr>(S, D, AL); |
| 7167 | break; |
| 7168 | case ParsedAttr::AT_PtGuardedVar: |
| 7169 | handlePtGuardedVarAttr(S, D, AL); |
| 7170 | break; |
| 7171 | case ParsedAttr::AT_ScopedLockable: |
| 7172 | handleSimpleAttribute<ScopedLockableAttr>(S, D, AL); |
| 7173 | break; |
| 7174 | case ParsedAttr::AT_NoSanitize: |
| 7175 | handleNoSanitizeAttr(S, D, AL); |
| 7176 | break; |
| 7177 | case ParsedAttr::AT_NoSanitizeSpecific: |
| 7178 | handleNoSanitizeSpecificAttr(S, D, AL); |
| 7179 | break; |
| 7180 | case ParsedAttr::AT_NoThreadSafetyAnalysis: |
| 7181 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL); |
| 7182 | break; |
| 7183 | case ParsedAttr::AT_GuardedBy: |
| 7184 | handleGuardedByAttr(S, D, AL); |
| 7185 | break; |
| 7186 | case ParsedAttr::AT_PtGuardedBy: |
| 7187 | handlePtGuardedByAttr(S, D, AL); |
| 7188 | break; |
| 7189 | case ParsedAttr::AT_ExclusiveTrylockFunction: |
| 7190 | handleExclusiveTrylockFunctionAttr(S, D, AL); |
| 7191 | break; |
| 7192 | case ParsedAttr::AT_LockReturned: |
| 7193 | handleLockReturnedAttr(S, D, AL); |
| 7194 | break; |
| 7195 | case ParsedAttr::AT_LocksExcluded: |
| 7196 | handleLocksExcludedAttr(S, D, AL); |
| 7197 | break; |
| 7198 | case ParsedAttr::AT_SharedTrylockFunction: |
| 7199 | handleSharedTrylockFunctionAttr(S, D, AL); |
| 7200 | break; |
| 7201 | case ParsedAttr::AT_AcquiredBefore: |
| 7202 | handleAcquiredBeforeAttr(S, D, AL); |
| 7203 | break; |
| 7204 | case ParsedAttr::AT_AcquiredAfter: |
| 7205 | handleAcquiredAfterAttr(S, D, AL); |
| 7206 | break; |
| 7207 | |
| 7208 | |
| 7209 | case ParsedAttr::AT_Capability: |
| 7210 | case ParsedAttr::AT_Lockable: |
| 7211 | handleCapabilityAttr(S, D, AL); |
| 7212 | break; |
| 7213 | case ParsedAttr::AT_RequiresCapability: |
| 7214 | handleRequiresCapabilityAttr(S, D, AL); |
| 7215 | break; |
| 7216 | |
| 7217 | case ParsedAttr::AT_AssertCapability: |
| 7218 | handleAssertCapabilityAttr(S, D, AL); |
| 7219 | break; |
| 7220 | case ParsedAttr::AT_AcquireCapability: |
| 7221 | handleAcquireCapabilityAttr(S, D, AL); |
| 7222 | break; |
| 7223 | case ParsedAttr::AT_ReleaseCapability: |
| 7224 | handleReleaseCapabilityAttr(S, D, AL); |
| 7225 | break; |
| 7226 | case ParsedAttr::AT_TryAcquireCapability: |
| 7227 | handleTryAcquireCapabilityAttr(S, D, AL); |
| 7228 | break; |
| 7229 | |
| 7230 | |
| 7231 | case ParsedAttr::AT_Consumable: |
| 7232 | handleConsumableAttr(S, D, AL); |
| 7233 | break; |
| 7234 | case ParsedAttr::AT_ConsumableAutoCast: |
| 7235 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL); |
| 7236 | break; |
| 7237 | case ParsedAttr::AT_ConsumableSetOnRead: |
| 7238 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL); |
| 7239 | break; |
| 7240 | case ParsedAttr::AT_CallableWhen: |
| 7241 | handleCallableWhenAttr(S, D, AL); |
| 7242 | break; |
| 7243 | case ParsedAttr::AT_ParamTypestate: |
| 7244 | handleParamTypestateAttr(S, D, AL); |
| 7245 | break; |
| 7246 | case ParsedAttr::AT_ReturnTypestate: |
| 7247 | handleReturnTypestateAttr(S, D, AL); |
| 7248 | break; |
| 7249 | case ParsedAttr::AT_SetTypestate: |
| 7250 | handleSetTypestateAttr(S, D, AL); |
| 7251 | break; |
| 7252 | case ParsedAttr::AT_TestTypestate: |
| 7253 | handleTestTypestateAttr(S, D, AL); |
| 7254 | break; |
| 7255 | |
| 7256 | |
| 7257 | case ParsedAttr::AT_ArgumentWithTypeTag: |
| 7258 | handleArgumentWithTypeTagAttr(S, D, AL); |
| 7259 | break; |
| 7260 | case ParsedAttr::AT_TypeTagForDatatype: |
| 7261 | handleTypeTagForDatatypeAttr(S, D, AL); |
| 7262 | break; |
| 7263 | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: |
| 7264 | handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL); |
| 7265 | break; |
| 7266 | case ParsedAttr::AT_RenderScriptKernel: |
| 7267 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL); |
| 7268 | break; |
| 7269 | |
| 7270 | case ParsedAttr::AT_XRayInstrument: |
| 7271 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL); |
| 7272 | break; |
| 7273 | case ParsedAttr::AT_XRayLogArgs: |
| 7274 | handleXRayLogArgsAttr(S, D, AL); |
| 7275 | break; |
| 7276 | |
| 7277 | |
| 7278 | case ParsedAttr::AT_Reinitializes: |
| 7279 | handleSimpleAttribute<ReinitializesAttr>(S, D, AL); |
| 7280 | break; |
| 7281 | |
| 7282 | case ParsedAttr::AT_AlwaysDestroy: |
| 7283 | case ParsedAttr::AT_NoDestroy: |
| 7284 | handleDestroyAttr(S, D, AL); |
| 7285 | break; |
| 7286 | |
| 7287 | case ParsedAttr::AT_Uninitialized: |
| 7288 | handleUninitializedAttr(S, D, AL); |
| 7289 | break; |
| 7290 | |
| 7291 | case ParsedAttr::AT_ObjCExternallyRetained: |
| 7292 | handleObjCExternallyRetainedAttr(S, D, AL); |
| 7293 | break; |
| 7294 | |
| 7295 | case ParsedAttr::AT_MIGServerRoutine: |
| 7296 | handleMIGServerRoutineAttr(S, D, AL); |
| 7297 | break; |
| 7298 | |
| 7299 | case ParsedAttr::AT_MSAllocator: |
| 7300 | handleMSAllocatorAttr(S, D, AL); |
| 7301 | break; |
| 7302 | } |
| 7303 | } |
| 7304 | |
| 7305 | |
| 7306 | |
| 7307 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
| 7308 | const ParsedAttributesView &AttrList, |
| 7309 | bool IncludeCXX11Attributes) { |
| 7310 | if (AttrList.empty()) |
| 7311 | return; |
| 7312 | |
| 7313 | for (const ParsedAttr &AL : AttrList) |
| 7314 | ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes); |
| 7315 | |
| 7316 | |
| 7317 | |
| 7318 | |
| 7319 | |
| 7320 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
| 7321 | Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias) |
| 7322 | << cast<NamedDecl>(D); |
| 7323 | D->dropAttr<WeakRefAttr>(); |
| 7324 | return; |
| 7325 | } |
| 7326 | |
| 7327 | |
| 7328 | |
| 7329 | |
| 7330 | |
| 7331 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 7332 | |
| 7333 | if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 7334 | |
| 7335 | |
| 7336 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 7337 | D->setInvalidDecl(); |
| 7338 | } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 7339 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 7340 | D->setInvalidDecl(); |
| 7341 | } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) { |
| 7342 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 7343 | D->setInvalidDecl(); |
| 7344 | } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
| 7345 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 7346 | D->setInvalidDecl(); |
| 7347 | } else if (!D->hasAttr<CUDAGlobalAttr>()) { |
| 7348 | if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 7349 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7350 | << A << ExpectedKernelFunction; |
| 7351 | D->setInvalidDecl(); |
| 7352 | } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7353 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7354 | << A << ExpectedKernelFunction; |
| 7355 | D->setInvalidDecl(); |
| 7356 | } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 7357 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7358 | << A << ExpectedKernelFunction; |
| 7359 | D->setInvalidDecl(); |
| 7360 | } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7361 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7362 | << A << ExpectedKernelFunction; |
| 7363 | D->setInvalidDecl(); |
| 7364 | } |
| 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | |
| 7369 | |
| 7370 | |
| 7371 | |
| 7372 | |
| 7373 | if (D->hasAttr<ObjCDesignatedInitializerAttr>() && |
| 7374 | cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) { |
| 7375 | Diag(D->getLocation(), diag::err_designated_init_attr_non_init); |
| 7376 | D->dropAttr<ObjCDesignatedInitializerAttr>(); |
| 7377 | } |
| 7378 | } |
| 7379 | |
| 7380 | |
| 7381 | void Sema::ProcessDeclAttributeDelayed(Decl *D, |
| 7382 | const ParsedAttributesView &AttrList) { |
| 7383 | for (const ParsedAttr &AL : AttrList) |
| 7384 | if (AL.getKind() == ParsedAttr::AT_TransparentUnion) { |
| 7385 | handleTransparentUnionAttr(*this, D, AL); |
| 7386 | break; |
| 7387 | } |
| 7388 | } |
| 7389 | |
| 7390 | |
| 7391 | |
| 7392 | bool Sema::ProcessAccessDeclAttributeList( |
| 7393 | AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) { |
| 7394 | for (const ParsedAttr &AL : AttrList) { |
| 7395 | if (AL.getKind() == ParsedAttr::AT_Annotate) { |
| 7396 | ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute()); |
| 7397 | } else { |
| 7398 | Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec); |
| 7399 | return true; |
| 7400 | } |
| 7401 | } |
| 7402 | return false; |
| 7403 | } |
| 7404 | |
| 7405 | |
| 7406 | |
| 7407 | static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) { |
| 7408 | for (const ParsedAttr &AL : A) { |
| 7409 | |
| 7410 | if (AL.isUsedAsTypeAttr() || AL.isInvalid()) |
| 7411 | continue; |
| 7412 | if (AL.getKind() == ParsedAttr::IgnoredAttribute) |
| 7413 | continue; |
| 7414 | |
| 7415 | if (AL.getKind() == ParsedAttr::UnknownAttribute) { |
| 7416 | S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) |
| 7417 | << AL << AL.getRange(); |
| 7418 | } else { |
| 7419 | S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL |
| 7420 | << AL.getRange(); |
| 7421 | } |
| 7422 | } |
| 7423 | } |
| 7424 | |
| 7425 | |
| 7426 | |
| 7427 | |
| 7428 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 7429 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes()); |
| 7430 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 7431 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 7432 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 7433 | } |
| 7434 | |
| 7435 | |
| 7436 | |
| 7437 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 7438 | SourceLocation Loc) { |
| 7439 | (ND) || isa(ND)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 7439, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
| 7440 | NamedDecl *NewD = nullptr; |
| 7441 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) { |
| 7442 | FunctionDecl *NewFD; |
| 7443 | |
| 7444 | |
| 7445 | |
| 7446 | |
| 7447 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 7448 | Loc, Loc, DeclarationName(II), |
| 7449 | FD->getType(), FD->getTypeSourceInfo(), |
| 7450 | SC_None, false, |
| 7451 | FD->hasPrototype(), |
| 7452 | false); |
| 7453 | NewD = NewFD; |
| 7454 | |
| 7455 | if (FD->getQualifier()) |
| 7456 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
| 7457 | |
| 7458 | |
| 7459 | |
| 7460 | QualType FDTy = FD->getType(); |
| 7461 | if (const auto *FT = FDTy->getAs<FunctionProtoType>()) { |
| 7462 | SmallVector<ParmVarDecl*, 16> Params; |
| 7463 | for (const auto &AI : FT->param_types()) { |
| 7464 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
| 7465 | Param->setScopeInfo(0, Params.size()); |
| 7466 | Params.push_back(Param); |
| 7467 | } |
| 7468 | NewFD->setParams(Params); |
| 7469 | } |
| 7470 | } else if (auto *VD = dyn_cast<VarDecl>(ND)) { |
| 7471 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
| 7472 | VD->getInnerLocStart(), VD->getLocation(), II, |
| 7473 | VD->getType(), VD->getTypeSourceInfo(), |
| 7474 | VD->getStorageClass()); |
| 7475 | if (VD->getQualifier()) |
| 7476 | cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc()); |
| 7477 | } |
| 7478 | return NewD; |
| 7479 | } |
| 7480 | |
| 7481 | |
| 7482 | |
| 7483 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
| 7484 | if (W.getUsed()) return; |
| 7485 | W.setUsed(true); |
| 7486 | if (W.getAlias()) { |
| 7487 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 7488 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
| 7489 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 7490 | W.getLocation())); |
| 7491 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
| 7492 | WeakTopLevelDecl.push_back(NewD); |
| 7493 | |
| 7494 | |
| 7495 | DeclContext *SavedContext = CurContext; |
| 7496 | CurContext = Context.getTranslationUnitDecl(); |
| 7497 | NewD->setDeclContext(CurContext); |
| 7498 | NewD->setLexicalDeclContext(CurContext); |
| 7499 | PushOnScopeChains(NewD, S); |
| 7500 | CurContext = SavedContext; |
| 7501 | } else { |
| 7502 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
| 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 7507 | |
| 7508 | |
| 7509 | LoadExternalWeakUndeclaredIdentifiers(); |
| 7510 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 7511 | NamedDecl *ND = nullptr; |
| 7512 | if (auto *VD = dyn_cast<VarDecl>(D)) |
| 7513 | if (VD->isExternC()) |
| 7514 | ND = VD; |
| 7515 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 7516 | if (FD->isExternC()) |
| 7517 | ND = FD; |
| 7518 | if (ND) { |
| 7519 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 7520 | auto I = WeakUndeclaredIdentifiers.find(Id); |
| 7521 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 7522 | WeakInfo W = I->second; |
| 7523 | DeclApplyPragmaWeak(S, ND, W); |
| 7524 | WeakUndeclaredIdentifiers[Id] = W; |
| 7525 | } |
| 7526 | } |
| 7527 | } |
| 7528 | } |
| 7529 | } |
| 7530 | |
| 7531 | |
| 7532 | |
| 7533 | |
| 7534 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
| 7535 | |
| 7536 | if (!PD.getDeclSpec().getAttributes().empty()) |
| 7537 | ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes()); |
| 7538 | |
| 7539 | |
| 7540 | |
| 7541 | |
| 7542 | |
| 7543 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 7544 | ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(), |
| 7545 | ); |
| 7546 | |
| 7547 | |
| 7548 | ProcessDeclAttributeList(S, D, PD.getAttributes()); |
| 7549 | |
| 7550 | |
| 7551 | AddPragmaAttributes(S, D); |
| 7552 | } |
| 7553 | |
| 7554 | |
| 7555 | |
| 7556 | |
| 7557 | static bool isForbiddenTypeAllowed(Sema &S, Decl *D, |
| 7558 | const DelayedDiagnostic &diag, |
| 7559 | UnavailableAttr::ImplicitReason &reason) { |
| 7560 | |
| 7561 | |
| 7562 | |
| 7563 | if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) && |
| 7564 | !isa<FunctionDecl>(D)) |
| 7565 | return false; |
| 7566 | |
| 7567 | |
| 7568 | |
| 7569 | |
| 7570 | |
| 7571 | |
| 7572 | if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
| 7573 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 7574 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 7575 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 7576 | return true; |
| 7577 | } |
| 7578 | } |
| 7579 | |
| 7580 | |
| 7581 | if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) { |
| 7582 | |
| 7583 | |
| 7584 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 7585 | return true; |
| 7586 | } |
| 7587 | |
| 7588 | return false; |
| 7589 | } |
| 7590 | |
| 7591 | |
| 7592 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD, |
| 7593 | Decl *D) { |
| 7594 | auto Reason = UnavailableAttr::IR_None; |
| 7595 | if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) { |
| 7596 | (0) . __assert_fail ("Reason && \"didn't set reason?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 7596, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Reason && "didn't set reason?"); |
| 7597 | D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc)); |
| 7598 | return; |
| 7599 | } |
| 7600 | if (S.getLangOpts().ObjCAutoRefCount) |
| 7601 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 7602 | |
| 7603 | |
| 7604 | if (FD->hasAttr<UnavailableAttr>() && |
| 7605 | DD.getForbiddenTypeDiagnostic() == |
| 7606 | diag::err_arc_array_param_no_ownership) { |
| 7607 | DD.Triggered = true; |
| 7608 | return; |
| 7609 | } |
| 7610 | } |
| 7611 | |
| 7612 | S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic()) |
| 7613 | << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument(); |
| 7614 | DD.Triggered = true; |
| 7615 | } |
| 7616 | |
| 7617 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 7618 | const Decl *D) { |
| 7619 | |
| 7620 | for (const auto *A : D->attrs()) { |
| 7621 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 7622 | |
| 7623 | |
| 7624 | |
| 7625 | |
| 7626 | |
| 7627 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 7628 | StringRef RealizedPlatform = ActualPlatform; |
| 7629 | if (Context.getLangOpts().AppExt) { |
| 7630 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 7631 | if (suffix != StringRef::npos) |
| 7632 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 7633 | } |
| 7634 | |
| 7635 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 7636 | |
| 7637 | |
| 7638 | if (RealizedPlatform == TargetPlatform) |
| 7639 | return Avail; |
| 7640 | } |
| 7641 | } |
| 7642 | return nullptr; |
| 7643 | } |
| 7644 | |
| 7645 | |
| 7646 | |
| 7647 | |
| 7648 | |
| 7649 | |
| 7650 | |
| 7651 | |
| 7652 | |
| 7653 | static std::pair<AvailabilityResult, const NamedDecl *> |
| 7654 | ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D, |
| 7655 | std::string *Message, |
| 7656 | ObjCInterfaceDecl *ClassReceiver) { |
| 7657 | AvailabilityResult Result = D->getAvailability(Message); |
| 7658 | |
| 7659 | |
| 7660 | |
| 7661 | while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 7662 | if (Result == AR_Available) { |
| 7663 | if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
| 7664 | D = TT->getDecl(); |
| 7665 | Result = D->getAvailability(Message); |
| 7666 | continue; |
| 7667 | } |
| 7668 | } |
| 7669 | break; |
| 7670 | } |
| 7671 | |
| 7672 | |
| 7673 | if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 7674 | if (IDecl->getDefinition()) { |
| 7675 | D = IDecl->getDefinition(); |
| 7676 | Result = D->getAvailability(Message); |
| 7677 | } |
| 7678 | } |
| 7679 | |
| 7680 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 7681 | if (Result == AR_Available) { |
| 7682 | const DeclContext *DC = ECD->getDeclContext(); |
| 7683 | if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) { |
| 7684 | Result = TheEnumDecl->getAvailability(Message); |
| 7685 | D = TheEnumDecl; |
| 7686 | } |
| 7687 | } |
| 7688 | |
| 7689 | |
| 7690 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7691 | if (S.NSAPIObj && ClassReceiver) { |
| 7692 | ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod( |
| 7693 | S.NSAPIObj->getInitSelector()); |
| 7694 | if (Init && Result == AR_Available && MD->isClassMethod() && |
| 7695 | MD->getSelector() == S.NSAPIObj->getNewSelector() && |
| 7696 | MD->definedInNSObject(S.getASTContext())) { |
| 7697 | Result = Init->getAvailability(Message); |
| 7698 | D = Init; |
| 7699 | } |
| 7700 | } |
| 7701 | } |
| 7702 | |
| 7703 | return {Result, D}; |
| 7704 | } |
| 7705 | |
| 7706 | |
| 7707 | |
| 7708 | |
| 7709 | |
| 7710 | static bool |
| 7711 | ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 7712 | VersionTuple DeclVersion, Decl *Ctx, |
| 7713 | const NamedDecl *OffendingDecl) { |
| 7714 | (0) . __assert_fail ("K != AR_Available && \"Expected an unavailable declaration here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 7714, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 7715 | |
| 7716 | |
| 7717 | auto CheckContext = [&](const Decl *C) { |
| 7718 | if (K == AR_NotYetIntroduced) { |
| 7719 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 7720 | if (AA->getIntroduced() >= DeclVersion) |
| 7721 | return true; |
| 7722 | } else if (K == AR_Deprecated) { |
| 7723 | if (C->isDeprecated()) |
| 7724 | return true; |
| 7725 | } else if (K == AR_Unavailable) { |
| 7726 | |
| 7727 | |
| 7728 | |
| 7729 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) { |
| 7730 | if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) { |
| 7731 | if (MD->getClassInterface() == Impl->getClassInterface()) |
| 7732 | return true; |
| 7733 | } |
| 7734 | } |
| 7735 | } |
| 7736 | |
| 7737 | if (C->isUnavailable()) |
| 7738 | return true; |
| 7739 | return false; |
| 7740 | }; |
| 7741 | |
| 7742 | do { |
| 7743 | if (CheckContext(Ctx)) |
| 7744 | return false; |
| 7745 | |
| 7746 | |
| 7747 | |
| 7748 | if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx)) |
| 7749 | if (MethodD->isClassMethod() && |
| 7750 | MethodD->getSelector().getAsString() == "load") |
| 7751 | return true; |
| 7752 | |
| 7753 | if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
| 7754 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 7755 | if (CheckContext(Interface)) |
| 7756 | return false; |
| 7757 | } |
| 7758 | |
| 7759 | else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
| 7760 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 7761 | if (CheckContext(Interface)) |
| 7762 | return false; |
| 7763 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 7764 | |
| 7765 | return true; |
| 7766 | } |
| 7767 | |
| 7768 | static bool |
| 7769 | shouldDiagnoseAvailabilityByDefault(const ASTContext &Context, |
| 7770 | const VersionTuple &DeploymentVersion, |
| 7771 | const VersionTuple &DeclVersion) { |
| 7772 | const auto &Triple = Context.getTargetInfo().getTriple(); |
| 7773 | VersionTuple ForceAvailabilityFromVersion; |
| 7774 | switch (Triple.getOS()) { |
| 7775 | case llvm::Triple::IOS: |
| 7776 | case llvm::Triple::TvOS: |
| 7777 | ForceAvailabilityFromVersion = VersionTuple(); |
| 7778 | break; |
| 7779 | case llvm::Triple::WatchOS: |
| 7780 | ForceAvailabilityFromVersion = VersionTuple(); |
| 7781 | break; |
| 7782 | case llvm::Triple::Darwin: |
| 7783 | case llvm::Triple::MacOSX: |
| 7784 | ForceAvailabilityFromVersion = VersionTuple(, ); |
| 7785 | break; |
| 7786 | default: |
| 7787 | |
| 7788 | return Triple.getVendor() == llvm::Triple::Apple; |
| 7789 | } |
| 7790 | return DeploymentVersion >= ForceAvailabilityFromVersion || |
| 7791 | DeclVersion >= ForceAvailabilityFromVersion; |
| 7792 | } |
| 7793 | |
| 7794 | static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) { |
| 7795 | for (Decl *Ctx = OrigCtx; Ctx; |
| 7796 | Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) { |
| 7797 | if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx)) |
| 7798 | return cast<NamedDecl>(Ctx); |
| 7799 | if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) { |
| 7800 | if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx)) |
| 7801 | return Imp->getClassInterface(); |
| 7802 | return CD; |
| 7803 | } |
| 7804 | } |
| 7805 | |
| 7806 | return dyn_cast<NamedDecl>(OrigCtx); |
| 7807 | } |
| 7808 | |
| 7809 | namespace { |
| 7810 | |
| 7811 | struct AttributeInsertion { |
| 7812 | StringRef Prefix; |
| 7813 | SourceLocation Loc; |
| 7814 | StringRef Suffix; |
| 7815 | |
| 7816 | static AttributeInsertion createInsertionAfter(const NamedDecl *D) { |
| 7817 | return {" ", D->getEndLoc(), ""}; |
| 7818 | } |
| 7819 | static AttributeInsertion createInsertionAfter(SourceLocation Loc) { |
| 7820 | return {" ", Loc, ""}; |
| 7821 | } |
| 7822 | static AttributeInsertion createInsertionBefore(const NamedDecl *D) { |
| 7823 | return {"", D->getBeginLoc(), "\n"}; |
| 7824 | } |
| 7825 | }; |
| 7826 | |
| 7827 | } |
| 7828 | |
| 7829 | |
| 7830 | |
| 7831 | |
| 7832 | |
| 7833 | |
| 7834 | |
| 7835 | |
| 7836 | |
| 7837 | static Optional<unsigned> |
| 7838 | tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames, |
| 7839 | const LangOptions &LangOpts) { |
| 7840 | |
| 7841 | if (!Name.empty() && (Name.front() == '-' || Name.front() == '+')) |
| 7842 | Name = Name.drop_front(1); |
| 7843 | if (Name.empty()) |
| 7844 | return None; |
| 7845 | Name.split(SlotNames, ':'); |
| 7846 | unsigned NumParams; |
| 7847 | if (Name.back() == ':') { |
| 7848 | |
| 7849 | SlotNames.pop_back(); |
| 7850 | NumParams = SlotNames.size(); |
| 7851 | } else { |
| 7852 | if (SlotNames.size() != 1) |
| 7853 | |
| 7854 | return None; |
| 7855 | NumParams = 0; |
| 7856 | } |
| 7857 | |
| 7858 | bool AllowDollar = LangOpts.DollarIdents; |
| 7859 | for (StringRef S : SlotNames) { |
| 7860 | if (S.empty()) |
| 7861 | continue; |
| 7862 | if (!isValidIdentifier(S, AllowDollar)) |
| 7863 | return None; |
| 7864 | } |
| 7865 | return NumParams; |
| 7866 | } |
| 7867 | |
| 7868 | |
| 7869 | |
| 7870 | static Optional<AttributeInsertion> |
| 7871 | createAttributeInsertion(const NamedDecl *D, const SourceManager &SM, |
| 7872 | const LangOptions &LangOpts) { |
| 7873 | if (isa<ObjCPropertyDecl>(D)) |
| 7874 | return AttributeInsertion::createInsertionAfter(D); |
| 7875 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7876 | if (MD->hasBody()) |
| 7877 | return None; |
| 7878 | return AttributeInsertion::createInsertionAfter(D); |
| 7879 | } |
| 7880 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 7881 | SourceLocation Loc = |
| 7882 | Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts); |
| 7883 | if (Loc.isInvalid()) |
| 7884 | return None; |
| 7885 | |
| 7886 | return AttributeInsertion::createInsertionAfter(Loc); |
| 7887 | } |
| 7888 | return AttributeInsertion::createInsertionBefore(D); |
| 7889 | } |
| 7890 | |
| 7891 | |
| 7892 | |
| 7893 | |
| 7894 | |
| 7895 | |
| 7896 | |
| 7897 | |
| 7898 | |
| 7899 | |
| 7900 | |
| 7901 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
| 7902 | Decl *Ctx, const NamedDecl *ReferringDecl, |
| 7903 | const NamedDecl *OffendingDecl, |
| 7904 | StringRef Message, |
| 7905 | ArrayRef<SourceLocation> Locs, |
| 7906 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7907 | const ObjCPropertyDecl *ObjCProperty, |
| 7908 | bool ObjCPropertyAccess) { |
| 7909 | |
| 7910 | unsigned diag, diag_message, diag_fwdclass_message; |
| 7911 | unsigned diag_available_here = diag::note_availability_specified_here; |
| 7912 | SourceLocation NoteLocation = OffendingDecl->getLocation(); |
| 7913 | |
| 7914 | |
| 7915 | unsigned property_note_select; |
| 7916 | |
| 7917 | |
| 7918 | unsigned available_here_select_kind; |
| 7919 | |
| 7920 | VersionTuple DeclVersion; |
| 7921 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl)) |
| 7922 | DeclVersion = AA->getIntroduced(); |
| 7923 | |
| 7924 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx, |
| 7925 | OffendingDecl)) |
| 7926 | return; |
| 7927 | |
| 7928 | SourceLocation Loc = Locs.front(); |
| 7929 | |
| 7930 | |
| 7931 | |
| 7932 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl); |
| 7933 | if (A && A->isInherited()) { |
| 7934 | for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl; |
| 7935 | Redecl = Redecl->getPreviousDecl()) { |
| 7936 | const AvailabilityAttr *AForRedecl = |
| 7937 | getAttrForPlatform(S.Context, Redecl); |
| 7938 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 7939 | |
| 7940 | |
| 7941 | NoteLocation = Redecl->getLocation(); |
| 7942 | break; |
| 7943 | } |
| 7944 | } |
| 7945 | } |
| 7946 | |
| 7947 | switch (K) { |
| 7948 | case AR_NotYetIntroduced: { |
| 7949 | |
| 7950 | |
| 7951 | |
| 7952 | |
| 7953 | const AvailabilityAttr *AA = |
| 7954 | getAttrForPlatform(S.getASTContext(), OffendingDecl); |
| 7955 | VersionTuple Introduced = AA->getIntroduced(); |
| 7956 | |
| 7957 | bool UseNewWarning = shouldDiagnoseAvailabilityByDefault( |
| 7958 | S.Context, S.Context.getTargetInfo().getPlatformMinVersion(), |
| 7959 | Introduced); |
| 7960 | unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new |
| 7961 | : diag::warn_unguarded_availability; |
| 7962 | |
| 7963 | std::string PlatformName = AvailabilityAttr::getPrettyPlatformName( |
| 7964 | S.getASTContext().getTargetInfo().getPlatformName()); |
| 7965 | |
| 7966 | S.Diag(Loc, Warning) << OffendingDecl << PlatformName |
| 7967 | << Introduced.getAsString(); |
| 7968 | |
| 7969 | S.Diag(OffendingDecl->getLocation(), |
| 7970 | diag::note_partial_availability_specified_here) |
| 7971 | << OffendingDecl << PlatformName << Introduced.getAsString() |
| 7972 | << S.Context.getTargetInfo().getPlatformMinVersion().getAsString(); |
| 7973 | |
| 7974 | if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) { |
| 7975 | if (const auto *TD = dyn_cast<TagDecl>(Enclosing)) |
| 7976 | if (TD->getDeclName().isEmpty()) { |
| 7977 | S.Diag(TD->getLocation(), |
| 7978 | diag::note_decl_unguarded_availability_silence) |
| 7979 | << 1 << TD->getKindName(); |
| 7980 | return; |
| 7981 | } |
| 7982 | auto FixitNoteDiag = |
| 7983 | S.Diag(Enclosing->getLocation(), |
| 7984 | diag::note_decl_unguarded_availability_silence) |
| 7985 | << 0 << Enclosing; |
| 7986 | |
| 7987 | if (Enclosing->hasAttr<AvailabilityAttr>()) |
| 7988 | return; |
| 7989 | if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE")) |
| 7990 | return; |
| 7991 | Optional<AttributeInsertion> Insertion = createAttributeInsertion( |
| 7992 | Enclosing, S.getSourceManager(), S.getLangOpts()); |
| 7993 | if (!Insertion) |
| 7994 | return; |
| 7995 | std::string PlatformName = |
| 7996 | AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7997 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 7998 | .lower(); |
| 7999 | std::string Introduced = |
| 8000 | OffendingDecl->getVersionIntroduced().getAsString(); |
| 8001 | FixitNoteDiag << FixItHint::CreateInsertion( |
| 8002 | Insertion->Loc, |
| 8003 | (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName + |
| 8004 | "(" + Introduced + "))" + Insertion->Suffix) |
| 8005 | .str()); |
| 8006 | } |
| 8007 | return; |
| 8008 | } |
| 8009 | case AR_Deprecated: |
| 8010 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 8011 | : diag::warn_property_method_deprecated; |
| 8012 | diag_message = diag::warn_deprecated_message; |
| 8013 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 8014 | property_note_select = 0; |
| 8015 | available_here_select_kind = 2; |
| 8016 | if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 8017 | NoteLocation = AL->getLocation(); |
| 8018 | break; |
| 8019 | |
| 8020 | case AR_Unavailable: |
| 8021 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 8022 | : diag::err_property_method_unavailable; |
| 8023 | diag_message = diag::err_unavailable_message; |
| 8024 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 8025 | property_note_select = 1; |
| 8026 | available_here_select_kind = 0; |
| 8027 | |
| 8028 | if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) { |
| 8029 | if (AL->isImplicit() && AL->getImplicitReason()) { |
| 8030 | |
| 8031 | |
| 8032 | auto flagARCError = [&] { |
| 8033 | if (S.getLangOpts().ObjCAutoRefCount && |
| 8034 | S.getSourceManager().isInSystemHeader( |
| 8035 | OffendingDecl->getLocation())) |
| 8036 | diag = diag::err_unavailable_in_arc; |
| 8037 | }; |
| 8038 | |
| 8039 | switch (AL->getImplicitReason()) { |
| 8040 | case UnavailableAttr::IR_None: break; |
| 8041 | |
| 8042 | case UnavailableAttr::IR_ARCForbiddenType: |
| 8043 | flagARCError(); |
| 8044 | diag_available_here = diag::note_arc_forbidden_type; |
| 8045 | break; |
| 8046 | |
| 8047 | case UnavailableAttr::IR_ForbiddenWeak: |
| 8048 | if (S.getLangOpts().ObjCWeakRuntime) |
| 8049 | diag_available_here = diag::note_arc_weak_disabled; |
| 8050 | else |
| 8051 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 8052 | break; |
| 8053 | |
| 8054 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 8055 | flagARCError(); |
| 8056 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 8057 | break; |
| 8058 | |
| 8059 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 8060 | flagARCError(); |
| 8061 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 8062 | break; |
| 8063 | |
| 8064 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 8065 | flagARCError(); |
| 8066 | diag_available_here = diag::note_arc_field_with_ownership; |
| 8067 | break; |
| 8068 | } |
| 8069 | } |
| 8070 | } |
| 8071 | break; |
| 8072 | |
| 8073 | case AR_Available: |
| 8074 | llvm_unreachable("Warning for availability of available declaration?"); |
| 8075 | } |
| 8076 | |
| 8077 | SmallVector<FixItHint, 12> FixIts; |
| 8078 | if (K == AR_Deprecated) { |
| 8079 | StringRef Replacement; |
| 8080 | if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 8081 | Replacement = AL->getReplacement(); |
| 8082 | if (auto AL = getAttrForPlatform(S.Context, OffendingDecl)) |
| 8083 | Replacement = AL->getReplacement(); |
| 8084 | |
| 8085 | CharSourceRange UseRange; |
| 8086 | if (!Replacement.empty()) |
| 8087 | UseRange = |
| 8088 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
| 8089 | if (UseRange.isValid()) { |
| 8090 | if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) { |
| 8091 | Selector Sel = MethodDecl->getSelector(); |
| 8092 | SmallVector<StringRef, 12> SelectorSlotNames; |
| 8093 | Optional<unsigned> NumParams = tryParseObjCMethodName( |
| 8094 | Replacement, SelectorSlotNames, S.getLangOpts()); |
| 8095 | if (NumParams && NumParams.getValue() == Sel.getNumArgs()) { |
| 8096 | assert(SelectorSlotNames.size() == Locs.size()); |
| 8097 | for (unsigned I = 0; I < Locs.size(); ++I) { |
| 8098 | if (!Sel.getNameForSlot(I).empty()) { |
| 8099 | CharSourceRange NameRange = CharSourceRange::getCharRange( |
| 8100 | Locs[I], S.getLocForEndOfToken(Locs[I])); |
| 8101 | FixIts.push_back(FixItHint::CreateReplacement( |
| 8102 | NameRange, SelectorSlotNames[I])); |
| 8103 | } else |
| 8104 | FixIts.push_back( |
| 8105 | FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I])); |
| 8106 | } |
| 8107 | } else |
| 8108 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 8109 | } else |
| 8110 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 8111 | } |
| 8112 | } |
| 8113 | |
| 8114 | if (!Message.empty()) { |
| 8115 | S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts; |
| 8116 | if (ObjCProperty) |
| 8117 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 8118 | << ObjCProperty->getDeclName() << property_note_select; |
| 8119 | } else if (!UnknownObjCClass) { |
| 8120 | S.Diag(Loc, diag) << ReferringDecl << FixIts; |
| 8121 | if (ObjCProperty) |
| 8122 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 8123 | << ObjCProperty->getDeclName() << property_note_select; |
| 8124 | } else { |
| 8125 | S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts; |
| 8126 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 8127 | } |
| 8128 | |
| 8129 | S.Diag(NoteLocation, diag_available_here) |
| 8130 | << OffendingDecl << available_here_select_kind; |
| 8131 | } |
| 8132 | |
| 8133 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 8134 | Decl *Ctx) { |
| 8135 | (0) . __assert_fail ("DD.Kind == DelayedDiagnostic..Availability && \"Expected an availability diagnostic here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 8136, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DD.Kind == DelayedDiagnostic::Availability && |
| 8136 | (0) . __assert_fail ("DD.Kind == DelayedDiagnostic..Availability && \"Expected an availability diagnostic here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 8136, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expected an availability diagnostic here"); |
| 8137 | |
| 8138 | DD.Triggered = true; |
| 8139 | DoEmitAvailabilityWarning( |
| 8140 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(), |
| 8141 | DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), |
| 8142 | DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(), |
| 8143 | DD.getObjCProperty(), false); |
| 8144 | } |
| 8145 | |
| 8146 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 8147 | assert(DelayedDiagnostics.getCurrentPool()); |
| 8148 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
| 8149 | DelayedDiagnostics.popWithoutEmitting(state); |
| 8150 | |
| 8151 | |
| 8152 | |
| 8153 | |
| 8154 | if (!decl) return; |
| 8155 | |
| 8156 | |
| 8157 | |
| 8158 | |
| 8159 | |
| 8160 | |
| 8161 | |
| 8162 | |
| 8163 | const DelayedDiagnosticPool *pool = &poppedPool; |
| 8164 | do { |
| 8165 | bool AnyAccessFailures = false; |
| 8166 | for (DelayedDiagnosticPool::pool_iterator |
| 8167 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 8168 | |
| 8169 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
| 8170 | if (diag.Triggered) |
| 8171 | continue; |
| 8172 | |
| 8173 | switch (diag.Kind) { |
| 8174 | case DelayedDiagnostic::Availability: |
| 8175 | |
| 8176 | |
| 8177 | if (!decl->isInvalidDecl()) |
| 8178 | handleDelayedAvailabilityCheck(*this, diag, decl); |
| 8179 | break; |
| 8180 | |
| 8181 | case DelayedDiagnostic::Access: |
| 8182 | |
| 8183 | |
| 8184 | |
| 8185 | if (AnyAccessFailures && isa<DecompositionDecl>(decl)) |
| 8186 | continue; |
| 8187 | HandleDelayedAccessCheck(diag, decl); |
| 8188 | if (diag.Triggered) |
| 8189 | AnyAccessFailures = true; |
| 8190 | break; |
| 8191 | |
| 8192 | case DelayedDiagnostic::ForbiddenType: |
| 8193 | handleDelayedForbiddenType(*this, diag, decl); |
| 8194 | break; |
| 8195 | } |
| 8196 | } |
| 8197 | } while ((pool = pool->getParent())); |
| 8198 | } |
| 8199 | |
| 8200 | |
| 8201 | |
| 8202 | |
| 8203 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 8204 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 8205 | (0) . __assert_fail ("curPool && \"re-emitting in undelayed context not supported\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 8205, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(curPool && "re-emitting in undelayed context not supported"); |
| 8206 | curPool->steal(pool); |
| 8207 | } |
| 8208 | |
| 8209 | static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR, |
| 8210 | const NamedDecl *ReferringDecl, |
| 8211 | const NamedDecl *OffendingDecl, |
| 8212 | StringRef Message, |
| 8213 | ArrayRef<SourceLocation> Locs, |
| 8214 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 8215 | const ObjCPropertyDecl *ObjCProperty, |
| 8216 | bool ObjCPropertyAccess) { |
| 8217 | |
| 8218 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 8219 | S.DelayedDiagnostics.add( |
| 8220 | DelayedDiagnostic::makeAvailability( |
| 8221 | AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass, |
| 8222 | ObjCProperty, Message, ObjCPropertyAccess)); |
| 8223 | return; |
| 8224 | } |
| 8225 | |
| 8226 | Decl *Ctx = cast<Decl>(S.getCurLexicalContext()); |
| 8227 | DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl, |
| 8228 | Message, Locs, UnknownObjCClass, ObjCProperty, |
| 8229 | ObjCPropertyAccess); |
| 8230 | } |
| 8231 | |
| 8232 | namespace { |
| 8233 | |
| 8234 | |
| 8235 | bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) { |
| 8236 | switch (Parent->getStmtClass()) { |
| 8237 | case Stmt::IfStmtClass: |
| 8238 | return cast<IfStmt>(Parent)->getThen() == S || |
| 8239 | cast<IfStmt>(Parent)->getElse() == S; |
| 8240 | case Stmt::WhileStmtClass: |
| 8241 | return cast<WhileStmt>(Parent)->getBody() == S; |
| 8242 | case Stmt::DoStmtClass: |
| 8243 | return cast<DoStmt>(Parent)->getBody() == S; |
| 8244 | case Stmt::ForStmtClass: |
| 8245 | return cast<ForStmt>(Parent)->getBody() == S; |
| 8246 | case Stmt::CXXForRangeStmtClass: |
| 8247 | return cast<CXXForRangeStmt>(Parent)->getBody() == S; |
| 8248 | case Stmt::ObjCForCollectionStmtClass: |
| 8249 | return cast<ObjCForCollectionStmt>(Parent)->getBody() == S; |
| 8250 | case Stmt::CaseStmtClass: |
| 8251 | case Stmt::DefaultStmtClass: |
| 8252 | return cast<SwitchCase>(Parent)->getSubStmt() == S; |
| 8253 | default: |
| 8254 | return false; |
| 8255 | } |
| 8256 | } |
| 8257 | |
| 8258 | class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> { |
| 8259 | const Stmt *Target; |
| 8260 | |
| 8261 | public: |
| 8262 | bool VisitStmt(Stmt *S) { return S != Target; } |
| 8263 | |
| 8264 | |
| 8265 | static bool isContained(const Stmt *Target, const Decl *D) { |
| 8266 | StmtUSEFinder Visitor; |
| 8267 | Visitor.Target = Target; |
| 8268 | return !Visitor.TraverseDecl(const_cast<Decl *>(D)); |
| 8269 | } |
| 8270 | }; |
| 8271 | |
| 8272 | |
| 8273 | |
| 8274 | class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> { |
| 8275 | const Decl *D; |
| 8276 | |
| 8277 | public: |
| 8278 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 8279 | if (DRE->getDecl() == D) |
| 8280 | return false; |
| 8281 | return true; |
| 8282 | } |
| 8283 | |
| 8284 | static const Stmt *findLastStmtThatUsesDecl(const Decl *D, |
| 8285 | const CompoundStmt *Scope) { |
| 8286 | LastDeclUSEFinder Visitor; |
| 8287 | Visitor.D = D; |
| 8288 | for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) { |
| 8289 | const Stmt *S = *I; |
| 8290 | if (!Visitor.TraverseStmt(const_cast<Stmt *>(S))) |
| 8291 | return S; |
| 8292 | } |
| 8293 | return nullptr; |
| 8294 | } |
| 8295 | }; |
| 8296 | |
| 8297 | |
| 8298 | |
| 8299 | |
| 8300 | |
| 8301 | |
| 8302 | |
| 8303 | class DiagnoseUnguardedAvailability |
| 8304 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 8305 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 8306 | |
| 8307 | Sema &SemaRef; |
| 8308 | Decl *Ctx; |
| 8309 | |
| 8310 | |
| 8311 | SmallVector<VersionTuple, 8> AvailabilityStack; |
| 8312 | SmallVector<const Stmt *, 16> StmtStack; |
| 8313 | |
| 8314 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range, |
| 8315 | ObjCInterfaceDecl *ClassReceiver = nullptr); |
| 8316 | |
| 8317 | public: |
| 8318 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 8319 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 8320 | AvailabilityStack.push_back( |
| 8321 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
| 8322 | } |
| 8323 | |
| 8324 | bool TraverseDecl(Decl *D) { |
| 8325 | |
| 8326 | if (!D || isa<FunctionDecl>(D)) |
| 8327 | return true; |
| 8328 | return Base::TraverseDecl(D); |
| 8329 | } |
| 8330 | |
| 8331 | bool TraverseStmt(Stmt *S) { |
| 8332 | if (!S) |
| 8333 | return true; |
| 8334 | StmtStack.push_back(S); |
| 8335 | bool Result = Base::TraverseStmt(S); |
| 8336 | StmtStack.pop_back(); |
| 8337 | return Result; |
| 8338 | } |
| 8339 | |
| 8340 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 8341 | |
| 8342 | bool TraverseIfStmt(IfStmt *If); |
| 8343 | |
| 8344 | bool TraverseLambdaExpr(LambdaExpr *E) { return true; } |
| 8345 | |
| 8346 | |
| 8347 | |
| 8348 | bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); } |
| 8349 | |
| 8350 | bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) { |
| 8351 | if (PRE->isClassReceiver()) |
| 8352 | DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation()); |
| 8353 | return true; |
| 8354 | } |
| 8355 | |
| 8356 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
| 8357 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) { |
| 8358 | ObjCInterfaceDecl *ID = nullptr; |
| 8359 | QualType ReceiverTy = Msg->getClassReceiver(); |
| 8360 | if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType()) |
| 8361 | ID = ReceiverTy->getAsObjCInterfaceType()->getInterface(); |
| 8362 | |
| 8363 | DiagnoseDeclAvailability( |
| 8364 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID); |
| 8365 | } |
| 8366 | return true; |
| 8367 | } |
| 8368 | |
| 8369 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 8370 | DiagnoseDeclAvailability(DRE->getDecl(), |
| 8371 | SourceRange(DRE->getBeginLoc(), DRE->getEndLoc())); |
| 8372 | return true; |
| 8373 | } |
| 8374 | |
| 8375 | bool VisitMemberExpr(MemberExpr *ME) { |
| 8376 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
| 8377 | SourceRange(ME->getBeginLoc(), ME->getEndLoc())); |
| 8378 | return true; |
| 8379 | } |
| 8380 | |
| 8381 | bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { |
| 8382 | SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use) |
| 8383 | << (!SemaRef.getLangOpts().ObjC); |
| 8384 | return true; |
| 8385 | } |
| 8386 | |
| 8387 | bool VisitTypeLoc(TypeLoc Ty); |
| 8388 | }; |
| 8389 | |
| 8390 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
| 8391 | NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) { |
| 8392 | AvailabilityResult Result; |
| 8393 | const NamedDecl *OffendingDecl; |
| 8394 | std::tie(Result, OffendingDecl) = |
| 8395 | ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass); |
| 8396 | if (Result != AR_Available) { |
| 8397 | |
| 8398 | |
| 8399 | if (Result != AR_NotYetIntroduced) |
| 8400 | return; |
| 8401 | |
| 8402 | const AvailabilityAttr *AA = |
| 8403 | getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl); |
| 8404 | VersionTuple Introduced = AA->getIntroduced(); |
| 8405 | |
| 8406 | if (AvailabilityStack.back() >= Introduced) |
| 8407 | return; |
| 8408 | |
| 8409 | |
| 8410 | |
| 8411 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx, |
| 8412 | OffendingDecl)) |
| 8413 | return; |
| 8414 | |
| 8415 | |
| 8416 | |
| 8417 | |
| 8418 | |
| 8419 | unsigned DiagKind = |
| 8420 | shouldDiagnoseAvailabilityByDefault( |
| 8421 | SemaRef.Context, |
| 8422 | SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced) |
| 8423 | ? diag::warn_unguarded_availability_new |
| 8424 | : diag::warn_unguarded_availability; |
| 8425 | |
| 8426 | std::string PlatformName = AvailabilityAttr::getPrettyPlatformName( |
| 8427 | SemaRef.getASTContext().getTargetInfo().getPlatformName()); |
| 8428 | |
| 8429 | SemaRef.Diag(Range.getBegin(), DiagKind) |
| 8430 | << Range << D << PlatformName << Introduced.getAsString(); |
| 8431 | |
| 8432 | SemaRef.Diag(OffendingDecl->getLocation(), |
| 8433 | diag::note_partial_availability_specified_here) |
| 8434 | << OffendingDecl << PlatformName << Introduced.getAsString() |
| 8435 | << SemaRef.Context.getTargetInfo() |
| 8436 | .getPlatformMinVersion() |
| 8437 | .getAsString(); |
| 8438 | |
| 8439 | auto FixitDiag = |
| 8440 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 8441 | << Range << D |
| 8442 | << (SemaRef.getLangOpts().ObjC ? 0 |
| 8443 | : 1); |
| 8444 | |
| 8445 | |
| 8446 | if (StmtStack.empty()) |
| 8447 | return; |
| 8448 | const Stmt *StmtOfUse = StmtStack.back(); |
| 8449 | const CompoundStmt *Scope = nullptr; |
| 8450 | for (const Stmt *S : llvm::reverse(StmtStack)) { |
| 8451 | if (const auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 8452 | Scope = CS; |
| 8453 | break; |
| 8454 | } |
| 8455 | if (isBodyLikeChildStmt(StmtOfUse, S)) { |
| 8456 | |
| 8457 | |
| 8458 | |
| 8459 | break; |
| 8460 | } |
| 8461 | StmtOfUse = S; |
| 8462 | } |
| 8463 | const Stmt *LastStmtOfUse = nullptr; |
| 8464 | if (isa<DeclStmt>(StmtOfUse) && Scope) { |
| 8465 | for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) { |
| 8466 | if (StmtUSEFinder::isContained(StmtStack.back(), D)) { |
| 8467 | LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope); |
| 8468 | break; |
| 8469 | } |
| 8470 | } |
| 8471 | } |
| 8472 | |
| 8473 | const SourceManager &SM = SemaRef.getSourceManager(); |
| 8474 | SourceLocation IfInsertionLoc = |
| 8475 | SM.getExpansionLoc(StmtOfUse->getBeginLoc()); |
| 8476 | SourceLocation StmtEndLoc = |
| 8477 | SM.getExpansionRange( |
| 8478 | (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc()) |
| 8479 | .getEnd(); |
| 8480 | if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc)) |
| 8481 | return; |
| 8482 | |
| 8483 | StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM); |
| 8484 | const char * = " "; |
| 8485 | std::string FixItString; |
| 8486 | llvm::raw_string_ostream FixItOS(FixItString); |
| 8487 | FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available" |
| 8488 | : "__builtin_available") |
| 8489 | << "(" |
| 8490 | << AvailabilityAttr::getPlatformNameSourceSpelling( |
| 8491 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 8492 | << " " << Introduced.getAsString() << ", *)) {\n" |
| 8493 | << Indentation << ExtraIndentation; |
| 8494 | FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str()); |
| 8495 | SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken( |
| 8496 | StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(), |
| 8497 | ); |
| 8498 | if (ElseInsertionLoc.isInvalid()) |
| 8499 | ElseInsertionLoc = |
| 8500 | Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts()); |
| 8501 | FixItOS.str().clear(); |
| 8502 | FixItOS << "\n" |
| 8503 | << Indentation << "} else {\n" |
| 8504 | << Indentation << ExtraIndentation |
| 8505 | << "// Fallback on earlier versions\n" |
| 8506 | << Indentation << "}"; |
| 8507 | FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str()); |
| 8508 | } |
| 8509 | } |
| 8510 | |
| 8511 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 8512 | const Type *TyPtr = Ty.getTypePtr(); |
| 8513 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 8514 | |
| 8515 | if (Range.isInvalid()) |
| 8516 | return true; |
| 8517 | |
| 8518 | if (const auto *TT = dyn_cast<TagType>(TyPtr)) { |
| 8519 | TagDecl *TD = TT->getDecl(); |
| 8520 | DiagnoseDeclAvailability(TD, Range); |
| 8521 | |
| 8522 | } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) { |
| 8523 | TypedefNameDecl *D = TD->getDecl(); |
| 8524 | DiagnoseDeclAvailability(D, Range); |
| 8525 | |
| 8526 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 8527 | if (NamedDecl *D = ObjCO->getInterface()) |
| 8528 | DiagnoseDeclAvailability(D, Range); |
| 8529 | } |
| 8530 | |
| 8531 | return true; |
| 8532 | } |
| 8533 | |
| 8534 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 8535 | VersionTuple CondVersion; |
| 8536 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 8537 | CondVersion = E->getVersion(); |
| 8538 | |
| 8539 | |
| 8540 | |
| 8541 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
| 8542 | return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse()); |
| 8543 | } else { |
| 8544 | |
| 8545 | return Base::TraverseIfStmt(If); |
| 8546 | } |
| 8547 | |
| 8548 | AvailabilityStack.push_back(CondVersion); |
| 8549 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 8550 | AvailabilityStack.pop_back(); |
| 8551 | |
| 8552 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 8553 | } |
| 8554 | |
| 8555 | } |
| 8556 | |
| 8557 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 8558 | Stmt *Body = nullptr; |
| 8559 | |
| 8560 | if (auto *FD = D->getAsFunction()) { |
| 8561 | |
| 8562 | |
| 8563 | if (FD->isTemplateInstantiation()) |
| 8564 | return; |
| 8565 | |
| 8566 | Body = FD->getBody(); |
| 8567 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8568 | Body = MD->getBody(); |
| 8569 | else if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 8570 | Body = BD->getBody(); |
| 8571 | |
| 8572 | (0) . __assert_fail ("Body && \"Need a body here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclAttr.cpp", 8572, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Body && "Need a body here!"); |
| 8573 | |
| 8574 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
| 8575 | } |
| 8576 | |
| 8577 | void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, |
| 8578 | ArrayRef<SourceLocation> Locs, |
| 8579 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 8580 | bool ObjCPropertyAccess, |
| 8581 | bool AvoidPartialAvailabilityChecks, |
| 8582 | ObjCInterfaceDecl *ClassReceiver) { |
| 8583 | std::string Message; |
| 8584 | AvailabilityResult Result; |
| 8585 | const NamedDecl* OffendingDecl; |
| 8586 | |
| 8587 | std::tie(Result, OffendingDecl) = |
| 8588 | ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver); |
| 8589 | if (Result == AR_Available) |
| 8590 | return; |
| 8591 | |
| 8592 | if (Result == AR_NotYetIntroduced) { |
| 8593 | if (AvoidPartialAvailabilityChecks) |
| 8594 | return; |
| 8595 | |
| 8596 | |
| 8597 | |
| 8598 | |
| 8599 | if (getCurFunctionOrMethodDecl()) { |
| 8600 | getEnclosingFunction()->HasPotentialAvailabilityViolations = true; |
| 8601 | return; |
| 8602 | } else if (getCurBlock() || getCurLambda()) { |
| 8603 | getCurFunction()->HasPotentialAvailabilityViolations = true; |
| 8604 | return; |
| 8605 | } |
| 8606 | } |
| 8607 | |
| 8608 | const ObjCPropertyDecl *ObjCPDecl = nullptr; |
| 8609 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 8610 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 8611 | AvailabilityResult PDeclResult = PD->getAvailability(nullptr); |
| 8612 | if (PDeclResult == Result) |
| 8613 | ObjCPDecl = PD; |
| 8614 | } |
| 8615 | } |
| 8616 | |
| 8617 | EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs, |
| 8618 | UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess); |
| 8619 | } |
| 8620 | |