https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/118315
None >From 8a9fc5dfc40ecc46a21ce7c537e2ffe0dbbac8b0 Mon Sep 17 00:00:00 2001 From: Congcong Cai <congcongcai0...@163.com> Date: Mon, 2 Dec 2024 23:50:12 +0800 Subject: [PATCH] [clang-tidy] ignore `[[clang::lifetimebound]]` param in return-const-ref-from-parameter --- .../ReturnConstRefFromParameterCheck.cpp | 12 ++++++++- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++- .../return-const-ref-from-parameter.rst | 27 ++++++++++++------- .../return-const-ref-from-parameter.cpp | 6 +++++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp index 7da27c0474d519..06e1c5c407b175 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "ReturnConstRefFromParameterCheck.h" +#include "clang/AST/Attrs.inc" #include "clang/AST/Expr.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -15,6 +16,14 @@ using namespace clang::ast_matchers; namespace clang::tidy::bugprone { +namespace { + +AST_MATCHER(ParmVarDecl, hasLifetimeBoundAttr) { + return Node.getAttr<LifetimeBoundAttr>() != nullptr; +} + +} // namespace + void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) { const auto DRef = ignoringParens( declRefExpr( @@ -22,7 +31,8 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) { qualType(lValueReferenceType(pointee( qualType(isConstQualified())))) .bind("type"))), - hasDeclContext(functionDecl().bind("owner"))) + hasDeclContext(functionDecl().bind("owner")), + unless(hasLifetimeBoundAttr())) .bind("param"))) .bind("dref")); const auto Func = diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 453a91e3b504cd..c7d0c0f167e581 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -184,7 +184,8 @@ Changes in existing checks <clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check to diagnose potential dangling references when returning a ``const &`` parameter by using the conditional operator ``cond ? var1 : var2`` and no longer giving - false positives for functions which contain lambda. + false positives for functions which contain lambda and ignore the parameters + with ``[[clang::lifetimebound]]`` attribute. - Improved :doc:`bugprone-sizeof-expression <clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst index 2349e51477b7de..aa2a43b6e8e6d1 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst @@ -12,15 +12,6 @@ after the call. When the function returns such a parameter also as constant refe then the returned reference can be used after the object it refers to has been destroyed. -This issue can be resolved by declaring an overload of the problematic function -where the ``const &`` parameter is instead declared as ``&&``. The developer has -to ensure that the implementation of that function does not produce a -use-after-free, the exact error that this check is warning against. -Marking such an ``&&`` overload as ``deleted``, will silence the warning as -well. In the case of different ``const &`` parameters being returned depending -on the control flow of the function, an overload where all problematic -``const &`` parameters have been declared as ``&&`` will resolve the issue. - Example ------- @@ -38,3 +29,21 @@ Example const S& s = fn(S{1}); s.v; // use after free + + +This issue can be resolved by declaring an overload of the problematic function +where the ``const &`` parameter is instead declared as ``&&``. The developer has +to ensure that the implementation of that function does not produce a +use-after-free, the exact error that this check is warning against. +Marking such an ``&&`` overload as ``deleted``, will silence the warning as +well. In the case of different ``const &`` parameters being returned depending +on the control flow of the function, an overload where all problematic +``const &`` parameters have been declared as ``&&`` will resolve the issue. + +This issue can also be resolved by adding ``[[clang::lifetimebound]]`` and +enabling ``-Wdangling`` in clang. See `lifetimebound attribute<https://clang.llvm.org/docs/AttributeReference.html#id11>`_ +for details. + +.. code-block:: c++ + const int &f(const int &a [[clang::lifetimebound]]) { return a; } + const int &v = f(1); // warning: temporary bound to local reference 'v' will be destroyed at the end of the full-expression [-Wdangling] diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp index 49aeb50155b157..46cb9063beda97 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp @@ -197,3 +197,9 @@ int const &overload_params_difference3(int p1, int const &a, int p2) { return a; int const &overload_params_difference3(int p1, long &&a, int p2); } // namespace overload + +namespace gh117696 { +namespace use_lifetime_bound_attr { +int const &f(int const &a [[clang::lifetimebound]]) { return a; } +} // namespace use_lifetime_bound_attr +} // namespace gh117696 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits