Mordante updated this revision to Diff 290809. Mordante added a comment. Update the patch to match the behaviour where the attribute only affects the substatement of an if statement. This means the likelihood attributes on labels are accepted but are a no-op. Since they're a no-op the documentation doesn't mention them.
CHANGES SINCE LAST ACTION https://reviews.llvm.org/D86559/new/ https://reviews.llvm.org/D86559 Files: clang/lib/Sema/SemaDeclAttr.cpp clang/test/Sema/attr-likelihood.c clang/test/SemaCXX/attr-likelihood.cpp Index: clang/test/SemaCXX/attr-likelihood.cpp =================================================================== --- clang/test/SemaCXX/attr-likelihood.cpp +++ clang/test/SemaCXX/attr-likelihood.cpp @@ -115,8 +115,7 @@ if (x) goto lbl; - // FIXME: allow the attribute on the label - [[unlikely]] lbl : // expected-error {{'unlikely' attribute cannot be applied to a declaration}} + [[likely]] lbl : [[likely]] x = x + 1; [[likely]]++ x; Index: clang/test/Sema/attr-likelihood.c =================================================================== --- clang/test/Sema/attr-likelihood.c +++ clang/test/Sema/attr-likelihood.c @@ -43,8 +43,7 @@ if (x) goto lbl; - // FIXME: allow the attribute on the label - [[clang::unlikely]] lbl : // expected-error {{'unlikely' attribute cannot be applied to a declaration}} + [[clang::unlikely]] lbl : [[clang::likely]] x = x + 1; [[clang::likely]]++ x; Index: clang/lib/Sema/SemaDeclAttr.cpp =================================================================== --- clang/lib/Sema/SemaDeclAttr.cpp +++ clang/lib/Sema/SemaDeclAttr.cpp @@ -6451,6 +6451,29 @@ D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement)); } +static bool validateLikelihoodAttr(Sema &S, Decl *D, const ParsedAttr &A) { + if (!isa<LabelDecl>(D)) { + S.Diag(A.getRange().getBegin(), diag::err_stmt_attribute_invalid_on_decl) + << A << D->getBeginLoc(); + return false; + } + + if (!S.getLangOpts().CPlusPlus20 && A.isCXX11Attribute() && !A.getScopeName()) + S.Diag(A.getLoc(), diag::ext_cxx20_attr) << A << A.getRange(); + + return true; +} + +static void handleLikelyAttr(Sema &S, Decl *D, const ParsedAttr &A) { + if (validateLikelihoodAttr(S, D, A)) + D->addAttr(::new (S.Context) LikelyAttr(S.Context, A)); +} + +static void handleUnlikelyAttr(Sema &S, Decl *D, const ParsedAttr &A) { + if (validateLikelihoodAttr(S, D, A)) + D->addAttr(::new (S.Context) UnlikelyAttr(S.Context, A)); +} + static bool isGlobalVar(const Decl *D) { if (const auto *S = dyn_cast<VarDecl>(D)) return S->hasGlobalStorage(); @@ -6984,6 +7007,12 @@ case ParsedAttr::AT_Deprecated: handleDeprecatedAttr(S, D, AL); break; + case ParsedAttr::AT_Likely: + handleLikelyAttr(S, D, AL); + break; + case ParsedAttr::AT_Unlikely: + handleUnlikelyAttr(S, D, AL); + break; case ParsedAttr::AT_Destructor: if (S.Context.getTargetInfo().getTriple().isOSAIX()) llvm::report_fatal_error("'destructor' attribute is not yet supported on AIX");
Index: clang/test/SemaCXX/attr-likelihood.cpp =================================================================== --- clang/test/SemaCXX/attr-likelihood.cpp +++ clang/test/SemaCXX/attr-likelihood.cpp @@ -115,8 +115,7 @@ if (x) goto lbl; - // FIXME: allow the attribute on the label - [[unlikely]] lbl : // expected-error {{'unlikely' attribute cannot be applied to a declaration}} + [[likely]] lbl : [[likely]] x = x + 1; [[likely]]++ x; Index: clang/test/Sema/attr-likelihood.c =================================================================== --- clang/test/Sema/attr-likelihood.c +++ clang/test/Sema/attr-likelihood.c @@ -43,8 +43,7 @@ if (x) goto lbl; - // FIXME: allow the attribute on the label - [[clang::unlikely]] lbl : // expected-error {{'unlikely' attribute cannot be applied to a declaration}} + [[clang::unlikely]] lbl : [[clang::likely]] x = x + 1; [[clang::likely]]++ x; Index: clang/lib/Sema/SemaDeclAttr.cpp =================================================================== --- clang/lib/Sema/SemaDeclAttr.cpp +++ clang/lib/Sema/SemaDeclAttr.cpp @@ -6451,6 +6451,29 @@ D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement)); } +static bool validateLikelihoodAttr(Sema &S, Decl *D, const ParsedAttr &A) { + if (!isa<LabelDecl>(D)) { + S.Diag(A.getRange().getBegin(), diag::err_stmt_attribute_invalid_on_decl) + << A << D->getBeginLoc(); + return false; + } + + if (!S.getLangOpts().CPlusPlus20 && A.isCXX11Attribute() && !A.getScopeName()) + S.Diag(A.getLoc(), diag::ext_cxx20_attr) << A << A.getRange(); + + return true; +} + +static void handleLikelyAttr(Sema &S, Decl *D, const ParsedAttr &A) { + if (validateLikelihoodAttr(S, D, A)) + D->addAttr(::new (S.Context) LikelyAttr(S.Context, A)); +} + +static void handleUnlikelyAttr(Sema &S, Decl *D, const ParsedAttr &A) { + if (validateLikelihoodAttr(S, D, A)) + D->addAttr(::new (S.Context) UnlikelyAttr(S.Context, A)); +} + static bool isGlobalVar(const Decl *D) { if (const auto *S = dyn_cast<VarDecl>(D)) return S->hasGlobalStorage(); @@ -6984,6 +7007,12 @@ case ParsedAttr::AT_Deprecated: handleDeprecatedAttr(S, D, AL); break; + case ParsedAttr::AT_Likely: + handleLikelyAttr(S, D, AL); + break; + case ParsedAttr::AT_Unlikely: + handleUnlikelyAttr(S, D, AL); + break; case ParsedAttr::AT_Destructor: if (S.Context.getTargetInfo().getTriple().isOSAIX()) llvm::report_fatal_error("'destructor' attribute is not yet supported on AIX");
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits