[clang-tools-extra] 57907c1 - [clang-tidy] ignore `[[clang::lifetimebound]]` param in return-const-ref-from-parameter (#118315)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 3 06:25:32 PST 2024
Author: Congcong Cai
Date: 2024-12-03T22:25:29+08:00
New Revision: 57907c1a96e82c17b16111b919ab7c0f3d4370ab
URL: https://github.com/llvm/llvm-project/commit/57907c1a96e82c17b16111b919ab7c0f3d4370ab
DIFF: https://github.com/llvm/llvm-project/commit/57907c1a96e82c17b16111b919ab7c0f3d4370ab.diff
LOG: [clang-tidy] ignore `[[clang::lifetimebound]]` param in return-const-ref-from-parameter (#118315)
Fixed #117696
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst
clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
index 7da27c0474d519..1bd7abbad66d27 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.hasAttr<LifetimeBoundAttr>();
+}
+
+} // 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..e00f86f7d01447 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 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..ba47399914de3f 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
diff erent ``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,23 @@ 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
diff erent ``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]]``. Clang
+enable ``-Wdangling`` warning by default which can detect mis-uses of the
+annotated function. 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; } // no warning
+ 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_
diff erence3(int p1, int const &a, int p2) { return a;
int const &overload_params_
diff erence3(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
More information about the cfe-commits
mailing list